-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature: added
useSuspenseQuery
hook to react package (#353)
Co-authored-by: benitav <[email protected]>
- Loading branch information
1 parent
9f3cffd
commit 2b0466f
Showing
12 changed files
with
19,274 additions
and
23,330 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@powersync/react': minor | ||
--- | ||
|
||
Added `useSuspenseQuery` hook, allowing queries to suspend instead of returning `isLoading`/`isFetching` state. |
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import { AbstractPowerSyncDatabase } from '@powersync/common'; | ||
import { Query, WatchedQuery } from './WatchedQuery'; | ||
import { AdditionalOptions } from './hooks/useQuery'; | ||
|
||
export function generateQueryKey(sqlStatement: string, parameters: any[], options: AdditionalOptions): string { | ||
return `${sqlStatement} -- ${JSON.stringify(parameters)} -- ${JSON.stringify(options)}`; | ||
} | ||
|
||
export class QueryStore { | ||
cache = new Map<string, WatchedQuery>(); | ||
|
||
constructor(private db: AbstractPowerSyncDatabase) {} | ||
|
||
getQuery(key: string, query: Query<unknown>, options: AdditionalOptions) { | ||
if (this.cache.has(key)) { | ||
return this.cache.get(key); | ||
} | ||
|
||
const q = new WatchedQuery(this.db, query, options); | ||
const disposer = q.registerListener({ | ||
disposed: () => { | ||
this.cache.delete(key); | ||
disposer?.(); | ||
} | ||
}); | ||
|
||
this.cache.set(key, q); | ||
|
||
return q; | ||
} | ||
} | ||
|
||
let queryStores: WeakMap<AbstractPowerSyncDatabase, QueryStore> | undefined = undefined; | ||
|
||
export function getQueryStore(db: AbstractPowerSyncDatabase): QueryStore { | ||
queryStores ||= new WeakMap(); | ||
const existing = queryStores.get(db); | ||
if (existing) { | ||
return existing; | ||
} | ||
const store = new QueryStore(db); | ||
queryStores.set(db, store); | ||
return store; | ||
} |
Oops, something went wrong.