MCPcopy Create free account
hub / github.com/TanStack/db / constructor

Method constructor

packages/db/src/collection/subscription.ts:108–143  ·  view source on GitHub ↗
(
    private collection: CollectionImpl<any, any, any, any, any>,
    private callback: (changes: Array<ChangeMessage<any, any>>) => void,
    private options: CollectionSubscriptionOptions,
  )

Source from the content-addressed store, hash-verified

106 }
107
108 constructor(
109 private collection: CollectionImpl<any, any, any, any, any>,
110 private callback: (changes: Array<ChangeMessage<any, any>>) => void,
111 private options: CollectionSubscriptionOptions,
112 ) {
113 super()
114 if (options.onUnsubscribe) {
115 this.on(`unsubscribed`, (event) => options.onUnsubscribe!(event))
116 }
117
118 // Auto-index for where expressions if enabled
119 if (options.whereExpression) {
120 ensureIndexForExpression(options.whereExpression, this.collection)
121 }
122
123 const callbackWithSentKeysTracking = (
124 changes: Array<ChangeMessage<any, any>>,
125 ) => {
126 callback(changes)
127 this.trackSentKeys(changes)
128 }
129
130 this.callback = callbackWithSentKeysTracking
131
132 // Create a filtered callback if where clause is provided
133 this.filteredCallback = options.whereExpression
134 ? createFilteredCallback(this.callback, options)
135 : this.callback
136
137 // Listen for truncate events to re-request data after must-refetch
138 // When a truncate happens (e.g., from a 409 must-refetch), all collection data is cleared.
139 // We need to re-request all previously loaded subsets to repopulate the data.
140 this.truncateCleanup = this.collection.on(`truncate`, () => {
141 this.handleTruncate()
142 })
143 }
144
145 /**
146 * Handle collection truncate event by resetting state and re-requesting subsets.

Callers

nothing calls this directly

Calls 4

handleTruncateMethod · 0.95
ensureIndexForExpressionFunction · 0.85
createFilteredCallbackFunction · 0.85
onMethod · 0.45

Tested by

no test coverage detected