Skip to content
Source
Sign inSign up
TanStack/db
Open History to see the last commit

source-id-bindings.ts

96 lines3.2 KB

1import { BaseQueryBuilder, IR, isCollection } from '@tanstack/db'
2import type { DbClient } from '@tanstack/db'
3​
4const sourceObjectTokens = new WeakMap<object, number>()
5let nextSourceObjectToken = 0
6​
7export function getSourceObjectToken(source: object): number {
8 let id = sourceObjectTokens.get(source)
9 if (id === undefined) {
10 id = ++nextSourceObjectToken
11 sourceObjectTokens.set(source, id)
12 }
13 return id
15​
16export function getPreparedSources(
17 preparedValue: unknown,
18): Array<{ id: string }> {
19 if (isCollection(preparedValue)) return [preparedValue]
20 const query =
21 preparedValue instanceof BaseQueryBuilder
22 ? preparedValue
23 : preparedValue &&
24 typeof preparedValue === `object` &&
25 `query` in preparedValue &&
26 preparedValue.query instanceof BaseQueryBuilder
27 ? preparedValue.query
28 : undefined
29 return query
30 ? IR.collectCollectionSources(query._getQuery()).map(
31 ({ collection }) => collection,
32 )
33 : []
35​
36/** The source object each Collection ID named while one hook is mounted. */
37export type SourceIdBindings = {
38 unscoped: Map<string, number>
39 byClient: WeakMap<DbClient, Map<string, number>>
41​
42export function createSourceIdBindings(): SourceIdBindings {
43 return { unscoped: new Map(), byClient: new WeakMap() }
45​
46/**
47 * Derived query identity names each source by its Collection ID. While a hook
48 * is mounted, an ID must keep naming one source object in each client scope,
49 * or the hook could reuse a live query that reads the old source. Claims the
50 * prepared value's sources, or calls `release` and throws on a collision so a
51 * rejected render keeps no sync-start deferral.
52 */
53export function claimSourceIds(
54 bindings: SourceIdBindings,
55 preparedValue: unknown,
56 client: DbClient | undefined,
57 hookName: string,
58 release: () => void,
59): void {
60 const prior = client ? bindings.byClient.get(client) : bindings.unscoped
61 const seen = new Map<string, number>()
62 for (const source of getPreparedSources(preparedValue)) {
63 const token = getSourceObjectToken(source)
64 const previous = seen.get(source.id) ?? prior?.get(source.id)
65 if (previous !== undefined && previous !== token) {
66 release()
67 throw new Error(
68 `[${hookName}] Source Collection "${source.id}" was replaced by a different Collection with the same ID while this hook is mounted. Unmount the hook and clean up its previous source and client scope before reusing the ID.`,
69 )
70 }
71 seen.set(source.id, token)
72 }
73 const claimed = prior ?? new Map<string, number>()
74 for (const [id, token] of seen) claimed.set(id, token)
75 if (client) bindings.byClient.set(client, claimed)
77​
78/**
79 * Resumes every deferred sync start, then rethrows the first startup error, so
80 * one failing source cannot keep a later shared source deferred.
81 */
82export function resumeSyncStarts(
83 collections: Set<{ _resumeSyncStart: () => void }>,
84): void {
85 const pending = Array.from(collections)
86 collections.clear()
87 const errors: Array<unknown> = []
88 for (const collection of pending) {
89 try {
90 collection._resumeSyncStart()
91 } catch (error) {
92 errors.push(error)
93 }
94 }
95 if (errors.length) throw errors[0]