interface: clear state on reset

This commit is contained in:
Liam Fitzgerald 2021-07-22 13:17:03 +10:00
parent e015e6930a
commit 1375a7fac1
No known key found for this signature in database
GPG Key ID: D390E12C61D1CFFB
3 changed files with 18 additions and 4 deletions

View File

@ -29,6 +29,7 @@ export async function bootstrapApi() {
airlock.onOpen = () => {
useLocalState.setState({ subscription: 'connected' });
[useGraphState].map(s => s.getState()?.clear?.());
};
const promises = [

View File

@ -1,7 +1,7 @@
import { applyPatches, Patch, produceWithPatches, setAutoFreeze, enablePatches } from 'immer';
import { compose } from 'lodash/fp';
import _ from 'lodash';
import create, { GetState, SetState, UseStore } from 'zustand';
import create, { GetState, SetState, UseStore, PartialState } from 'zustand';
import { persist } from 'zustand/middleware';
import Urbit, { SubscriptionRequestInterface, FatalError } from '@urbit/http-api';
import { Poke } from '@urbit/api';
@ -94,6 +94,7 @@ export interface BaseState<StateType extends {}> {
removePatch: (id: string) => void;
optSet: (fn: (state: StateType & BaseState<StateType>) => void) => string;
initialize: (api: Urbit) => Promise<void>;
clear: () => void;
}
export function createSubscription(app: string, path: string, e: (data: any) => void): SubscriptionRequestInterface {
@ -114,8 +115,12 @@ export const createState = <T extends {}>(
name: string,
properties: T | ((set: SetState<T & BaseState<T>>, get: GetState<T & BaseState<T>>) => T),
blacklist: (keyof BaseState<T> | keyof T)[] = [],
subscriptions: ((set: SetState<T & BaseState<T>>, get: GetState<T & BaseState<T>>) => SubscriptionRequestInterface)[] = []
subscriptions: ((set: SetState<T & BaseState<T>>, get: GetState<T & BaseState<T>>) => SubscriptionRequestInterface)[] = [],
clear?: PartialState<T & BaseState<T>>
): UseStore<T & BaseState<T>> => create<T & BaseState<T>>(persist<T & BaseState<T>>((set, get) => ({
clear: () => {
set(clear);
},
initialize: async (api: Urbit) => {
await Promise.all(subscriptions.map(sub => api.subscribe(sub(set, get))));
},

View File

@ -252,9 +252,17 @@ const useGraphState = createState<GraphState>('Graph', (set, get) => ({
if(j) {
reduceStateN(get(), j, reduceDm);
}
})
})],
{
graphs: {},
looseNodes: {},
graphTimesentMap: {},
flatGraphs: {},
threadGraphs: {},
pendingDms: new Set<string>()
}
]);
);
export function useGraph(ship: string, name: string) {
return useGraphState(