From 6762b807c8e31c76a3f6eeac0a6e2461cb0d5d74 Mon Sep 17 00:00:00 2001 From: SandroMaglione Date: Fri, 10 Jan 2025 10:36:46 +0100 Subject: [PATCH 1/2] live query return `Promise` with results --- packages/pglite-react/src/hooks.ts | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/packages/pglite-react/src/hooks.ts b/packages/pglite-react/src/hooks.ts index 73d9ced6..7e3b0380 100644 --- a/packages/pglite-react/src/hooks.ts +++ b/packages/pglite-react/src/hooks.ts @@ -18,7 +18,7 @@ function paramsEqual( } function useLiveQueryImpl( - query: string | LiveQuery | Promise>, + query: string | LiveQuery | Promise> | (() => Promise), params: unknown[] | undefined | null, key?: string, ): Omit, 'affectedRows'> | undefined { @@ -27,7 +27,11 @@ function useLiveQueryImpl( const liveQueryRef = useRef | undefined>() let liveQuery: LiveQuery | undefined let liveQueryChanged = false - if (!(typeof query === 'string') && !(query instanceof Promise)) { + if ( + !(typeof query === 'string') && + !(typeof query === 'function') && + !(query instanceof Promise) + ) { liveQuery = query liveQueryChanged = liveQueryRef.current !== liveQuery liveQueryRef.current = liveQuery @@ -59,6 +63,17 @@ function useLiveQueryImpl( cancelled = true ret.then(({ unsubscribe }) => unsubscribe()) } + } else if (typeof query === 'function') { + query.then((liveQuery) => { + if (cancelled) return + liveQueryRef.current = liveQuery + setResults(liveQuery.initialResults) + liveQuery.subscribe(cb) + }) + return () => { + cancelled = true + liveQueryRef.current?.unsubscribe(cb) + } } else if (query instanceof Promise) { query.then((liveQuery) => { if (cancelled) return @@ -112,7 +127,11 @@ export function useLiveQuery( ): LiveQueryResults | undefined export function useLiveQuery( - query: string | LiveQuery | Promise>, + liveQueryPromise: () => Promise, +): LiveQueryResults | undefined + +export function useLiveQuery( + query: string | LiveQuery | Promise> | (() => Promise), params?: unknown[] | null, ): LiveQueryResults | undefined { return useLiveQueryImpl(query, params) From 28ea4596b7d89f07f6ddd9caaabfb23971eb7c74 Mon Sep 17 00:00:00 2001 From: SandroMaglione Date: Thu, 16 Jan 2025 16:01:20 +0100 Subject: [PATCH 2/2] `() => Promise>` live query implementation --- packages/pglite-react/src/hooks.ts | 35 +++++++++++++++++++++++++----- 1 file changed, 29 insertions(+), 6 deletions(-) diff --git a/packages/pglite-react/src/hooks.ts b/packages/pglite-react/src/hooks.ts index 7e3b0380..92f29855 100644 --- a/packages/pglite-react/src/hooks.ts +++ b/packages/pglite-react/src/hooks.ts @@ -18,7 +18,11 @@ function paramsEqual( } function useLiveQueryImpl( - query: string | LiveQuery | Promise> | (() => Promise), + query: + | string + | LiveQuery + | Promise> + | (() => Promise>), params: unknown[] | undefined | null, key?: string, ): Omit, 'affectedRows'> | undefined { @@ -53,6 +57,7 @@ function useLiveQueryImpl( if (cancelled) return setResults(results) } + if (typeof query === 'string') { const ret = key !== undefined @@ -63,24 +68,37 @@ function useLiveQueryImpl( cancelled = true ret.then(({ unsubscribe }) => unsubscribe()) } - } else if (typeof query === 'function') { + } else if (query instanceof Promise) { query.then((liveQuery) => { if (cancelled) return liveQueryRef.current = liveQuery setResults(liveQuery.initialResults) liveQuery.subscribe(cb) }) + return () => { cancelled = true liveQueryRef.current?.unsubscribe(cb) } - } else if (query instanceof Promise) { - query.then((liveQuery) => { + } else if (typeof query === 'function') { + query().then((liveQueryOrString) => { if (cancelled) return + const liveQuery = + typeof liveQueryOrString === 'string' + ? key !== undefined + ? db.live.incrementalQuery( + liveQueryOrString, + currentParams, + key, + cb, + ) + : db.live.query(liveQueryOrString, currentParams, cb) + : liveQueryOrString liveQueryRef.current = liveQuery setResults(liveQuery.initialResults) liveQuery.subscribe(cb) }) + return () => { cancelled = true liveQueryRef.current?.unsubscribe(cb) @@ -88,6 +106,7 @@ function useLiveQueryImpl( } else if (liveQuery) { setResults(liveQuery.initialResults) liveQuery.subscribe(cb) + return () => { cancelled = true liveQuery.unsubscribe(cb) @@ -127,11 +146,15 @@ export function useLiveQuery( ): LiveQueryResults | undefined export function useLiveQuery( - liveQueryPromise: () => Promise, + liveQueryPromise: () => Promise>, ): LiveQueryResults | undefined export function useLiveQuery( - query: string | LiveQuery | Promise> | (() => Promise), + query: + | string + | LiveQuery + | Promise> + | (() => Promise>), params?: unknown[] | null, ): LiveQueryResults | undefined { return useLiveQueryImpl(query, params)