From 3c94669d93ff2a31927da4b7ac8cd12405a19745 Mon Sep 17 00:00:00 2001 From: Mike Sawka Date: Fri, 18 Oct 2024 17:07:44 -0700 Subject: [PATCH] metakeyatom and overrideconfigatom (#1078) --- frontend/app/store/global.ts | 59 +++++++++++++++++++++++++++++++++++- 1 file changed, 58 insertions(+), 1 deletion(-) diff --git a/frontend/app/store/global.ts b/frontend/app/store/global.ts index 9be2262a4..54830fb20 100644 --- a/frontend/app/store/global.ts +++ b/frontend/app/store/global.ts @@ -219,8 +219,56 @@ function useBlockCache(blockId: string, name: string, makeFn: () => T): T { return value as T; } +function getBlockMetaKeyAtom(blockId: string, key: T): Atom { + const blockCache = getSingleBlockAtomCache(blockId); + const metaAtomName = "#meta-" + key; + let metaAtom = blockCache.get(metaAtomName); + if (metaAtom != null) { + return metaAtom; + } + metaAtom = atom((get) => { + let blockAtom = WOS.getWaveObjectAtom(WOS.makeORef("block", blockId)); + let blockData = get(blockAtom); + return blockData?.meta?.[key]; + }); + blockCache.set(metaAtomName, metaAtom); + return metaAtom; +} + +function useBlockMetaKeyAtom(blockId: string, key: T): MetaType[T] { + return useAtomValue(getBlockMetaKeyAtom(blockId, key)); +} + const settingsAtomCache = new Map>(); +function makeOverrideConfigAtom(blockId: string, key: T): Atom { + const blockCache = getSingleBlockAtomCache(blockId); + const overrideAtomName = "#settingsoverride-" + key; + let overrideAtom = blockCache.get(overrideAtomName); + if (overrideAtom != null) { + return overrideAtom; + } + overrideAtom = atom((get) => { + const blockMetaKeyAtom = getBlockMetaKeyAtom(blockId, key as any); + const metaKeyVal = get(blockMetaKeyAtom); + if (metaKeyVal != null) { + return metaKeyVal; + } + const settingsKeyAtom = getSettingsKeyAtom(key); + const settingsVal = get(settingsKeyAtom); + if (settingsVal != null) { + return settingsVal; + } + return null; + }); + blockCache.set(overrideAtomName, overrideAtom); + return overrideAtom; +} + +function useOverrideConfigAtom(blockId: string, key: T): SettingsType[T] { + return useAtomValue(makeOverrideConfigAtom(blockId, key)); +} + function getSettingsKeyAtom(key: T): Atom { let settingsKeyAtom = settingsAtomCache.get(key) as Atom; if (settingsKeyAtom == null) { @@ -254,12 +302,17 @@ function useSettingsPrefixAtom(prefix: string): Atom { const blockAtomCache = new Map>>(); -function useBlockAtom(blockId: string, name: string, makeFn: () => Atom): Atom { +function getSingleBlockAtomCache(blockId: string): Map> { let blockCache = blockAtomCache.get(blockId); if (blockCache == null) { blockCache = new Map>(); blockAtomCache.set(blockId, blockCache); } + return blockCache; +} + +function useBlockAtom(blockId: string, name: string, makeFn: () => Atom): Atom { + const blockCache = getSingleBlockAtomCache(blockId); let atom = blockCache.get(name); if (atom == null) { atom = makeFn(); @@ -527,6 +580,7 @@ export { fetchWaveFile, getApi, getBlockComponentModel, + getBlockMetaKeyAtom, getConnStatusAtom, getHostName, getObjectId, @@ -537,6 +591,7 @@ export { initGlobalWaveEventSubs, isDev, loadConnStatus, + makeOverrideConfigAtom, openLink, PLATFORM, pushFlashError, @@ -550,6 +605,8 @@ export { useBlockAtom, useBlockCache, useBlockDataLoaded, + useBlockMetaKeyAtom, + useOverrideConfigAtom, useSettingsPrefixAtom, WOS, };