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

Function trailBaseCollectionOptions

packages/trailbase-db-collection/src/trailbase.ts:121–431  ·  view source on GitHub ↗
(
  config: TrailBaseCollectionConfig<TItem, TRecord, TKey>,
)

Source from the content-addressed store, hash-verified

119}
120
121export function trailBaseCollectionOptions<
122 TItem extends ShapeOf<TRecord>,
123 TRecord extends ShapeOf<TItem> = TItem,
124 TKey extends string | number = string | number,
125>(
126 config: TrailBaseCollectionConfig<TItem, TRecord, TKey>,
127): CollectionConfig<TItem, TKey, never, TrailBaseCollectionUtils> & {
128 utils: TrailBaseCollectionUtils
129} {
130 const getKey = config.getKey
131
132 const parse = (record: TRecord) =>
133 convert<TRecord, TItem>(config.parse, record)
134 const serialUpd = (item: Partial<TItem>) =>
135 convertPartial<TItem, TRecord>(config.serialize, item)
136 const serialIns = (item: TItem) =>
137 convert<TItem, TRecord>(config.serialize, item)
138
139 const seenIds = new Store(new Map<string, number>())
140
141 const internalSyncMode = config.syncMode ?? `eager`
142 let fullSyncCompleted = false
143
144 const awaitIds = (
145 ids: Array<string>,
146 timeout: number = 120 * 1000,
147 ): Promise<void> => {
148 const completed = (value: Map<string, number>) =>
149 ids.every((id) => value.has(id))
150 if (completed(seenIds.state)) {
151 return Promise.resolve()
152 }
153
154 return new Promise<void>((resolve, reject) => {
155 const timeoutId = setTimeout(() => {
156 sub.unsubscribe()
157 reject(new TimeoutWaitingForIdsError(ids.toString()))
158 }, timeout)
159
160 const sub = seenIds.subscribe((value) => {
161 if (completed(value)) {
162 clearTimeout(timeoutId)
163 sub.unsubscribe()
164 resolve()
165 }
166 })
167 })
168 }
169
170 let eventReader: ReadableStreamDefaultReader<Event> | undefined
171 const cancelEventReader = () => {
172 if (eventReader) {
173 eventReader.cancel()
174 eventReader.releaseLock()
175 eventReader = undefined
176 }
177 }
178

Callers 5

setUpFunction · 0.90
collections.tsFile · 0.90
collections.tsFile · 0.90

Calls 7

serialInsFunction · 0.85
awaitIdsFunction · 0.85
serialUpdFunction · 0.85
startFunction · 0.70
mapMethod · 0.45
updateMethod · 0.45
deleteMethod · 0.45

Tested by 2

setUpFunction · 0.72