diff --git a/frontend/app/store/wos.ts b/frontend/app/store/wos.ts index e30a43e4d..bbc8bcb98 100644 --- a/frontend/app/store/wos.ts +++ b/frontend/app/store/wos.ts @@ -25,6 +25,8 @@ type WaveObjectValue = { holdTime: number; }; +type WritableWaveObjectAtom = jotai.WritableAtom; + function splitORef(oref: string): [string, string] { const parts = oref.split(":"); if (parts.length != 2) { @@ -186,12 +188,17 @@ function createWaveValueObject(oref: string, shouldFetch: boo return wov; } -function loadAndPinWaveObject(oref: string): Promise { +function getWaveObjectValue(oref: string, createIfMissing = true): WaveObjectValue { let wov = waveObjectValueCache.get(oref); - if (wov == null) { + if (wov === undefined && createIfMissing) { wov = createWaveValueObject(oref, true); waveObjectValueCache.set(oref, wov); } + return wov; +} + +function loadAndPinWaveObject(oref: string): Promise { + const wov = getWaveObjectValue(oref); wov.refCount++; if (wov.pendingPromise == null) { const dataValue = globalStore.get(wov.dataAtom); @@ -200,31 +207,8 @@ function loadAndPinWaveObject(oref: string): Promise { return wov.pendingPromise; } -function useWaveObjectValueWithSuspense(oref: string): T { - let wov = waveObjectValueCache.get(oref); - if (wov == null) { - wov = createWaveValueObject(oref, true); - waveObjectValueCache.set(oref, wov); - } - React.useEffect(() => { - wov.refCount++; - return () => { - wov.refCount--; - }; - }, [oref]); - const dataValue = jotai.useAtomValue(wov.dataAtom); - if (dataValue.loading) { - throw wov.pendingPromise; - } - return dataValue.value; -} - -function getWaveObjectAtom(oref: string): jotai.WritableAtom { - let wov = waveObjectValueCache.get(oref); - if (wov == null) { - wov = createWaveValueObject(oref, true); - waveObjectValueCache.set(oref, wov); - } +function getWaveObjectAtom(oref: string): WritableWaveObjectAtom { + const wov = getWaveObjectValue(oref); return jotai.atom( (get) => get(wov.dataAtom).value, (_get, set, value: T) => { @@ -234,11 +218,7 @@ function getWaveObjectAtom(oref: string): jotai.WritableAtom< } function getWaveObjectLoadingAtom(oref: string): jotai.Atom { - let wov = waveObjectValueCache.get(oref); - if (wov == null) { - wov = createWaveValueObject(oref, true); - waveObjectValueCache.set(oref, wov); - } + const wov = getWaveObjectValue(oref); return jotai.atom((get) => { const dataValue = get(wov.dataAtom); if (dataValue.loading) { @@ -248,12 +228,8 @@ function getWaveObjectLoadingAtom(oref: string): jotai.Atom { }); } -function useWaveObjectValue(oref: string): [T, boolean] { - let wov = waveObjectValueCache.get(oref); - if (wov == null) { - wov = createWaveValueObject(oref, true); - waveObjectValueCache.set(oref, wov); - } +function useWaveObjectValue(oref: string): [T, boolean] { + const wov = getWaveObjectValue(oref); React.useEffect(() => { wov.refCount++; return () => { @@ -264,36 +240,12 @@ function useWaveObjectValue(oref: string): [T, boolean] { return [atomVal.value, atomVal.loading]; } -function useWaveObject(oref: string): [T, boolean, (val: T) => void] { - let wov = waveObjectValueCache.get(oref); - if (wov == null) { - wov = createWaveValueObject(oref, true); - waveObjectValueCache.set(oref, wov); - } - React.useEffect(() => { - wov.refCount++; - return () => { - wov.refCount--; - }; - }, [oref]); - const [atomVal, setAtomVal] = jotai.useAtom(wov.dataAtom); - const simpleSet = (val: T) => { - setAtomVal({ value: val, loading: false }); - services.ObjectService.UpdateObject(val, false); - }; - return [atomVal.value, atomVal.loading, simpleSet]; -} - function updateWaveObject(update: WaveObjUpdate) { if (update == null) { return; } const oref = makeORef(update.otype, update.oid); - let wov = waveObjectValueCache.get(oref); - if (wov == null) { - wov = createWaveValueObject(oref, false); - waveObjectValueCache.set(oref, wov); - } + const wov = getWaveObjectValue(oref); if (update.updatetype == "delete") { console.log("WaveObj deleted", oref); globalStore.set(wov.dataAtom, { value: null, loading: false }); @@ -331,13 +283,8 @@ function cleanWaveObjectCache() { // gets the value of a WaveObject from the cache. // should provide getFn if it is available (e.g. inside of a jotai atom) // otherwise it will use the globalStore.get function -function getObjectValue(oref: string, getFn?: jotai.Getter): T { - let wov = waveObjectValueCache.get(oref); - if (wov == null) { - console.log("wov is null, creating new wov", oref); - wov = createWaveValueObject(oref, true); - waveObjectValueCache.set(oref, wov); - } +function getObjectValue(oref: string, getFn?: jotai.Getter): T { + const wov = getWaveObjectValue(oref); if (getFn == null) { getFn = globalStore.get; } @@ -350,7 +297,7 @@ function getObjectValue(oref: string, getFn?: jotai.Getter): T { // otherwise it will use the globalStore.set function function setObjectValue(value: T, setFn?: jotai.Setter, pushToServer?: boolean) { const oref = makeORef(value.otype, value.oid); - const wov = waveObjectValueCache.get(oref); + const wov = getWaveObjectValue(oref, false); if (wov === undefined) { return; } @@ -375,10 +322,7 @@ export { setObjectValue, updateWaveObject, updateWaveObjects, - useWaveObject, useWaveObjectValue, - useWaveObjectValueWithSuspense, - waveObjectValueCache, wshServerRpcHelper_call, wshServerRpcHelper_responsestream, };