diff --git a/packages/store/src/synced.ts b/packages/store/src/synced.ts index c27d6f1..23cd902 100644 --- a/packages/store/src/synced.ts +++ b/packages/store/src/synced.ts @@ -17,6 +17,8 @@ export interface SyncConfig { storage: StorageProvider } +export type Synced = Writable & {ready: Promise} + export const sync = async ({key, store, storage}: SyncConfig) => { const storedValue = await storage.get(key) @@ -35,10 +37,11 @@ export interface SyncedConfig { defaultValue: T } -export const synced = async ({key, storage, defaultValue}: SyncedConfig) => { - const store = writable(defaultValue) +export const synced = ({key, storage, defaultValue}: SyncedConfig) => { + const store = writable(defaultValue) as Synced - await sync({key, store, storage}) + const syncPromise = sync({key, store, storage}) + store.ready = syncPromise return store }