[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

@ -3,7 +3,7 @@ import { Emitter, EmitterEvent, EmitterListener, EmitterListenerData, EmitterLis
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 {
@ -11,8 +11,7 @@ export default function createEmitter(): Emitter {
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) {
@ -23,13 +22,12 @@ export default function createEmitter(): Emitter {
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,14 +7,13 @@ 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];
@ -37,7 +36,7 @@ export function createProxy(target: any = {}): { proxy: any, emitter: Emitter }
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 // we do not care about success, if this actually does fail we have other problems
return true; return true;
@ -45,7 +44,8 @@ export function createProxy(target: any = {}): { proxy: any, emitter: Emitter }
deleteProperty(target, prop: string) { deleteProperty(target, prop: string) {
const success = delete target[prop]; const success = delete target[prop];
if (success) emitter.emit("DEL", { if (success)
emitter.emit("DEL", {
path: [...path, prop], path: [...path, prop],
}); });
return success; return success;
@ -56,7 +56,7 @@ export function createProxy(target: any = {}): { proxy: any, emitter: Emitter }
return { return {
proxy: createProxy(target, []), proxy: createProxy(target, []),
emitter, emitter,
} };
} }
export function useProxy<T>(storage: T): T { export function useProxy<T>(storage: T): T {
@ -73,14 +73,14 @@ export function useProxy<T>(storage: T): T {
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));
@ -102,14 +102,15 @@ export function wrapSync<T extends Promise<any>>(store: T): Awaited<T> {
}); });
return new Proxy({} as Awaited<T>, { return new Proxy({} as Awaited<T>, {
...Object.fromEntries(Object.getOwnPropertyNames(Reflect) ...Object.fromEntries(
Object.getOwnPropertyNames(Reflect)
// @ts-expect-error // @ts-expect-error
.map((k) => [k, (t: T, ...a: any[]) => Reflect[k](awaited ?? t, ...a)])), .map((k) => [k, (t: T, ...a: any[]) => Reflect[k](awaited ?? t, ...a)])
),
get(target, prop, recv) { get(target, prop, recv) {
if (prop === syncAwaitSymbol) if (prop === syncAwaitSymbol) return awaitInit;
return awaitInit;
return Reflect.get(awaited ?? target, prop, recv); return Reflect.get(awaited ?? target, prop, recv);
} },
}); });
} }