()
| 508 | // On-demand mode. |
| 509 | // Registers a diff trigger for the active WHERE expressions. |
| 510 | function runOnDemandSync() { |
| 511 | let onUnloadSubset: CleanupFn | void | null = null |
| 512 | |
| 513 | start().catch((error) => |
| 514 | database.logger.error( |
| 515 | `Could not start syncing process for ${viewName} into ${trackedTableName}`, |
| 516 | error, |
| 517 | ), |
| 518 | ) |
| 519 | |
| 520 | // Tracks all active WHERE expressions for on-demand sync filtering. |
| 521 | // Each loadSubset call pushes its predicate; unloadSubset removes it. |
| 522 | const activeWhereExpressions: Array<LoadSubsetOptions['where']> = [] |
| 523 | |
| 524 | const loadSubset = async ( |
| 525 | options?: LoadSubsetOptions, |
| 526 | ): Promise<void> => { |
| 527 | if (options) { |
| 528 | activeWhereExpressions.push(options.where) |
| 529 | onUnloadSubset = await restConfig.onLoadSubset?.(options) |
| 530 | } |
| 531 | |
| 532 | if (activeWhereExpressions.length === 0) { |
| 533 | await database.writeLock(async (ctx) => { |
| 534 | await flushDiffRecordsWithContext(ctx) |
| 535 | await disposeTracking?.({ context: ctx }) |
| 536 | }) |
| 537 | return |
| 538 | } |
| 539 | |
| 540 | const combinedWhere = |
| 541 | activeWhereExpressions.length === 1 |
| 542 | ? activeWhereExpressions[0] |
| 543 | : or( |
| 544 | activeWhereExpressions[0], |
| 545 | activeWhereExpressions[1], |
| 546 | ...activeWhereExpressions.slice(2), |
| 547 | ) |
| 548 | |
| 549 | const compiledNewData = compileSQLite( |
| 550 | { where: combinedWhere }, |
| 551 | { jsonColumn: 'NEW.data' }, |
| 552 | ) |
| 553 | |
| 554 | const compiledOldData = compileSQLite( |
| 555 | { where: combinedWhere }, |
| 556 | { jsonColumn: 'OLD.data' }, |
| 557 | ) |
| 558 | |
| 559 | const compiledView = compileSQLite({ where: combinedWhere }) |
| 560 | |
| 561 | const newDataWhenClause = toInlinedWhereClause(compiledNewData) |
| 562 | const oldDataWhenClause = toInlinedWhereClause(compiledOldData) |
| 563 | const viewWhereClause = toInlinedWhereClause(compiledView) |
| 564 | |
| 565 | await database.writeLock(async (ctx) => { |
| 566 | await flushDiffRecordsWithContext(ctx) |
| 567 | await disposeTracking?.({ context: ctx }) |
no test coverage detected