(options: LoadSubsetOptions)
| 602 | } |
| 603 | |
| 604 | const unloadSubset = async (options: LoadSubsetOptions) => { |
| 605 | onUnloadSubset?.() |
| 606 | |
| 607 | const idx = activeWhereExpressions.indexOf(options.where) |
| 608 | if (idx !== -1) { |
| 609 | activeWhereExpressions.splice(idx, 1) |
| 610 | } |
| 611 | |
| 612 | // Evict rows that were exclusively loaded by the departing predicate. |
| 613 | // These are rows matching the departing WHERE that are no longer covered |
| 614 | // by any remaining active predicate. |
| 615 | const compiledDeparting = compileSQLite({ where: options.where }) |
| 616 | const departingWhereSQL = toInlinedWhereClause(compiledDeparting) |
| 617 | |
| 618 | let evictionSQL: string |
| 619 | if (activeWhereExpressions.length === 0) { |
| 620 | evictionSQL = `SELECT id FROM ${viewName} WHERE ${departingWhereSQL}` |
| 621 | } else { |
| 622 | const combinedRemaining = |
| 623 | activeWhereExpressions.length === 1 |
| 624 | ? activeWhereExpressions[0]! |
| 625 | : or( |
| 626 | activeWhereExpressions[0], |
| 627 | activeWhereExpressions[1], |
| 628 | ...activeWhereExpressions.slice(2), |
| 629 | ) |
| 630 | const compiledRemaining = compileSQLite({ |
| 631 | where: combinedRemaining, |
| 632 | }) |
| 633 | const remainingWhereSQL = toInlinedWhereClause(compiledRemaining) |
| 634 | evictionSQL = `SELECT id FROM ${viewName} WHERE (${departingWhereSQL}) AND NOT (${remainingWhereSQL})` |
| 635 | } |
| 636 | |
| 637 | const rowsToEvict = await database.getAll<{ id: string }>(evictionSQL) |
| 638 | if (rowsToEvict.length > 0) { |
| 639 | begin() |
| 640 | for (const { id } of rowsToEvict) { |
| 641 | write({ type: `delete`, key: id }) |
| 642 | } |
| 643 | commit() |
| 644 | } |
| 645 | |
| 646 | // Recreate the diff trigger for the remaining active WHERE expressions. |
| 647 | await loadSubset() |
| 648 | } |
| 649 | |
| 650 | markReady() |
| 651 |
no test coverage detected