Created
December 9, 2023 18:30
-
-
Save dusty-phillips/dde181bbbe887598e576abd32a429c62 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export function createDexieStore<T extends StoreNode>( | |
querier: () => T | undefined | Promise<T | undefined>, | |
): WithLoadingState<T> { | |
const [store, setStore] = createStore<WithLoadingState<T>>({ | |
__loading: true, | |
}); | |
const observable = liveQuery(querier); | |
const signal = from<T | undefined>(observable); | |
createEffect(() => { | |
const data = signal(); | |
if (data) { | |
setStore(reconcile({ ...data, __loading: false }, { merge: true })); | |
} | |
}); | |
return store; | |
} | |
export function createDexieResource<T extends StoreNode>( | |
querier: () => T | undefined | Promise<T | undefined>, | |
): ResourceReturn<T> { | |
const store = createDexieStore(querier); | |
let loaded = false; | |
return createResource(store, (theStore) => { | |
if (theStore.__loading) { | |
return new Promise((resolve) => { | |
createEffect(() => { | |
if (!store.__loading && !loaded) { | |
loaded = true; | |
resolve(store as T); | |
} | |
}); | |
}); | |
} | |
return Promise.resolve(store as T); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment