[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";
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<EmitterListeners>((acc, val: string) => ((acc[val] = new Set<EmitterListener>()), acc), {}) as EmitterListeners,
return {
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);
},
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);
},
};
}

View file

@ -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<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(() => {
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<T>(storeName: string): Promise<Awaited<T>> {
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<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));
store.then((v) => {
awaited = v;
awaitQueue.forEach((cb) => cb());
awaited = v;
awaitQueue.forEach((cb) => cb());
});
return new Proxy({} as Awaited<T>, {
...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);
},
});
}