mirror of
https://github.com/wavetermdev/waveterm.git
synced 2024-12-21 16:38:23 +01:00
metakeyatom and overrideconfigatom (#1078)
This commit is contained in:
parent
7385906106
commit
3c94669d93
@ -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,
|
||||||
};
|
};
|
||||||
|
Loading…
Reference in New Issue
Block a user