From 76ea19f8efc19c509abd41b6250ff87c90516a02 Mon Sep 17 00:00:00 2001 From: Beef Date: Sat, 4 Feb 2023 02:28:08 +0000 Subject: [PATCH] [Storage] Run prettier --- src/lib/emitter.ts | 46 +++++++-------- src/lib/storage.ts | 143 +++++++++++++++++++++++---------------------- 2 files changed, 94 insertions(+), 95 deletions(-) diff --git a/src/lib/emitter.ts b/src/lib/emitter.ts index d7bc1cf..9128305 100644 --- a/src/lib/emitter.ts +++ b/src/lib/emitter.ts @@ -1,35 +1,33 @@ import { Emitter, EmitterEvent, EmitterListener, EmitterListenerData, EmitterListeners } from "@types"; export const Events = Object.freeze({ - GET: "GET", - SET: "SET", - DEL: "DEL" + GET: "GET", + SET: "SET", + DEL: "DEL", }); export default function createEmitter(): Emitter { - return { - listeners: Object.values(Events).reduce((acc, val: string) => ((acc[val] = new Set()), acc), {}) as EmitterListeners, + return { + listeners: Object.values(Events).reduce((acc, val: string) => ((acc[val] = new Set()), acc), {}) as EmitterListeners, - on(event: EmitterEvent, listener: EmitterListener) { - if (!this.listeners[event].has(listener)) - this.listeners[event].add(listener); - }, + on(event: EmitterEvent, listener: EmitterListener) { + if (!this.listeners[event].has(listener)) this.listeners[event].add(listener); + }, - off(event: EmitterEvent, listener: EmitterListener) { - this.listeners[event].delete(listener); - }, + off(event: EmitterEvent, listener: EmitterListener) { + this.listeners[event].delete(listener); + }, - once(event: EmitterEvent, listener: EmitterListener) { - const once = (event: EmitterEvent, data: EmitterListenerData) => { - this.off(event, once); - listener(event, data); - } - this.on(event, once); - }, + once(event: EmitterEvent, listener: EmitterListener) { + 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); - }, - }; + emit(event: EmitterEvent, data: EmitterListenerData) { + for (const listener of this.listeners[event]) listener(event, data); + }, + }; } diff --git a/src/lib/storage.ts b/src/lib/storage.ts index f4d6572..1c48e8a 100644 --- a/src/lib/storage.ts +++ b/src/lib/storage.ts @@ -7,87 +7,87 @@ 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 } { - const emitter = createEmitter(); +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; + function createProxy(target: any, path: string[]): any { + return new Proxy(target, { + get(target, prop: string) { + if ((prop as unknown) === emitterSymbol) return emitter; - const newPath = [...path, prop]; - const value: any = target[prop]; + const newPath = [...path, prop]; + const value: any = target[prop]; - if (value !== undefined && value !== null) { - emitter.emit("GET", { - path: newPath, - value, - }); - if (typeof value === "object") { - return createProxy(value, newPath); - } - return value; - } + if (value !== undefined && value !== null) { + emitter.emit("GET", { + path: newPath, + value, + }); + if (typeof value === "object") { + return createProxy(value, newPath); + } + return value; + } - return value; - }, + return value; + }, - set(target, prop: string, value) { - target[prop] = value; - emitter.emit("SET", { - path: [...path, prop], - value + set(target, prop: string, value) { + target[prop] = value; + emitter.emit("SET", { + path: [...path, prop], + 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) { - const success = delete target[prop]; - if (success) emitter.emit("DEL", { - path: [...path, prop], - }); - return success; - }, - }); - } - - return { - proxy: createProxy(target, []), - emitter, - } + return { + proxy: createProxy(target, []), + emitter, + }; } export function useProxy(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(() => { - const listener = () => forceUpdate(); + React.useEffect(() => { + const listener = () => forceUpdate(); - emitter.on("SET", listener); - emitter.on("DEL", listener); + emitter.on("SET", listener); + emitter.on("DEL", listener); - return () => { - emitter.off("SET", listener); - emitter.off("DEL", listener); - } - }, []); + return () => { + emitter.off("SET", listener); + emitter.off("DEL", listener); + }; + }, []); - return storage; + return storage; } export async function createStorage(storeName: string): Promise> { - const data = JSON.parse(await MMKVManager.getItem(storeName) ?? "{}"); - const { proxy, emitter } = createProxy(data); + const data = JSON.parse((await MMKVManager.getItem(storeName)) ?? "{}"); + const { proxy, emitter } = createProxy(data); - const handler = () => MMKVManager.setItem(storeName, JSON.stringify(proxy)); - emitter.on("SET", handler); - emitter.on("DEL", handler); + const handler = () => MMKVManager.setItem(storeName, JSON.stringify(proxy)); + emitter.on("SET", handler); + emitter.on("DEL", handler); - return proxy; + return proxy; } export function wrapSync>(store: T): Awaited { @@ -97,19 +97,20 @@ export function wrapSync>(store: T): Awaited { const awaitInit = (cb: () => void) => (awaited ? cb() : awaitQueue.push(cb)); store.then((v) => { - awaited = v; - awaitQueue.forEach((cb) => cb()); + awaited = v; + awaitQueue.forEach((cb) => cb()); }); return new Proxy({} as Awaited, { - ...Object.fromEntries(Object.getOwnPropertyNames(Reflect) - // @ts-expect-error - .map((k) => [k, (t: T, ...a: any[]) => Reflect[k](awaited ?? t, ...a)])), - get(target, prop, recv) { - if (prop === syncAwaitSymbol) - return awaitInit; - return Reflect.get(awaited ?? target, prop, recv); - } + ...Object.fromEntries( + Object.getOwnPropertyNames(Reflect) + // @ts-expect-error + .map((k) => [k, (t: T, ...a: any[]) => Reflect[k](awaited ?? t, ...a)]) + ), + get(target, prop, recv) { + if (prop === syncAwaitSymbol) return awaitInit; + return Reflect.get(awaited ?? target, prop, recv); + }, }); }