Skip to content

Instantly share code, notes, and snippets.

@dusty-phillips
Created December 9, 2023 18:30
Show Gist options
  • Save dusty-phillips/dde181bbbe887598e576abd32a429c62 to your computer and use it in GitHub Desktop.
Save dusty-phillips/dde181bbbe887598e576abd32a429c62 to your computer and use it in GitHub Desktop.
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