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

Function setupNestedPipelines

packages/db/src/query/live/collection-config-builder.ts:1284–1352  ·  view source on GitHub ↗

* Sets up shared buffers for nested includes pipelines. * Instead of writing directly into a single shared IncludesOutputState, * each nested pipeline writes into a buffer that is later drained per-entry.

(
  includes: Array<IncludesCompilationResult>,
  syncState: SyncState,
)

Source from the content-addressed store, hash-verified

1282 * each nested pipeline writes into a buffer that is later drained per-entry.
1283 */
1284function setupNestedPipelines(
1285 includes: Array<IncludesCompilationResult>,
1286 syncState: SyncState,
1287): Array<NestedIncludesSetup> {
1288 return includes.map((entry) => {
1289 const buffer: Map<unknown, Map<unknown, Changes<any>>> = new Map()
1290
1291 // Attach output callback that writes into the shared buffer
1292 entry.pipeline.pipe(
1293 output((data) => {
1294 const messages = data.getInner()
1295 syncState.messagesCount += messages.length
1296
1297 for (const [[childKey, tupleData], multiplicity] of messages) {
1298 const [childResult, _orderByIndex, correlationKey, parentContext] =
1299 tupleData as unknown as [
1300 any,
1301 string | undefined,
1302 unknown,
1303 Record<string, any> | null,
1304 ]
1305
1306 const routingKey = computeRoutingKey(correlationKey, parentContext)
1307
1308 let byChild = buffer.get(routingKey)
1309 if (!byChild) {
1310 byChild = new Map()
1311 buffer.set(routingKey, byChild)
1312 }
1313
1314 const existing = byChild.get(childKey) || {
1315 deletes: 0,
1316 inserts: 0,
1317 value: childResult,
1318 orderByIndex: _orderByIndex,
1319 }
1320
1321 if (multiplicity < 0) {
1322 existing.deletes += Math.abs(multiplicity)
1323 } else if (multiplicity > 0) {
1324 existing.inserts += multiplicity
1325 existing.value = childResult
1326 }
1327
1328 byChild.set(childKey, existing)
1329 }
1330 }),
1331 )
1332
1333 const setup: NestedIncludesSetup = {
1334 compilationResult: entry,
1335 buffer,
1336 snapshot: new Map(),
1337 routingIndex: new Map(),
1338 routingReverseIndex: new Map(),
1339 routingChildToNested: new Map(),
1340 }
1341

Callers 1

setupIncludesOutputMethod · 0.85

Calls 7

outputFunction · 0.90
computeRoutingKeyFunction · 0.85
pipeMethod · 0.80
getInnerMethod · 0.80
mapMethod · 0.45
getMethod · 0.45
setMethod · 0.45

Tested by

no test coverage detected