grid: clean up kiln state initialization

This commit is contained in:
tomholford 2022-11-04 00:05:43 -07:00
parent ac3dc72983
commit 29f42aff1f
2 changed files with 13 additions and 26 deletions

View File

@ -86,10 +86,8 @@ const AppRoutes = () => {
fetchCharges(); fetchCharges();
fetchAllies(); fetchAllies();
const { fetchVats, fetchLag, fetchPikes } = useKilnState.getState(); const { initializeKiln } = useKilnState.getState();
fetchVats(); initializeKiln();
fetchLag();
fetchPikes();
useContactState.getState().initialize(api); useContactState.getState().initialize(api);
useHarkState.getState().start(); useHarkState.getState().start();

View File

@ -22,23 +22,19 @@ interface KilnState {
vats: Vats; vats: Vats;
pikes: Pikes; pikes: Pikes;
loaded: boolean; loaded: boolean;
fetchVats: () => Promise<void>;
lag: boolean; lag: boolean;
fetchLag: () => Promise<void>; fetchLag: () => Promise<void>;
fetchPikes: () => Promise<void>; fetchPikes: () => Promise<void>;
changeOTASource: (ship: string) => Promise<void>; changeOTASource: (ship: string) => Promise<void>;
toggleOTAs: (desk: string, on: boolean) => Promise<void>; toggleOTAs: (desk: string, on: boolean) => Promise<void>;
set: (s: KilnState) => void; set: (s: KilnState) => void;
initializeKiln: () => Promise<void>;
} }
const useKilnState = create<KilnState>((set, get) => ({ const useKilnState = create<KilnState>((set, get) => ({
vats: {}, vats: {},
pikes: useMockData ? mockPikes : {}, pikes: useMockData ? mockPikes : {},
lag: false, lag: false,
loaded: false, loaded: false,
fetchVats: async () => {
const vats = await api.scry<Vats>(getVats);
set({ vats, loaded: true });
},
fetchPikes: async () => { fetchPikes: async () => {
if (useMockData) { if (useMockData) {
await fakeRequest({}, 500); await fakeRequest({}, 500);
@ -58,31 +54,24 @@ const useKilnState = create<KilnState>((set, get) => ({
toggleOTAs: async (desk: string, on: boolean) => { toggleOTAs: async (desk: string, on: boolean) => {
set( set(
produce((draft: KilnState) => { produce((draft: KilnState) => {
const { arak } = draft.vats[desk]; const pike = draft.pikes[desk];
if (!arak.rail) { if (!pike) {
return; return;
} }
if (on) {
arak.rail.paused = false; pike.zest = on ? 'live' : 'held';
} else {
arak.rail.paused = true;
}
}) })
); );
await api.poke(on ? kilnResume(desk) : kilnPause(desk)); await api.poke(on ? kilnResume(desk) : kilnPause(desk));
await get().fetchVats(); // refresh vat state await get().fetchPikes(); // refresh pikes state
}, },
set: produce(set) set: produce(set),
})); initializeKiln: async () => {
await get().fetchLag();
api.subscribe({ await get().fetchPikes();
app: 'hood',
path: '/kiln/vats',
event: () => {
useKilnState.getState().fetchVats();
} }
}); }));
const selBlockers = (s: KilnState) => getBlockers(s.vats); const selBlockers = (s: KilnState) => getBlockers(s.vats);
export function useBlockers() { export function useBlockers() {