fix(storage): cache childrens proxies

This commit is contained in:
amsyarasyiq 2024-07-22 16:16:36 +08:00
parent d09ce3894b
commit b1cd040993
2 changed files with 13 additions and 6 deletions

View file

@ -67,7 +67,6 @@ export default function AddonPage<T extends object>({ card: CardComponent, ...pr
data={data}
estimatedItemSize={136}
ListHeaderComponent={headerElement}
onLoad={console.log}
contentContainerStyle={{ paddingBottom: 90, paddingHorizontal: 5 }}
renderItem={({ item }: any) => (
<View style={{ paddingVertical: 6, paddingHorizontal: 8 }}>

View file

@ -31,22 +31,30 @@ function createFileBackend<T>(filePath: string, dflt = {} as T): StorageBackend<
}
function _createProxy(target: any, path: string[], emitter: Emitter): any {
// cache the children proxies so proxy.child === proxy.child (yields the same value)
const objChildrens = new WeakMap<object, object>();
return new Proxy(target, {
get(target, prop: string) {
if ((prop as unknown) === emitterSymbol) return emitter;
const newPath = [...path, prop];
const value: any = target[prop];
let value: any = target[prop];
if (value && typeof value === "object") {
const origValue = value;
value = objChildrens.get(origValue);
if (!value) {
value = _createProxy(value, newPath, emitter);
objChildrens.set(origValue, value!);
}
}
if (value != null) {
emitter.emit("GET", {
path: newPath,
value,
});
if (typeof value === "object") {
return _createProxy(value, newPath, emitter);
}
return value;
}
return value;