diff --git a/frontend/app/store/global.ts b/frontend/app/store/global.ts index f72ffc53a..515b66da2 100644 --- a/frontend/app/store/global.ts +++ b/frontend/app/store/global.ts @@ -90,6 +90,22 @@ Events.On("block:ptydata", (event: any) => { subject.next(data); }); +const blockCache = new Map>(); + +function useBlockCache(blockId: string, name: string, makeFn: () => T): T { + let blockMap = blockCache.get(blockId); + if (blockMap == null) { + blockMap = new Map(); + blockCache.set(blockId, blockMap); + } + let value = blockMap.get(name); + if (value == null) { + value = makeFn(); + blockMap.set(name, value); + } + return value as T; +} + const blockAtomCache = new Map>>(); function useBlockAtom(blockId: string, name: string, makeFn: () => jotai.Atom): jotai.Atom { @@ -107,4 +123,4 @@ function useBlockAtom(blockId: string, name: string, makeFn: () => jotai.Atom return atom as jotai.Atom; } -export { WOS, atoms, getBlockSubject, globalStore, useBlockAtom }; +export { WOS, atoms, getBlockSubject, globalStore, useBlockAtom, useBlockCache };