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

Function processChangeMessage

packages/electric-db-collection/src/electric.ts:1497–1548  ·  view source on GitHub ↗
(changeMessage: Message<T>)

Source from the content-addressed store, hash-verified

1495 * Process a change message: handle tags and write the mutation
1496 */
1497 const processChangeMessage = (changeMessage: Message<T>) => {
1498 if (!isChangeMessage(changeMessage)) {
1499 return
1500 }
1501
1502 // Process tags if present
1503 const tags = changeMessage.headers.tags
1504 const removedTags = changeMessage.headers.removed_tags
1505 const hasTags = tags || removedTags
1506
1507 // Extract active_conditions from headers (DNF support)
1508 const activeConditions = changeMessage.headers.active_conditions as
1509 | ActiveConditions
1510 | undefined
1511
1512 const rowId = collection.getKeyFromItem(changeMessage.value)
1513 const operation = changeMessage.headers.operation
1514
1515 // Track synced keys and handle overlapping subset queries.
1516 // When multiple subset queries return the same row, the server sends
1517 // `insert` for each response. We convert subsequent inserts to updates
1518 // to avoid duplicate key errors when the row's data has changed.
1519 const isDelete = operation === `delete`
1520 const isDuplicateInsert =
1521 operation === `insert` && syncedKeys.has(rowId)
1522
1523 if (isDelete) {
1524 syncedKeys.delete(rowId)
1525 } else {
1526 syncedKeys.add(rowId)
1527 }
1528
1529 if (isDelete) {
1530 clearTagsForRow(rowId)
1531 } else if (hasTags) {
1532 processTagsForChangeMessage(
1533 tags,
1534 removedTags,
1535 rowId,
1536 activeConditions,
1537 )
1538 }
1539
1540 write({
1541 type: isDuplicateInsert ? `update` : operation,
1542 value: changeMessage.value,
1543 // Include the primary key and relation info in the metadata
1544 metadata: {
1545 ...changeMessage.headers,
1546 },
1547 })
1548 }
1549
1550 // Create deduplicated loadSubset wrapper for non-eager modes
1551 // This prevents redundant snapshot requests when multiple concurrent

Callers 1

createElectricSyncFunction · 0.85

Calls 6

clearTagsForRowFunction · 0.85
getKeyFromItemMethod · 0.80
hasMethod · 0.45
deleteMethod · 0.45
addMethod · 0.45

Tested by

no test coverage detected