diff --git a/src/lib/storage.ts b/src/lib/storage.ts index 7af9547..f4d6572 100644 --- a/src/lib/storage.ts +++ b/src/lib/storage.ts @@ -5,6 +5,7 @@ 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(); @@ -91,11 +92,25 @@ export async function createStorage(storeName: string): Promise> { export function wrapSync>(store: T): Awaited { let awaited: any = undefined; - store.then((v) => (awaited = v)); - return new Proxy( - {} as Awaited, - Object.fromEntries(Object.getOwnPropertyNames(Reflect) + + 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, { + ...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((res) => store[syncAwaitSymbol](res));