From 1375a7fac1e2bc67c7948d5174af50a18c418579 Mon Sep 17 00:00:00 2001 From: Liam Fitzgerald Date: Thu, 22 Jul 2021 13:17:03 +1000 Subject: [PATCH] interface: clear state on reset --- pkg/interface/src/logic/api/bootstrap.ts | 1 + pkg/interface/src/logic/state/base.ts | 9 +++++++-- pkg/interface/src/logic/state/graph.ts | 12 ++++++++++-- 3 files changed, 18 insertions(+), 4 deletions(-) diff --git a/pkg/interface/src/logic/api/bootstrap.ts b/pkg/interface/src/logic/api/bootstrap.ts index c1816bf739..5682952bea 100644 --- a/pkg/interface/src/logic/api/bootstrap.ts +++ b/pkg/interface/src/logic/api/bootstrap.ts @@ -29,6 +29,7 @@ export async function bootstrapApi() { airlock.onOpen = () => { useLocalState.setState({ subscription: 'connected' }); + [useGraphState].map(s => s.getState()?.clear?.()); }; const promises = [ diff --git a/pkg/interface/src/logic/state/base.ts b/pkg/interface/src/logic/state/base.ts index 87ccd55766..ee1e0cb657 100644 --- a/pkg/interface/src/logic/state/base.ts +++ b/pkg/interface/src/logic/state/base.ts @@ -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 { removePatch: (id: string) => void; optSet: (fn: (state: StateType & BaseState) => void) => string; initialize: (api: Urbit) => Promise; + clear: () => void; } export function createSubscription(app: string, path: string, e: (data: any) => void): SubscriptionRequestInterface { @@ -114,8 +115,12 @@ export const createState = ( name: string, properties: T | ((set: SetState>, get: GetState>) => T), blacklist: (keyof BaseState | keyof T)[] = [], - subscriptions: ((set: SetState>, get: GetState>) => SubscriptionRequestInterface)[] = [] + subscriptions: ((set: SetState>, get: GetState>) => SubscriptionRequestInterface)[] = [], + clear?: PartialState> ): UseStore> => create>(persist>((set, get) => ({ + clear: () => { + set(clear); + }, initialize: async (api: Urbit) => { await Promise.all(subscriptions.map(sub => api.subscribe(sub(set, get)))); }, diff --git a/pkg/interface/src/logic/state/graph.ts b/pkg/interface/src/logic/state/graph.ts index fe9df71d8a..626b0570f2 100644 --- a/pkg/interface/src/logic/state/graph.ts +++ b/pkg/interface/src/logic/state/graph.ts @@ -252,9 +252,17 @@ const useGraphState = createState('Graph', (set, get) => ({ if(j) { reduceStateN(get(), j, reduceDm); } - }) + })], + { + graphs: {}, + looseNodes: {}, + graphTimesentMap: {}, + flatGraphs: {}, + threadGraphs: {}, + pendingDms: new Set() + } -]); +); export function useGraph(ship: string, name: string) { return useGraphState(