tinybase 1.2.3 → 1.3.0-beta.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1230,6 +1230,7 @@ const idsChanged = (ids, id, added) =>
1230
1230
  mapSet(ids, id, mapGet(ids, id) == -added ? void 0 : added);
1231
1231
  const createStore = () => {
1232
1232
  let hasSchema;
1233
+ let cellsTouched;
1233
1234
  let nextRowId = 0;
1234
1235
  let transactions = 0;
1235
1236
  const changedTableIds = mapNew();
@@ -1248,6 +1249,7 @@ const createStore = () => {
1248
1249
  const cellIdsListeners = mapNewPair();
1249
1250
  const cellListeners = mapNewPair();
1250
1251
  const invalidCellListeners = mapNewPair();
1252
+ const finishTransactionListeners = mapNewPair(setNew);
1251
1253
  const [addListener, callListeners, delListenerImpl, callListenerImpl] =
1252
1254
  getListenerFunctions(() => store);
1253
1255
  const validateSchema = (schema) =>
@@ -1480,75 +1482,73 @@ const createStore = () => {
1480
1482
  )
1481
1483
  : 0;
1482
1484
  const callListenersForChanges = (mutator) => {
1483
- if (!collIsEmpty(changedCells)) {
1484
- const emptyIdListeners =
1485
- collIsEmpty(cellIdsListeners[mutator]) &&
1486
- collIsEmpty(rowIdsListeners[mutator]) &&
1487
- collIsEmpty(tableIdsListeners[mutator]);
1488
- const emptyOtherListeners =
1489
- collIsEmpty(cellListeners[mutator]) &&
1490
- collIsEmpty(rowListeners[mutator]) &&
1491
- collIsEmpty(tableListeners[mutator]) &&
1492
- collIsEmpty(tablesListeners[mutator]);
1493
- if (!(emptyIdListeners && emptyOtherListeners)) {
1494
- const changes = mutator
1495
- ? [
1496
- mapClone(changedTableIds),
1497
- mapClone2(changedRowIds),
1498
- mapClone(changedCellIds, mapClone2),
1499
- mapClone(changedCells, mapClone2),
1500
- ]
1501
- : [changedTableIds, changedRowIds, changedCellIds, changedCells];
1502
- if (!emptyIdListeners) {
1503
- collForEach(changes[2], (rowCellIds, tableId) =>
1504
- collForEach(rowCellIds, (changedIds, rowId) => {
1505
- if (!collIsEmpty(changedIds)) {
1506
- callListeners(cellIdsListeners[mutator], [tableId, rowId]);
1507
- }
1508
- }),
1509
- );
1510
- collForEach(changes[1], (changedIds, tableId) => {
1485
+ const emptyIdListeners =
1486
+ collIsEmpty(cellIdsListeners[mutator]) &&
1487
+ collIsEmpty(rowIdsListeners[mutator]) &&
1488
+ collIsEmpty(tableIdsListeners[mutator]);
1489
+ const emptyOtherListeners =
1490
+ collIsEmpty(cellListeners[mutator]) &&
1491
+ collIsEmpty(rowListeners[mutator]) &&
1492
+ collIsEmpty(tableListeners[mutator]) &&
1493
+ collIsEmpty(tablesListeners[mutator]);
1494
+ if (!(emptyIdListeners && emptyOtherListeners)) {
1495
+ const changes = mutator
1496
+ ? [
1497
+ mapClone(changedTableIds),
1498
+ mapClone2(changedRowIds),
1499
+ mapClone(changedCellIds, mapClone2),
1500
+ mapClone(changedCells, mapClone2),
1501
+ ]
1502
+ : [changedTableIds, changedRowIds, changedCellIds, changedCells];
1503
+ if (!emptyIdListeners) {
1504
+ collForEach(changes[2], (rowCellIds, tableId) =>
1505
+ collForEach(rowCellIds, (changedIds, rowId) => {
1511
1506
  if (!collIsEmpty(changedIds)) {
1512
- callListeners(rowIdsListeners[mutator], [tableId]);
1507
+ callListeners(cellIdsListeners[mutator], [tableId, rowId]);
1513
1508
  }
1514
- });
1515
- if (!collIsEmpty(changes[0])) {
1516
- callListeners(tableIdsListeners[mutator]);
1509
+ }),
1510
+ );
1511
+ collForEach(changes[1], (changedIds, tableId) => {
1512
+ if (!collIsEmpty(changedIds)) {
1513
+ callListeners(rowIdsListeners[mutator], [tableId]);
1517
1514
  }
1515
+ });
1516
+ if (!collIsEmpty(changes[0])) {
1517
+ callListeners(tableIdsListeners[mutator]);
1518
1518
  }
1519
- if (!emptyOtherListeners) {
1520
- let tablesChanged;
1521
- collForEach(changes[3], (rows, tableId) => {
1522
- let tableChanged;
1523
- collForEach(rows, (cells, rowId) => {
1524
- let rowChanged;
1525
- collForEach(cells, ([oldCell, newCell], cellId) => {
1526
- if (newCell !== oldCell) {
1527
- callListeners(
1528
- cellListeners[mutator],
1529
- [tableId, rowId, cellId],
1530
- newCell,
1531
- oldCell,
1532
- getCellChange,
1533
- );
1534
- tablesChanged = tableChanged = rowChanged = 1;
1535
- }
1536
- });
1537
- if (rowChanged) {
1519
+ }
1520
+ if (!emptyOtherListeners) {
1521
+ let tablesChanged;
1522
+ collForEach(changes[3], (rows, tableId) => {
1523
+ let tableChanged;
1524
+ collForEach(rows, (cells, rowId) => {
1525
+ let rowChanged;
1526
+ collForEach(cells, ([oldCell, newCell], cellId) => {
1527
+ if (newCell !== oldCell) {
1538
1528
  callListeners(
1539
- rowListeners[mutator],
1540
- [tableId, rowId],
1529
+ cellListeners[mutator],
1530
+ [tableId, rowId, cellId],
1531
+ newCell,
1532
+ oldCell,
1541
1533
  getCellChange,
1542
1534
  );
1535
+ tablesChanged = tableChanged = rowChanged = 1;
1543
1536
  }
1544
1537
  });
1545
- if (tableChanged) {
1546
- callListeners(tableListeners[mutator], [tableId], getCellChange);
1538
+ if (rowChanged) {
1539
+ callListeners(
1540
+ rowListeners[mutator],
1541
+ [tableId, rowId],
1542
+ getCellChange,
1543
+ );
1547
1544
  }
1548
1545
  });
1549
- if (tablesChanged) {
1550
- callListeners(tablesListeners[mutator], [], getCellChange);
1546
+ if (tableChanged) {
1547
+ callListeners(tableListeners[mutator], [tableId], getCellChange);
1551
1548
  }
1549
+ });
1550
+ if (tablesChanged) {
1551
+ callListeners(tablesListeners[mutator], [], getCellChange);
1552
1552
  }
1553
1553
  }
1554
1554
  }
@@ -1677,61 +1677,79 @@ const createStore = () => {
1677
1677
  if (transactions == -1) {
1678
1678
  return;
1679
1679
  }
1680
- transactions++;
1680
+ startTransaction();
1681
1681
  const result = actions?.();
1682
- transactions--;
1683
- if (transactions == 0) {
1684
- transactions = 1;
1685
- callInvalidCellListeners(1);
1686
- callListenersForChanges(1);
1687
- transactions = -1;
1688
- if (
1689
- doRollback?.(
1690
- mapToObj(
1691
- changedCells,
1692
- (table) =>
1693
- mapToObj(
1694
- table,
1695
- (row) =>
1696
- mapToObj(
1697
- row,
1698
- (cells) => [...cells],
1699
- ([oldCell, newCell]) => oldCell === newCell,
1700
- ),
1701
- objIsEmpty,
1702
- ),
1703
- objIsEmpty,
1704
- ),
1705
- mapToObj(invalidCells, (map) => mapToObj(map, mapToObj)),
1706
- )
1707
- ) {
1682
+ finishTransaction(doRollback);
1683
+ return result;
1684
+ };
1685
+ const startTransaction = () => {
1686
+ transactions++;
1687
+ return store;
1688
+ };
1689
+ const finishTransaction = (doRollback) => {
1690
+ if (transactions > 0) {
1691
+ transactions--;
1692
+ if (transactions == 0) {
1693
+ cellsTouched = !collIsEmpty(changedCells);
1708
1694
  transactions = 1;
1709
- collForEach(changedCells, (table, tableId) =>
1710
- collForEach(table, (row, rowId) =>
1711
- collForEach(row, ([oldCell], cellId) =>
1712
- isUndefined(oldCell)
1713
- ? delCell(tableId, rowId, cellId, true)
1714
- : setCell(tableId, rowId, cellId, oldCell),
1695
+ callInvalidCellListeners(1);
1696
+ if (cellsTouched) {
1697
+ callListenersForChanges(1);
1698
+ }
1699
+ transactions = -1;
1700
+ if (
1701
+ doRollback?.(
1702
+ mapToObj(
1703
+ changedCells,
1704
+ (table) =>
1705
+ mapToObj(
1706
+ table,
1707
+ (row) =>
1708
+ mapToObj(
1709
+ row,
1710
+ (cells) => [...cells],
1711
+ ([oldCell, newCell]) => oldCell === newCell,
1712
+ ),
1713
+ objIsEmpty,
1714
+ ),
1715
+ objIsEmpty,
1715
1716
  ),
1716
- ),
1717
+ mapToObj(invalidCells, (map) => mapToObj(map, mapToObj)),
1718
+ )
1719
+ ) {
1720
+ transactions = 1;
1721
+ collForEach(changedCells, (table, tableId) =>
1722
+ collForEach(table, (row, rowId) =>
1723
+ collForEach(row, ([oldCell], cellId) =>
1724
+ isUndefined(oldCell)
1725
+ ? delCell(tableId, rowId, cellId, true)
1726
+ : setCell(tableId, rowId, cellId, oldCell),
1727
+ ),
1728
+ ),
1729
+ );
1730
+ transactions = -1;
1731
+ cellsTouched = false;
1732
+ }
1733
+ callListeners(finishTransactionListeners[0], [], cellsTouched);
1734
+ callInvalidCellListeners(0);
1735
+ if (cellsTouched) {
1736
+ callListenersForChanges(0);
1737
+ }
1738
+ callListeners(finishTransactionListeners[1], [], cellsTouched);
1739
+ transactions = 0;
1740
+ arrayForEach(
1741
+ [
1742
+ changedCells,
1743
+ invalidCells,
1744
+ changedTableIds,
1745
+ changedRowIds,
1746
+ changedCellIds,
1747
+ ],
1748
+ collClear,
1717
1749
  );
1718
- transactions = -1;
1719
1750
  }
1720
- callInvalidCellListeners(0);
1721
- callListenersForChanges(0);
1722
- transactions = 0;
1723
- arrayForEach(
1724
- [
1725
- changedCells,
1726
- invalidCells,
1727
- changedTableIds,
1728
- changedRowIds,
1729
- changedCellIds,
1730
- ],
1731
- collClear,
1732
- );
1733
1751
  }
1734
- return result;
1752
+ return store;
1735
1753
  };
1736
1754
  const forEachTable = (tableCallback) =>
1737
1755
  collForEach(tablesMap, (tableMap, tableId) =>
@@ -1773,6 +1791,10 @@ const createStore = () => {
1773
1791
  rowId,
1774
1792
  cellId,
1775
1793
  ]);
1794
+ const addWillFinishTransactionListener = (listener) =>
1795
+ addListener(listener, finishTransactionListeners[0]);
1796
+ const addDidFinishTransactionListener = (listener) =>
1797
+ addListener(listener, finishTransactionListeners[1]);
1776
1798
  const callListener = (listenerId) => {
1777
1799
  callListenerImpl(listenerId, [getTableIds, getRowIds, getCellIds], (ids) =>
1778
1800
  isUndefined(ids[2]) ? [] : arrayPair(getCell(...ids)),
@@ -1821,6 +1843,8 @@ const createStore = () => {
1821
1843
  delCell,
1822
1844
  delSchema,
1823
1845
  transaction,
1846
+ startTransaction,
1847
+ finishTransaction,
1824
1848
  forEachTable,
1825
1849
  forEachRow,
1826
1850
  forEachCell,
@@ -1832,6 +1856,8 @@ const createStore = () => {
1832
1856
  addCellIdsListener,
1833
1857
  addCellListener,
1834
1858
  addInvalidCellListener,
1859
+ addWillFinishTransactionListener,
1860
+ addDidFinishTransactionListener,
1835
1861
  callListener,
1836
1862
  delListener,
1837
1863
  getListenerStats,
package/lib/store.d.ts CHANGED
@@ -176,6 +176,32 @@ export type MapCell = (cell: CellOrUndefined) => Cell;
176
176
  */
177
177
  export type GetCell = (cellId: Id) => CellOrUndefined;
178
178
 
179
+ /**
180
+ * The TransactionListener type describes a function that is used to listen to
181
+ * the completion of a transaction for the Store.
182
+ *
183
+ * A TransactionListener is provided when using the
184
+ * addWillFinishTransactionListener and addDidFinishTransactionListener methods.
185
+ * See those methods for specific examples.
186
+ *
187
+ * When called, a TransactionListener is simply given a reference to the Store
188
+ * and a boolean to indicate whether Cell values have been touched during the
189
+ * transaction. The latter flag is intended as a hint about whether non-mutating
190
+ * listeners might be being called at the end of the transaction.
191
+ *
192
+ * Here, 'touched' means that Cell values have either been changed, or changed
193
+ * and then changed back to its original value during the transaction. The
194
+ * exception is a transaction that has been rolled back, for which the value of
195
+ * `cellsTouched` in the listener will be `false` because all changes have been
196
+ * reverted.
197
+ *
198
+ * @param store A reference to the Store that is completing a transaction.
199
+ * @param cellsTouched Whether Cell values have been touched during the
200
+ * transaction.
201
+ * @category Listener
202
+ */
203
+ export type TransactionListener = (store: Store, cellsTouched: boolean) => void;
204
+
179
205
  /**
180
206
  * The TablesListener type describes a function that is used to listen to
181
207
  * changes to the whole Store.
@@ -1652,7 +1678,7 @@ export interface Store {
1652
1678
 
1653
1679
  /**
1654
1680
  * The transaction method takes a function that makes multiple mutations to
1655
- * the store, buffering all calls to the relevant listeners until it
1681
+ * the Store, buffering all calls to the relevant listeners until it
1656
1682
  * completes.
1657
1683
  *
1658
1684
  * This method is useful for making bulk changes to the data in a Store, and
@@ -1771,6 +1797,135 @@ export interface Store {
1771
1797
  ) => boolean,
1772
1798
  ): Return;
1773
1799
 
1800
+ /**
1801
+ * The startTransaction allows you to explicitly start a transaction that will
1802
+ * make multiple mutations to the Store, buffering all calls to the relevant
1803
+ * listeners until it completes when you call the finishTransaction method.
1804
+ *
1805
+ * Transactions are useful for making bulk changes to the data in a Store, and
1806
+ * when you don't want listeners to be called as you make each change. Changes
1807
+ * are made silently during the transaction, and listeners relevant to the
1808
+ * changes you have made will instead only be called when the whole
1809
+ * transaction is complete.
1810
+ *
1811
+ * Generally it is preferable to use the transaction method to wrap a block of
1812
+ * code as a transaction. It simply calls both the startTransaction and
1813
+ * finishTransaction methods for you. See that method for several transaction
1814
+ * examples.
1815
+ *
1816
+ * Use this startTransaction method when you have a more 'open-ended'
1817
+ * transaction, such as one containing mutations triggered from other events
1818
+ * that are asynchronous or not occurring inline to your code. You must
1819
+ * remember to also call the finishTransaction method explicitly when it is
1820
+ * done, of course.
1821
+ *
1822
+ * @returns A reference to the Store.
1823
+ * @example
1824
+ * This example makes changes to two Cells, first outside, and secondly
1825
+ * within, a transaction that is explicitly started and finished. In the
1826
+ * second case, the Row listener is only called once.
1827
+ *
1828
+ * ```js
1829
+ * const store = createStore().setTables({pets: {fido: {species: 'dog'}}});
1830
+ * store.addRowListener('pets', 'fido', () => console.log('Fido changed'));
1831
+ *
1832
+ * store.setCell('pets', 'fido', 'color', 'brown');
1833
+ * store.setCell('pets', 'fido', 'sold', false);
1834
+ * // -> 'Fido changed'
1835
+ * // -> 'Fido changed'
1836
+ *
1837
+ * store.startTransaction();
1838
+ * store.setCell('pets', 'fido', 'color', 'walnut');
1839
+ * store.setCell('pets', 'fido', 'sold', true);
1840
+ * store.finishTransaction();
1841
+ * // -> 'Fido changed'
1842
+ * ```
1843
+ * @category Transaction
1844
+ */
1845
+ startTransaction(): Store;
1846
+
1847
+ /**
1848
+ * The finishTransaction allows you to explicitly finish a transaction that
1849
+ * has made multiple mutations to the Store, triggering all calls to the
1850
+ * relevant listeners.
1851
+ *
1852
+ * Transactions are useful for making bulk changes to the data in a Store, and
1853
+ * when you don't want listeners to be called as you make each change. Changes
1854
+ * are made silently during the transaction, and listeners relevant to the
1855
+ * changes you have made will instead only be called when the whole
1856
+ * transaction is complete.
1857
+ *
1858
+ * Generally it is preferable to use the transaction method to wrap a block of
1859
+ * code as a transaction. It simply calls both the startTransaction and
1860
+ * finishTransaction methods for you. See that method for several transaction
1861
+ * examples.
1862
+ *
1863
+ * Use this finishTransaction method when you have a more 'open-ended'
1864
+ * transaction, such as one containing mutations triggered from other events
1865
+ * that are asynchronous or not occurring inline to your code. There must have
1866
+ * been a corresponding startTransaction method that this completes, of
1867
+ * course, otherwise this function has no effect.
1868
+ *
1869
+ * @param doRollback An optional callback that should return `true` if you
1870
+ * want to rollback the transaction at the end.
1871
+ * @returns A reference to the Store.
1872
+ * @example
1873
+ * This example makes changes to two Cells, first outside, and secondly
1874
+ * within, a transaction that is explicitly started and finished. In the
1875
+ * second case, the Row listener is only called once.
1876
+ *
1877
+ * ```js
1878
+ * const store = createStore().setTables({pets: {fido: {species: 'dog'}}});
1879
+ * store.addRowListener('pets', 'fido', () => console.log('Fido changed'));
1880
+ *
1881
+ * store.setCell('pets', 'fido', 'color', 'brown');
1882
+ * store.setCell('pets', 'fido', 'sold', false);
1883
+ * // -> 'Fido changed'
1884
+ * // -> 'Fido changed'
1885
+ *
1886
+ * store.startTransaction();
1887
+ * store.setCell('pets', 'fido', 'color', 'walnut');
1888
+ * store.setCell('pets', 'fido', 'sold', true);
1889
+ * store.finishTransaction();
1890
+ * // -> 'Fido changed'
1891
+ * ```
1892
+ * @example
1893
+ * This example makes multiple changes to the Store, including some attempts
1894
+ * to update a Cell with invalid values. The `doRollback` callback receives
1895
+ * information about the changes and invalid attempts, and then judges that
1896
+ * the transaction should be rolled back to its original state.
1897
+ *
1898
+ * ```js
1899
+ * const store = createStore().setTables({
1900
+ * pets: {fido: {species: 'dog', color: 'brown'}},
1901
+ * });
1902
+ *
1903
+ * store.startTransaction();
1904
+ * store.setCell('pets', 'fido', 'color', 'black');
1905
+ * store.setCell('pets', 'fido', 'eyes', ['left', 'right']);
1906
+ * store.setCell('pets', 'fido', 'info', {sold: null});
1907
+ * store.finishTransaction((changedCells, invalidCells) => {
1908
+ * console.log(store.getTables());
1909
+ * // -> {pets: {fido: {species: 'dog', color: 'black'}}}
1910
+ * console.log(changedCells);
1911
+ * // -> {pets: {fido: {color: ['brown', 'black']}}}
1912
+ * console.log(invalidCells);
1913
+ * // -> {pets: {fido: {eyes: [['left', 'right']], info: [{sold: null}]}}}
1914
+ * return invalidCells['pets'] != null;
1915
+ * });
1916
+ *
1917
+ * console.log(store.getTables());
1918
+ * // -> {pets: {fido: {species: 'dog', color: 'brown'}}}
1919
+ * ```
1920
+ * @category Transaction
1921
+ */
1922
+ finishTransaction(
1923
+ doRollback?: (
1924
+ changedCells: ChangedCells,
1925
+ invalidCells: InvalidCells,
1926
+ ) => boolean,
1927
+ ): Store;
1928
+
1774
1929
  /**
1775
1930
  * The forEachTable method takes a function that it will then call for each
1776
1931
  * Table in the Store.
@@ -2746,6 +2901,143 @@ export interface Store {
2746
2901
  mutator?: boolean,
2747
2902
  ): Id;
2748
2903
 
2904
+ /**
2905
+ * The addWillFinishTransactionListener method registers a listener function
2906
+ * with the Store that will be called just before other non-mutating listeners
2907
+ * are called at the end of the transaction.
2908
+ *
2909
+ * This is useful if you need to know that a set of listeners are about to be
2910
+ * called at the end of a transaction, perhaps to batch _their_ consequences
2911
+ * together.
2912
+ *
2913
+ * The provided TransactionListener will receive a reference to the Store and
2914
+ * a boolean to indicate whether Cell values have been touched during the
2915
+ * transaction. The latter flag is intended as a hint about whether
2916
+ * non-mutating listeners might be being called at the end of the transaction.
2917
+ *
2918
+ * Here, 'touched' means that Cell values have either been changed, or changed
2919
+ * and then changed back to its original value during the transaction. The
2920
+ * exception is a transaction that has been rolled back, for which the value
2921
+ * of `cellsTouched` in the listener will be `false` because all changes have
2922
+ * been reverted.
2923
+ *
2924
+ * @returns A unique Id for the listener that can later be used to remove it.
2925
+ * @example
2926
+ * This example registers a listener that is called at the end of the
2927
+ * transaction, just before its listeners will be called. The transactions
2928
+ * shown here variously change, touch, and rollback cells, demonstrating how
2929
+ * the `cellsTouched` parameter in the listener works.
2930
+ *
2931
+ * ```js
2932
+ * const store = createStore().setTables({
2933
+ * pets: {fido: {species: 'dog', color: 'brown'}},
2934
+ * });
2935
+ * const listenerId = store.addWillFinishTransactionListener(
2936
+ * (store, cellsTouched) => console.log(`Cells touched: ${cellsTouched}`),
2937
+ * );
2938
+ * const listenerId2 = store.addTablesListener(() =>
2939
+ * console.log('Tables changed'),
2940
+ * );
2941
+ *
2942
+ * store.transaction(() => store.setCell('pets', 'fido', 'color', 'brown'));
2943
+ * // -> 'Cells touched: false'
2944
+ *
2945
+ * store.transaction(() => store.setCell('pets', 'fido', 'color', 'walnut'));
2946
+ * // -> 'Cells touched: true'
2947
+ * // -> 'Tables changed'
2948
+ *
2949
+ * store.transaction(() => {
2950
+ * store.setRow('pets', 'felix', {species: 'cat'});
2951
+ * store.delRow('pets', 'felix');
2952
+ * });
2953
+ * // -> 'Cells touched: true'
2954
+ *
2955
+ * store.transaction(
2956
+ * () => store.setRow('pets', 'fido', {species: 'dog'}),
2957
+ * () => true,
2958
+ * );
2959
+ * // -> 'Cells touched: false'
2960
+ * // Transaction was rolled back.
2961
+ *
2962
+ * store.callListener(listenerId);
2963
+ * // -> 'Cells touched: undefined'
2964
+ * // It is meaningless to call this listener directly.
2965
+ *
2966
+ * store.delListener(listenerId).delListener(listenerId2);
2967
+ * ```
2968
+ * @category Listener
2969
+ */
2970
+ addWillFinishTransactionListener(listener: TransactionListener): Id;
2971
+
2972
+ /**
2973
+ * The addDidFinishTransactionListener method registers a listener function
2974
+ * with the Store that will be called just after other non-mutating listeners
2975
+ * are called at the end of the transaction.
2976
+ *
2977
+ * This is useful if you need to know that a set of listeners have just been
2978
+ * called at the end of a transaction, perhaps to batch _their_ consequences
2979
+ * together.
2980
+ *
2981
+ * The provided TransactionListener will receive a reference to the Store and
2982
+ * a boolean to indicate whether Cell values have been touched during the
2983
+ * transaction. The latter flag is intended as a hint about whether
2984
+ * non-mutating listeners might have been called at the end of the
2985
+ * transaction.
2986
+ *
2987
+ * Here, 'touched' means that Cell values have either been changed, or changed
2988
+ * and then changed back to its original value during the transaction. The
2989
+ * exception is a transaction that has been rolled back, for which the value
2990
+ * of `cellsTouched` in the listener will be `false` because all changes have
2991
+ * been reverted.
2992
+ *
2993
+ * @returns A unique Id for the listener that can later be used to remove it.
2994
+ * @example
2995
+ * This example registers a listener that is called at the end of the
2996
+ * transaction, just after its listeners have been called. The transactions
2997
+ * shown here variously change, touch, and rollback cells, demonstrating how
2998
+ * the `cellsTouched` parameter in the listener works.
2999
+ *
3000
+ * ```js
3001
+ * const store = createStore().setTables({
3002
+ * pets: {fido: {species: 'dog', color: 'brown'}},
3003
+ * });
3004
+ * const listenerId = store.addDidFinishTransactionListener(
3005
+ * (store, cellsTouched) => console.log(`Cells touched: ${cellsTouched}`),
3006
+ * );
3007
+ * const listenerId2 = store.addTablesListener(() =>
3008
+ * console.log('Tables changed'),
3009
+ * );
3010
+ *
3011
+ * store.transaction(() => store.setCell('pets', 'fido', 'color', 'brown'));
3012
+ * // -> 'Cells touched: false'
3013
+ *
3014
+ * store.transaction(() => store.setCell('pets', 'fido', 'color', 'walnut'));
3015
+ * // -> 'Tables changed'
3016
+ * // -> 'Cells touched: true'
3017
+ *
3018
+ * store.transaction(() => {
3019
+ * store.setRow('pets', 'felix', {species: 'cat'});
3020
+ * store.delRow('pets', 'felix');
3021
+ * });
3022
+ * // -> 'Cells touched: true'
3023
+ *
3024
+ * store.transaction(
3025
+ * () => store.setRow('pets', 'fido', {species: 'dog'}),
3026
+ * () => true,
3027
+ * );
3028
+ * // -> 'Cells touched: false'
3029
+ * // Transaction was rolled back.
3030
+ *
3031
+ * store.callListener(listenerId);
3032
+ * // -> 'Cells touched: undefined'
3033
+ * // It is meaningless to call this listener directly.
3034
+ *
3035
+ * store.delListener(listenerId).delListener(listenerId2);
3036
+ * ```
3037
+ * @category Listener
3038
+ */
3039
+ addDidFinishTransactionListener(listener: TransactionListener): Id;
3040
+
2749
3041
  /**
2750
3042
  * The callListener method provides a way for you to manually provoke a
2751
3043
  * listener to be called, even if the underlying data hasn't changed.