[Storage] Run prettier

This commit is contained in:
Beef 2023-02-04 02:28:08 +00:00
parent 3bf63a7b5b
commit 76ea19f8ef
2 changed files with 94 additions and 95 deletions

View file

@ -1,35 +1,33 @@
import { Emitter, EmitterEvent, EmitterListener, EmitterListenerData, EmitterListeners } from "@types"; import { Emitter, EmitterEvent, EmitterListener, EmitterListenerData, EmitterListeners } from "@types";
export const Events = Object.freeze({ export const Events = Object.freeze({
GET: "GET", GET: "GET",
SET: "SET", SET: "SET",
DEL: "DEL" DEL: "DEL",
}); });
export default function createEmitter(): Emitter { export default function createEmitter(): Emitter {
return { return {
listeners: Object.values(Events).reduce<EmitterListeners>((acc, val: string) => ((acc[val] = new Set<EmitterListener>()), acc), {}) as EmitterListeners, listeners: Object.values(Events).reduce<EmitterListeners>((acc, val: string) => ((acc[val] = new Set<EmitterListener>()), acc), {}) as EmitterListeners,
on(event: EmitterEvent, listener: EmitterListener) { on(event: EmitterEvent, listener: EmitterListener) {
if (!this.listeners[event].has(listener)) if (!this.listeners[event].has(listener)) this.listeners[event].add(listener);
this.listeners[event].add(listener); },
},
off(event: EmitterEvent, listener: EmitterListener) { off(event: EmitterEvent, listener: EmitterListener) {
this.listeners[event].delete(listener); this.listeners[event].delete(listener);
}, },
once(event: EmitterEvent, listener: EmitterListener) { once(event: EmitterEvent, listener: EmitterListener) {
const once = (event: EmitterEvent, data: EmitterListenerData) => { const once = (event: EmitterEvent, data: EmitterListenerData) => {
this.off(event, once); this.off(event, once);
listener(event, data); listener(event, data);
} };
this.on(event, once); this.on(event, once);
}, },
emit(event: EmitterEvent, data: EmitterListenerData) { emit(event: EmitterEvent, data: EmitterListenerData) {
for (const listener of this.listeners[event]) for (const listener of this.listeners[event]) listener(event, data);
listener(event, data); },
}, };
};
} }

View file

@ -7,87 +7,87 @@ const MMKVManager = RN.NativeModules.MMKVManager as MMKVManager;
const emitterSymbol = Symbol("emitter accessor"); const emitterSymbol = Symbol("emitter accessor");
const syncAwaitSymbol = Symbol("wrapSync promise accessor"); const syncAwaitSymbol = Symbol("wrapSync promise accessor");
export function createProxy(target: any = {}): { proxy: any, emitter: Emitter } { export function createProxy(target: any = {}): { proxy: any; emitter: Emitter } {
const emitter = createEmitter(); const emitter = createEmitter();
function createProxy(target: any, path: string[]): any { function createProxy(target: any, path: string[]): any {
return new Proxy(target, { return new Proxy(target, {
get(target, prop: string) { get(target, prop: string) {
if ((prop as unknown) === emitterSymbol) if ((prop as unknown) === emitterSymbol) return emitter;
return emitter;
const newPath = [...path, prop]; const newPath = [...path, prop];
const value: any = target[prop]; const value: any = target[prop];
if (value !== undefined && value !== null) { if (value !== undefined && value !== null) {
emitter.emit("GET", { emitter.emit("GET", {
path: newPath, path: newPath,
value, value,
}); });
if (typeof value === "object") { if (typeof value === "object") {
return createProxy(value, newPath); return createProxy(value, newPath);
} }
return value; return value;
} }
return value; return value;
}, },
set(target, prop: string, value) { set(target, prop: string, value) {
target[prop] = value; target[prop] = value;
emitter.emit("SET", { emitter.emit("SET", {
path: [...path, prop], path: [...path, prop],
value value,
});
// we do not care about success, if this actually does fail we have other problems
return true;
},
deleteProperty(target, prop: string) {
const success = delete target[prop];
if (success)
emitter.emit("DEL", {
path: [...path, prop],
});
return success;
},
}); });
// we do not care about success, if this actually does fail we have other problems }
return true;
},
deleteProperty(target, prop: string) { return {
const success = delete target[prop]; proxy: createProxy(target, []),
if (success) emitter.emit("DEL", { emitter,
path: [...path, prop], };
});
return success;
},
});
}
return {
proxy: createProxy(target, []),
emitter,
}
} }
export function useProxy<T>(storage: T): T { export function useProxy<T>(storage: T): T {
const emitter = (storage as any)[emitterSymbol] as Emitter; const emitter = (storage as any)[emitterSymbol] as Emitter;
const [, forceUpdate] = React.useReducer((n) => ~n, 0); const [, forceUpdate] = React.useReducer((n) => ~n, 0);
React.useEffect(() => { React.useEffect(() => {
const listener = () => forceUpdate(); const listener = () => forceUpdate();
emitter.on("SET", listener); emitter.on("SET", listener);
emitter.on("DEL", listener); emitter.on("DEL", listener);
return () => { return () => {
emitter.off("SET", listener); emitter.off("SET", listener);
emitter.off("DEL", listener); emitter.off("DEL", listener);
} };
}, []); }, []);
return storage; return storage;
} }
export async function createStorage<T>(storeName: string): Promise<Awaited<T>> { export async function createStorage<T>(storeName: string): Promise<Awaited<T>> {
const data = JSON.parse(await MMKVManager.getItem(storeName) ?? "{}"); const data = JSON.parse((await MMKVManager.getItem(storeName)) ?? "{}");
const { proxy, emitter } = createProxy(data); const { proxy, emitter } = createProxy(data);
const handler = () => MMKVManager.setItem(storeName, JSON.stringify(proxy)); const handler = () => MMKVManager.setItem(storeName, JSON.stringify(proxy));
emitter.on("SET", handler); emitter.on("SET", handler);
emitter.on("DEL", handler); emitter.on("DEL", handler);
return proxy; return proxy;
} }
export function wrapSync<T extends Promise<any>>(store: T): Awaited<T> { export function wrapSync<T extends Promise<any>>(store: T): Awaited<T> {
@ -97,19 +97,20 @@ export function wrapSync<T extends Promise<any>>(store: T): Awaited<T> {
const awaitInit = (cb: () => void) => (awaited ? cb() : awaitQueue.push(cb)); const awaitInit = (cb: () => void) => (awaited ? cb() : awaitQueue.push(cb));
store.then((v) => { store.then((v) => {
awaited = v; awaited = v;
awaitQueue.forEach((cb) => cb()); awaitQueue.forEach((cb) => cb());
}); });
return new Proxy({} as Awaited<T>, { return new Proxy({} as Awaited<T>, {
...Object.fromEntries(Object.getOwnPropertyNames(Reflect) ...Object.fromEntries(
// @ts-expect-error Object.getOwnPropertyNames(Reflect)
.map((k) => [k, (t: T, ...a: any[]) => Reflect[k](awaited ?? t, ...a)])), // @ts-expect-error
get(target, prop, recv) { .map((k) => [k, (t: T, ...a: any[]) => Reflect[k](awaited ?? t, ...a)])
if (prop === syncAwaitSymbol) ),
return awaitInit; get(target, prop, recv) {
return Reflect.get(awaited ?? target, prop, recv); if (prop === syncAwaitSymbol) return awaitInit;
} return Reflect.get(awaited ?? target, prop, recv);
},
}); });
} }