metakeyatom and overrideconfigatom (#1078)

This commit is contained in:
Mike Sawka 2024-10-18 17:07:44 -07:00 committed by GitHub
parent 7385906106
commit 3c94669d93
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -219,8 +219,56 @@ function useBlockCache<T>(blockId: string, name: string, makeFn: () => T): T {
return value as T; return value as T;
} }
function getBlockMetaKeyAtom<T extends keyof MetaType>(blockId: string, key: T): Atom<MetaType[T]> {
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<T extends keyof MetaType>(blockId: string, key: T): MetaType[T] {
return useAtomValue(getBlockMetaKeyAtom(blockId, key));
}
const settingsAtomCache = new Map<string, Atom<any>>(); const settingsAtomCache = new Map<string, Atom<any>>();
function makeOverrideConfigAtom<T extends keyof SettingsType>(blockId: string, key: T): Atom<SettingsType[T]> {
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<T extends keyof SettingsType>(blockId: string, key: T): SettingsType[T] {
return useAtomValue(makeOverrideConfigAtom(blockId, key));
}
function getSettingsKeyAtom<T extends keyof SettingsType>(key: T): Atom<SettingsType[T]> { function getSettingsKeyAtom<T extends keyof SettingsType>(key: T): Atom<SettingsType[T]> {
let settingsKeyAtom = settingsAtomCache.get(key) as Atom<SettingsType[T]>; let settingsKeyAtom = settingsAtomCache.get(key) as Atom<SettingsType[T]>;
if (settingsKeyAtom == null) { if (settingsKeyAtom == null) {
@ -254,12 +302,17 @@ function useSettingsPrefixAtom(prefix: string): Atom<SettingsType> {
const blockAtomCache = new Map<string, Map<string, Atom<any>>>(); const blockAtomCache = new Map<string, Map<string, Atom<any>>>();
function useBlockAtom<T>(blockId: string, name: string, makeFn: () => Atom<T>): Atom<T> { function getSingleBlockAtomCache(blockId: string): Map<string, Atom<any>> {
let blockCache = blockAtomCache.get(blockId); let blockCache = blockAtomCache.get(blockId);
if (blockCache == null) { if (blockCache == null) {
blockCache = new Map<string, Atom<any>>(); blockCache = new Map<string, Atom<any>>();
blockAtomCache.set(blockId, blockCache); blockAtomCache.set(blockId, blockCache);
} }
return blockCache;
}
function useBlockAtom<T>(blockId: string, name: string, makeFn: () => Atom<T>): Atom<T> {
const blockCache = getSingleBlockAtomCache(blockId);
let atom = blockCache.get(name); let atom = blockCache.get(name);
if (atom == null) { if (atom == null) {
atom = makeFn(); atom = makeFn();
@ -527,6 +580,7 @@ export {
fetchWaveFile, fetchWaveFile,
getApi, getApi,
getBlockComponentModel, getBlockComponentModel,
getBlockMetaKeyAtom,
getConnStatusAtom, getConnStatusAtom,
getHostName, getHostName,
getObjectId, getObjectId,
@ -537,6 +591,7 @@ export {
initGlobalWaveEventSubs, initGlobalWaveEventSubs,
isDev, isDev,
loadConnStatus, loadConnStatus,
makeOverrideConfigAtom,
openLink, openLink,
PLATFORM, PLATFORM,
pushFlashError, pushFlashError,
@ -550,6 +605,8 @@ export {
useBlockAtom, useBlockAtom,
useBlockCache, useBlockCache,
useBlockDataLoaded, useBlockDataLoaded,
useBlockMetaKeyAtom,
useOverrideConfigAtom,
useSettingsPrefixAtom, useSettingsPrefixAtom,
WOS, WOS,
}; };