offline-first, p2p synced, atproto enabled, feed reader
at main 42 lines 1.1 kB view raw
1// pulled mainly from 2// https://github.com/faassen/solid-dexie/blob/main/src/solid-dexie.ts 3import {liveQuery} from 'dexie' 4import {Accessor, createEffect, createSignal, on, onCleanup} from 'solid-js' 5import {type ReconcileOptions, createStore, reconcile} from 'solid-js/store' 6 7import {NotArray} from '#lib/types.js' 8 9export function makeSignalQuery<T>( 10 query: () => NotArray<T> | Promise<NotArray<T>>, 11 options?: ReconcileOptions, 12): Accessor<T | undefined> { 13 const [value, setValue] = createSignal<T>() 14 15 createEffect( 16 on(query, () => { 17 const unsub = liveQuery(query).subscribe((v) => setValue(reconcile(v, options))) 18 onCleanup(() => { 19 unsub.unsubscribe() 20 }) 21 }), 22 ) 23 24 return value 25} 26 27export function makeStoreQuery<T>(query: () => T[] | Promise<T[]>, options?: ReconcileOptions): T[] { 28 const [store, setStore] = createStore<T[]>([]) 29 30 createEffect( 31 on(query, () => { 32 const unsub = liveQuery(query).subscribe((v) => { 33 setStore(reconcile(v, options)) 34 }) 35 onCleanup(() => { 36 unsub.unsubscribe() 37 }) 38 }), 39 ) 40 41 return store 42}