[Global] Loader config and identity (#17)
* [TS] Add definition for DCDFileManager * [Storage] Introduce backends * [Settings] Add loader config * [TS] Update storage definitions * [TS] Update loader config and identity types * [Loader] Expose loader config and identity * [UI] Actually update UI for the new loader config fields
This commit is contained in:
parent
cfccd5f5b2
commit
1840577fb5
7 changed files with 154 additions and 19 deletions
24
src/lib/storage/backends.ts
Normal file
24
src/lib/storage/backends.ts
Normal file
|
@ -0,0 +1,24 @@
|
|||
import { DCDFileManager, MMKVManager, StorageBackend } from "@types";
|
||||
import { ReactNative as RN } from "@metro/hoist";
|
||||
|
||||
const MMKVManager = RN.NativeModules.MMKVManager as MMKVManager;
|
||||
const DCDFileManager = RN.NativeModules.DCDFileManager as DCDFileManager;
|
||||
|
||||
const filePathFixer: (file: string) => string = RN.Platform.select({
|
||||
default: (f) => f,
|
||||
ios: (f) => `Documents/${f}`,
|
||||
});
|
||||
|
||||
export const createMMKVBackend = (store: string): StorageBackend => ({
|
||||
get: async function() {
|
||||
return JSON.parse((await MMKVManager.getItem(store)) ?? "{}");
|
||||
},
|
||||
set: (data) => MMKVManager.setItem(store, JSON.stringify(data)),
|
||||
});
|
||||
|
||||
export const createFileBackend = (file: string): StorageBackend => ({
|
||||
get: async function() {
|
||||
return JSON.parse((await DCDFileManager.readFile(`${DCDFileManager.getConstants().DocumentsDirPath}/${file}`, "utf8")) ?? "{}");
|
||||
},
|
||||
set: (data) => void DCDFileManager.writeFile("documents", filePathFixer(file), JSON.stringify(data), "utf8"),
|
||||
});
|
119
src/lib/storage/index.ts
Normal file
119
src/lib/storage/index.ts
Normal file
|
@ -0,0 +1,119 @@
|
|||
import { Emitter, MMKVManager, StorageBackend } from "@types";
|
||||
import { ReactNative as RN } from "@metro/hoist";
|
||||
import createEmitter from "../emitter";
|
||||
|
||||
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();
|
||||
|
||||
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];
|
||||
|
||||
if (value !== undefined && value !== null) {
|
||||
emitter.emit("GET", {
|
||||
path: newPath,
|
||||
value,
|
||||
});
|
||||
if (typeof value === "object") {
|
||||
return createProxy(value, newPath);
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
return 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;
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
return {
|
||||
proxy: createProxy(target, []),
|
||||
emitter,
|
||||
};
|
||||
}
|
||||
|
||||
export function useProxy<T>(storage: T): T {
|
||||
const emitter = (storage as any)[emitterSymbol] as Emitter;
|
||||
|
||||
const [, forceUpdate] = React.useReducer((n) => ~n, 0);
|
||||
|
||||
React.useEffect(() => {
|
||||
const listener = () => forceUpdate();
|
||||
|
||||
emitter.on("SET", listener);
|
||||
emitter.on("DEL", listener);
|
||||
|
||||
return () => {
|
||||
emitter.off("SET", listener);
|
||||
emitter.off("DEL", listener);
|
||||
};
|
||||
}, []);
|
||||
|
||||
return storage;
|
||||
}
|
||||
|
||||
export async function createStorage<T>(backend: StorageBackend): Promise<Awaited<T>> {
|
||||
const data = await backend.get();
|
||||
const { proxy, emitter } = createProxy(data);
|
||||
|
||||
const handler = () => backend.set(proxy);
|
||||
emitter.on("SET", handler);
|
||||
emitter.on("DEL", handler);
|
||||
|
||||
return proxy;
|
||||
}
|
||||
|
||||
export function wrapSync<T extends Promise<any>>(store: T): Awaited<T> {
|
||||
let awaited: any = undefined;
|
||||
|
||||
const awaitQueue: (() => void)[] = [];
|
||||
const awaitInit = (cb: () => void) => (awaited ? cb() : awaitQueue.push(cb));
|
||||
|
||||
store.then((v) => {
|
||||
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);
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
export const awaitSyncWrapper = (store: any) => new Promise<void>((res) => store[syncAwaitSymbol](res));
|
||||
|
||||
export * from "./backends";
|
Loading…
Add table
Add a link
Reference in a new issue