[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({
GET: "GET",
SET: "SET",
DEL: "DEL"
DEL: "DEL",
});
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,
on(event: EmitterEvent, listener: EmitterListener) {
if (!this.listeners[event].has(listener))
this.listeners[event].add(listener);
if (!this.listeners[event].has(listener)) this.listeners[event].add(listener);
},
off(event: EmitterEvent, listener: EmitterListener) {
@ -23,13 +22,12 @@ export default function createEmitter(): Emitter {
const once = (event: EmitterEvent, data: EmitterListenerData) => {
this.off(event, once);
listener(event, data);
}
};
this.on(event, once);
},
emit(event: EmitterEvent, data: EmitterListenerData) {
for (const listener of this.listeners[event])
listener(event, data);
for (const listener of this.listeners[event]) listener(event, data);
},
};
}

View file

@ -7,14 +7,13 @@ const MMKVManager = RN.NativeModules.MMKVManager as MMKVManager;
const emitterSymbol = Symbol("emitter 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();
function createProxy(target: any, path: string[]): any {
return new Proxy(target, {
get(target, prop: string) {
if ((prop as unknown) === emitterSymbol)
return emitter;
if ((prop as unknown) === emitterSymbol) return emitter;
const newPath = [...path, prop];
const value: any = target[prop];
@ -37,7 +36,7 @@ export function createProxy(target: any = {}): { proxy: any, emitter: Emitter }
target[prop] = value;
emitter.emit("SET", {
path: [...path, prop],
value
value,
});
// we do not care about success, if this actually does fail we have other problems
return true;
@ -45,7 +44,8 @@ export function createProxy(target: any = {}): { proxy: any, emitter: Emitter }
deleteProperty(target, prop: string) {
const success = delete target[prop];
if (success) emitter.emit("DEL", {
if (success)
emitter.emit("DEL", {
path: [...path, prop],
});
return success;
@ -56,7 +56,7 @@ export function createProxy(target: any = {}): { proxy: any, emitter: Emitter }
return {
proxy: createProxy(target, []),
emitter,
}
};
}
export function useProxy<T>(storage: T): T {
@ -73,14 +73,14 @@ export function useProxy<T>(storage: T): T {
return () => {
emitter.off("SET", listener);
emitter.off("DEL", listener);
}
};
}, []);
return storage;
}
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 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>, {
...Object.fromEntries(Object.getOwnPropertyNames(Reflect)
...Object.fromEntries(
Object.getOwnPropertyNames(Reflect)
// @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) {
if (prop === syncAwaitSymbol)
return awaitInit;
if (prop === syncAwaitSymbol) return awaitInit;
return Reflect.get(awaited ?? target, prop, recv);
}
},
});
}