tinybase 1.0.2 → 1.1.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.
Files changed (60) hide show
  1. package/lib/checkpoints.d.ts +945 -0
  2. package/lib/checkpoints.js +1 -0
  3. package/lib/checkpoints.js.gz +0 -0
  4. package/lib/common.d.ts +115 -0
  5. package/lib/common.js +1 -0
  6. package/lib/common.js.gz +0 -0
  7. package/lib/debug/checkpoints.d.ts +66 -0
  8. package/lib/debug/checkpoints.js +332 -0
  9. package/lib/debug/common.js +3 -0
  10. package/lib/debug/indexes.d.ts +167 -6
  11. package/lib/debug/indexes.js +431 -0
  12. package/lib/debug/metrics.d.ts +72 -0
  13. package/lib/debug/metrics.js +401 -0
  14. package/lib/debug/persisters.js +191 -0
  15. package/lib/debug/relationships.d.ts +86 -1
  16. package/lib/debug/relationships.js +434 -0
  17. package/lib/debug/store.d.ts +187 -5
  18. package/lib/debug/store.js +783 -0
  19. package/lib/debug/tinybase.js +144 -39
  20. package/lib/indexes.d.ts +939 -0
  21. package/lib/indexes.js +1 -0
  22. package/lib/indexes.js.gz +0 -0
  23. package/lib/metrics.d.ts +829 -0
  24. package/lib/metrics.js +1 -0
  25. package/lib/metrics.js.gz +0 -0
  26. package/lib/persisters.d.ts +711 -0
  27. package/lib/persisters.js +1 -0
  28. package/lib/persisters.js.gz +0 -0
  29. package/lib/relationships.d.ts +1201 -0
  30. package/lib/relationships.js +1 -0
  31. package/lib/relationships.js.gz +0 -0
  32. package/lib/store.d.ts +2688 -0
  33. package/lib/store.js +1 -0
  34. package/lib/store.js.gz +0 -0
  35. package/lib/tinybase.d.ts +13 -0
  36. package/lib/tinybase.js +1 -0
  37. package/lib/tinybase.js.gz +0 -0
  38. package/lib/ui-react.d.ts +7185 -0
  39. package/lib/ui-react.js +1 -0
  40. package/lib/ui-react.js.gz +0 -0
  41. package/lib/umd/checkpoints.js +1 -0
  42. package/lib/umd/checkpoints.js.gz +0 -0
  43. package/lib/umd/common.js +1 -0
  44. package/lib/umd/common.js.gz +0 -0
  45. package/lib/umd/indexes.js +1 -0
  46. package/lib/umd/indexes.js.gz +0 -0
  47. package/lib/umd/metrics.js +1 -0
  48. package/lib/umd/metrics.js.gz +0 -0
  49. package/lib/umd/persisters.js +1 -0
  50. package/lib/umd/persisters.js.gz +0 -0
  51. package/lib/umd/relationships.js +1 -0
  52. package/lib/umd/relationships.js.gz +0 -0
  53. package/lib/umd/store.js +1 -0
  54. package/lib/umd/store.js.gz +0 -0
  55. package/lib/umd/tinybase.js +1 -0
  56. package/lib/umd/tinybase.js.gz +0 -0
  57. package/lib/umd/ui-react.js +1 -0
  58. package/lib/umd/ui-react.js.gz +0 -0
  59. package/package.json +14 -14
  60. package/readme.md +2 -2
package/lib/store.d.ts ADDED
@@ -0,0 +1,2688 @@
1
+ /**
2
+ * The store module is the core of the TinyBase project and contains the types,
3
+ * interfaces, and functions to work with Store objects.
4
+ *
5
+ * The main entry point to this module is the createStore function, which
6
+ * returns a new Store. From there, you can set and get data, register
7
+ * listeners, and use other modules to build an entire app around the state and
8
+ * tabular data within.
9
+ *
10
+ * @packageDocumentation
11
+ * @module store
12
+ */
13
+
14
+ import {Id, IdOrNull, Ids, Json} from './common.d';
15
+
16
+ /**
17
+ * The Tables type is the data structure representing all of the data in a
18
+ * Store.
19
+ *
20
+ * A Tables object is used when setting all of the tables together with the
21
+ * setTables method, and when getting them back out again with the getTables
22
+ * method. A Tables object is a regular JavaScript object containing individual
23
+ * Table objects, keyed by their Id.
24
+ *
25
+ * @example
26
+ * ```js
27
+ * const tables: Tables = {
28
+ * pets: {
29
+ * fido: {species: 'dog', color: 'brown'},
30
+ * felix: {species: 'cat'},
31
+ * },
32
+ * species: {
33
+ * dog: {price: 5},
34
+ * cat: {price: 4},
35
+ * },
36
+ * };
37
+ * ```
38
+ * @category Store
39
+ */
40
+ export type Tables = {[tableId: Id]: Table};
41
+
42
+ /**
43
+ * The Table type is the data structure representing the data in a single table.
44
+ *
45
+ * A Table is used when setting a table with the setTable method, and when
46
+ * getting it back out again with the getTable method. A Table object is a
47
+ * regular JavaScript object containing individual Row objects, keyed by their
48
+ * Id.
49
+ *
50
+ * @example
51
+ * ```js
52
+ * const table: Table = {
53
+ * fido: {species: 'dog', color: 'brown'},
54
+ * felix: {species: 'cat'},
55
+ * };
56
+ * ```
57
+ * @category Store
58
+ */
59
+ export type Table = {[rowId: Id]: Row};
60
+
61
+ /**
62
+ * The Row type is the data structure representing the data in a single row.
63
+ *
64
+ * A Row is used when setting a row with the setRow method, and when getting it
65
+ * back out again with the getRow method. A Row object is a regular JavaScript
66
+ * object containing individual Cell objects, keyed by their Id.
67
+ *
68
+ * @example
69
+ * ```js
70
+ * const row: Row = {species: 'dog', color: 'brown'};
71
+ * ```
72
+ * @category Store
73
+ */
74
+ export type Row = {[cellId: Id]: Cell};
75
+
76
+ /**
77
+ * The Cell type is the data structure representing the data in a single cell.
78
+ *
79
+ * A Cell is used when setting a cell with the setCell method, and when getting
80
+ * it back out again with the getCell method. A Cell is a JavaScript string,
81
+ * number, or boolean.
82
+ *
83
+ * @example
84
+ * ```js
85
+ * const cell: Cell = 'dog';
86
+ * ```
87
+ * @category Store
88
+ */
89
+ export type Cell = string | number | boolean;
90
+
91
+ /**
92
+ * The TableCallback type describes a function that takes a Table's Id and a
93
+ * callback to loop over each Row within it.
94
+ *
95
+ * A TableCallback is provided when using the forEachTable method, so that you
96
+ * can do something based on every Table in the Store. See that method for
97
+ * specific examples.
98
+ *
99
+ * @param tableId The Id of the Table that the callback can operate on.
100
+ * @param forEachRow A function that will let you iterate over the Row objects
101
+ * in this Table.
102
+ * @category Callback
103
+ */
104
+ export type TableCallback = (
105
+ tableId: Id,
106
+ forEachRow: (rowCallback: RowCallback) => void,
107
+ ) => void;
108
+
109
+ /**
110
+ * The RowCallback type describes a function that takes a Row's Id and a
111
+ * callback to loop over each Cell within it.
112
+ *
113
+ * A RowCallback is provided when using the forEachRow method, so that you can
114
+ * do something based on every Row in a Table. See that method for specific
115
+ * examples.
116
+ *
117
+ * @param rowId The Id of the Row that the callback can operate on.
118
+ * @param forEachRow A function that will let you iterate over the Cell values
119
+ * in this Row.
120
+ * @category Callback
121
+ */
122
+ export type RowCallback = (
123
+ rowId: Id,
124
+ forEachCell: (cellCallback: CellCallback) => void,
125
+ ) => void;
126
+
127
+ /**
128
+ * The CellCallback type describes a function that takes a Cell's Id and its
129
+ * value.
130
+ *
131
+ * A CellCallback is provided when using the forEachCell method, so that you can
132
+ * do something based on every Cell in a Row. See that method for specific
133
+ * examples.
134
+ *
135
+ * @param cellId The Id of the Cell that the callback can operate on.
136
+ * @param cell The value of the Cell.
137
+ * @category Callback
138
+ */
139
+ export type CellCallback = (cellId: Id, cell: Cell) => void;
140
+
141
+ /**
142
+ * The MapCell type describes a function that takes an existing Cell value and
143
+ * returns another.
144
+ *
145
+ * A MapCell can be provided in the setCell method to map an existing value to a
146
+ * new one, such as when incrementing a number. See that method for specific
147
+ * examples.
148
+ *
149
+ * @param cell The current value of the Cell to map to a new value.
150
+ * @category Callback
151
+ */
152
+ export type MapCell = (cell: Cell | undefined) => Cell;
153
+
154
+ /**
155
+ * The GetCell type describes a function that takes a Id and returns the Cell
156
+ * value for a particular Row.
157
+ *
158
+ * A GetCell can be provided when setting definitions, as in the
159
+ * setMetricDefinition method of a Metrics object, or the setIndexDefinition
160
+ * method of an Indexes object. See those methods for specific examples.
161
+ *
162
+ * @param cellId The Id of the Cell to fetch the value for.
163
+ * @category Callback
164
+ */
165
+ export type GetCell = (cellId: Id) => Cell | undefined;
166
+
167
+ /**
168
+ * The TablesListener type describes a function that is used to listen to
169
+ * changes to the whole Store.
170
+ *
171
+ * A TablesListener is provided when using the addTablesListener method. See
172
+ * that method for specific examples.
173
+ *
174
+ * When called, a TablesListener is given a reference to the Store and a
175
+ * GetCellChange function that can be used to query Cell values before and after
176
+ * the current transaction.
177
+ *
178
+ * Note that if the listener was manually forced to be called (with the
179
+ * callListener method rather than due to a real change in the Store), the
180
+ * GetCellChange function will not be present.
181
+ *
182
+ * @param store A reference to the Store that changed.
183
+ * @param getCellChange A function that returns information about any Cell's
184
+ * changes.
185
+ * @category Listener
186
+ */
187
+ export type TablesListener = (
188
+ store: Store,
189
+ getCellChange: GetCellChange | undefined,
190
+ ) => void;
191
+
192
+ /**
193
+ * The TableIdsListener type describes a function that is used to listen to
194
+ * changes to the Table Ids in the Store.
195
+ *
196
+ * A TableIdsListener is provided when using the addTableIdsListener method. See
197
+ * that method for specific examples.
198
+ *
199
+ * When called, a TableIdsListener is given a reference to the Store.
200
+ *
201
+ * @param store A reference to the Store that changed.
202
+ * @category Listener
203
+ */
204
+ export type TableIdsListener = (store: Store) => void;
205
+
206
+ /**
207
+ * The TableListener type describes a function that is used to listen to changes
208
+ * to a Table.
209
+ *
210
+ * A TableListener is provided when using the addTableListener method. See that
211
+ * method for specific examples.
212
+ *
213
+ * When called, a TableListener is given a reference to the Store, the Id of the
214
+ * Table that changed, and a GetCellChange function that can be used to query
215
+ * Cell values before and after the current transaction.
216
+ *
217
+ * Note that if the listener was manually forced to be called (with the
218
+ * callListener method rather than due to a real change in the Store), the
219
+ * GetCellChange function will not be present.
220
+ *
221
+ * @param store A reference to the Store that changed.
222
+ * @param tableId The Id of the Table that changed.
223
+ * @param getCellChange A function that returns information about any Cell's
224
+ * changes.
225
+ * @category Listener
226
+ */
227
+ export type TableListener = (
228
+ store: Store,
229
+ tableId: Id,
230
+ getCellChange: GetCellChange | undefined,
231
+ ) => void;
232
+
233
+ /**
234
+ * The RowIdsListener type describes a function that is used to listen to
235
+ * changes to the Row Ids in a Table.
236
+ *
237
+ * A RowIdsListener is provided when using the addRowIdsListener method. See
238
+ * that method for specific examples.
239
+ *
240
+ * When called, a RowIdsListener is given a reference to the Store, and the Id
241
+ * of the Table whose Row Ids changed.
242
+ *
243
+ * @param store A reference to the Store that changed.
244
+ * @param tableId The Id of the Table that changed.
245
+ * @category Listener
246
+ */
247
+ export type RowIdsListener = (store: Store, tableId: Id) => void;
248
+
249
+ /**
250
+ * The RowListener type describes a function that is used to listen to changes
251
+ * to a Row.
252
+ *
253
+ * A RowListener is provided when using the addRowListener method. See that
254
+ * method for specific examples.
255
+ *
256
+ * When called, a RowListener is given a reference to the Store, the Id of the
257
+ * Table that changed, the Id of the Row that changed, and a GetCellChange
258
+ * function that can be used to query Cell values before and after the current
259
+ * transaction.
260
+ *
261
+ * Note that if the listener was manually forced to be called (with the
262
+ * callListener method rather than due to a real change in the Store), the
263
+ * GetCellChange function will not be present.
264
+ *
265
+ * @param store A reference to the Store that changed.
266
+ * @param tableId The Id of the Table that changed.
267
+ * @param rowId The Id of the Row that changed.
268
+ * @param getCellChange A function that returns information about any Cell's
269
+ * changes.
270
+ * @category Listener
271
+ */
272
+ export type RowListener = (
273
+ store: Store,
274
+ tableId: Id,
275
+ rowId: Id,
276
+ getCellChange: GetCellChange | undefined,
277
+ ) => void;
278
+
279
+ /**
280
+ * The CellIdsListener type describes a function that is used to listen to
281
+ * changes to the Cell Ids in a Row.
282
+ *
283
+ * A CellIdsListener is provided when using the addCellIdsListener method. See
284
+ * that method for specific examples.
285
+ *
286
+ * When called, a CellIdsListener is given a reference to the Store, the Id of
287
+ * the Table that changed, and the Id of the Row whose Cell Ids changed.
288
+ *
289
+ * @param store A reference to the Store that changed.
290
+ * @param tableId The Id of the Table that changed.
291
+ * @param rowId The Id of the Row that changed.
292
+ * changes.
293
+ * @category Listener
294
+ */
295
+ export type CellIdsListener = (store: Store, tableId: Id, rowId: Id) => void;
296
+
297
+ /**
298
+ * The CellListener type describes a function that is used to listen to changes
299
+ * to a Cell.
300
+ *
301
+ * A CellListener is provided when using the addCellListener method. See that
302
+ * method for specific examples.
303
+ *
304
+ * When called, a CellListener is given a reference to the Store, the Id of the
305
+ * Table that changed, the Id of the Row that changed, and the Id of Cell that
306
+ * changed. It is also given the new value of the Cell, the old value of the
307
+ * Cell, and a GetCellChange function that can be used to query Cell values
308
+ * before and after the current transaction.
309
+ *
310
+ * Note that if the listener was manually forced to be called (with the
311
+ * callListener method rather than due to a real change in the Store), the
312
+ * GetCellChange function will not be present and the new and old values of the
313
+ * Cell will be the same.
314
+ *
315
+ * @param store A reference to the Store that changed.
316
+ * @param tableId The Id of the Table that changed.
317
+ * @param rowId The Id of the Row that changed.
318
+ * @param cellId The Id of the Cell that changed.
319
+ * @param newCell The new value of the Cell that changed.
320
+ * @param oldCell The old value of the Cell that changed.
321
+ * @param getCellChange A function that returns information about any Cell's
322
+ * changes.
323
+ * @category Listener
324
+ */
325
+ export type CellListener = (
326
+ store: Store,
327
+ tableId: Id,
328
+ rowId: Id,
329
+ cellId: Id,
330
+ newCell: Cell,
331
+ oldCell: Cell,
332
+ getCellChange: GetCellChange | undefined,
333
+ ) => void;
334
+
335
+ /**
336
+ * The InvalidCellListener type describes a function that is used to listen to
337
+ * attempts to set invalid data to a Cell.
338
+ *
339
+ * A InvalidCellListener is provided when using the addInvalidCellListener
340
+ * method. See that method for specific examples.
341
+ *
342
+ * When called, a InvalidCellListener is given a reference to the Store, the Id
343
+ * of the Table, the Id of the Row, and the Id of Cell that were being attempted
344
+ * to be changed. It is also given the invalid value of the Cell, which could
345
+ * have been of absolutely any type. Since there could have been multiple failed
346
+ * attempts to set the Cell within a single transaction, this is an array
347
+ * containing each attempt, chronologically.
348
+ *
349
+ * @param store A reference to the Store that was being changed.
350
+ * @param tableId The Id of the Table that was being changed.
351
+ * @param rowId The Id of the Row that was being changed.
352
+ * @param cellId The Id of the Cell that was being changed.
353
+ * @param invalidCells An array of the values of the Cell that were invalid.
354
+ * @category Listener
355
+ */
356
+ export type InvalidCellListener = (
357
+ store: Store,
358
+ tableId: Id,
359
+ rowId: Id,
360
+ cellId: Id,
361
+ invalidCells: any[],
362
+ ) => void;
363
+
364
+ /**
365
+ * The GetCellChange type describes a function that returns information about
366
+ * any Cell's changes during a transaction.
367
+ *
368
+ * A GetCellChange function is provided to every listener when called due the
369
+ * Store changing. The listener can then fetch the previous value of a Cell
370
+ * before the current transaction, the new value after it, and a convenience
371
+ * flag that indicates that the value has changed.
372
+ *
373
+ * @param tableId The Id of the Table to inspect.
374
+ * @param rowId The Id of the Row to inspect.
375
+ * @param cellId The Id of the Cell to inspect.
376
+ * @returns A CellChange array containing information about the Cell's changes.
377
+ * @category Listener
378
+ */
379
+ export type GetCellChange = (tableId: Id, rowId: Id, cellId: Id) => CellChange;
380
+
381
+ /**
382
+ * The CellChange type describes a Cell's changes during a transaction.
383
+ *
384
+ * This is returned by the GetCellChange function that is provided to every
385
+ * listener when called. This array contains the previous value of a Cell
386
+ * before the current transaction, the new value after it, and a convenience
387
+ * flag that indicates that the value has changed.
388
+ *
389
+ * @category Listener
390
+ */
391
+ export type CellChange = [
392
+ changed: boolean,
393
+ oldCell: Cell | undefined,
394
+ newCell: Cell | undefined,
395
+ ];
396
+
397
+ /**
398
+ * The Schema type describes the structure of a Store in terms of valid Table
399
+ * Ids and the types of Cell that can exist within them.
400
+ *
401
+ * A Schema comprises a JavaScript object describing each Table, in turn a
402
+ * nested JavaScript object containing information about each Cell and its
403
+ * CellSchema. It is provided to the setSchema method.
404
+ *
405
+ * @example
406
+ * When applied to a Store, this Schema only allows one Table called `pets`, in
407
+ * which each Row may contain a string `species` Cell, and is guaranteed to
408
+ * contain a boolean `sold` Cell that defaults to `false`.
409
+ *
410
+ *```js
411
+ * const schema: Schema = {
412
+ * pets: {
413
+ * species: {type: 'string'},
414
+ * sold: {type: 'boolean', default: false},
415
+ * },
416
+ * };
417
+ * ```
418
+ * @category Schema
419
+ */
420
+ export type Schema = {
421
+ [tableId: Id]: {
422
+ [cellId: Id]: CellSchema;
423
+ };
424
+ };
425
+
426
+ /**
427
+ * The CellSchema type describes what values are allowed for each Cell in a
428
+ * Table.
429
+ *
430
+ * A CellSchema specifies the type of the Cell (`string`, `boolean`, or
431
+ * `number`), and what the default value can be when an explicit value is not
432
+ * specified.
433
+ *
434
+ * If a default value is provided (and its type is correct), you can be certain
435
+ * that that Cell will always be present in a Row.
436
+ *
437
+ * If the default value is _not_ provided (or its type is incorrect), the Cell
438
+ * may be missing from the Row, but when present you can be guaranteed it is of
439
+ * the correct type.
440
+ *
441
+ * @example
442
+ * When applied to a Store, this CellSchema ensures a boolean Cell is always
443
+ * present, and defaults it to `false`.
444
+ *
445
+ *```js
446
+ * const requiredBoolean: CellSchema = {type: 'boolean', default: false};
447
+ * ```
448
+ * @category Schema
449
+ */
450
+ export type CellSchema =
451
+ | {
452
+ type: 'string';
453
+ default?: string;
454
+ }
455
+ | {
456
+ type: 'number';
457
+ default?: number;
458
+ }
459
+ | {
460
+ type: 'boolean';
461
+ default?: boolean;
462
+ };
463
+
464
+ /**
465
+ * The StoreListenerStats type describes the number of listeners registered with
466
+ * the Store, and can be used for debugging purposes.
467
+ *
468
+ * The StoreListenerStats object contains a breakdown of the different types of
469
+ * listener. Totals include both mutator and non-mutator listeners. A
470
+ * StoreListenerStats object is returned from the getListenerStats method, and
471
+ * is only populated in a debug build.
472
+ *
473
+ * @category Development
474
+ */
475
+ export type StoreListenerStats = {
476
+ /**
477
+ * The number of TablesListeners registered with the Store.
478
+ */
479
+ tables?: number;
480
+ /**
481
+ * The number of TableIdsListeners registered with the Store.
482
+ */
483
+ tableIds?: number;
484
+ /**
485
+ * The number of TableListeners registered with the Store.
486
+ */
487
+ table?: number;
488
+ /**
489
+ * The number of RowIdsListeners registered with the Store.
490
+ */
491
+ rowIds?: number;
492
+ /**
493
+ * The number of RowListeners registered with the Store.
494
+ */
495
+ row?: number;
496
+ /**
497
+ * The number of CellIdsListeners registered with the Store.
498
+ */
499
+ cellIds?: number;
500
+ /**
501
+ * The number of CellListeners registered with the Store.
502
+ */
503
+ cell?: number;
504
+ };
505
+
506
+ /**
507
+ * A Store is the main location for keeping structured state and tabular data.
508
+ *
509
+ * Create a Store easily with the createStore function. From there, you can set
510
+ * and get data, add listeners for when the data changes, set a Schema, and so
511
+ * on.
512
+ *
513
+ * A Store has a simple hierarchical structure:
514
+ *
515
+ * - The Store contains a number of Table objects.
516
+ * - Each Table contains a number of Row objects.
517
+ * - Each Row contains a number of Cell objects.
518
+ *
519
+ * A Cell is a string, boolean, or number value.
520
+ *
521
+ * The members of each level of this hierarchy are identified with a unique Id
522
+ * (which is a string). In other words you can naively think of a Store as a
523
+ * three-level-deep JavaScript object, keyed with strings:
524
+ *
525
+ * ```json
526
+ * { // Store
527
+ * "table1": { // Table
528
+ * "row1": { // Row
529
+ * "cell1": "one", // Cell (string)
530
+ * "cell2": true, // Cell (boolean)
531
+ * "cell3": 3, // Cell (number)
532
+ * ...
533
+ * },
534
+ * ...
535
+ * },
536
+ * ...
537
+ * }
538
+ * ```
539
+ *
540
+ * In its default form, a Store has no sense of a structured schema, so, as long
541
+ * as they are unique within their own parent, the Id keys can each be any
542
+ * string you want. However, you _can_ optionally specify a Schema for a Store,
543
+ * which then usefully constrains the Table and Cell Ids (and Cell values) you
544
+ * can use.
545
+ *
546
+ * # Setting and getting data
547
+ *
548
+ * Every part of the Store can be accessed with getter methods. When you
549
+ * retrieve data from the Store, you are receiving a copy - rather than a
550
+ * reference - of it. This means that manipulating the data in the Store must be
551
+ * performed with the equivalent setter and deleter methods.
552
+ *
553
+ * To benefit from the reactive behavior of the Store, you can also subscribe to
554
+ * changes on any part of it with 'listeners'. Registering a listener returns a
555
+ * listener Id (that you can use later to remove it with the delListener
556
+ * method), and it will then be called every time there is a change within the
557
+ * part of the hierarchy you're listening to.
558
+ *
559
+ * This table shows the main ways you can set, get, and listen to, different
560
+ * types of data in a Store:
561
+ *
562
+ * |Type|Get data|Set data|Delete data|Add a listener|
563
+ * |-|-|-|-|-|
564
+ * |Tables|getTables|setTables|delTables|addTablesListener|
565
+ * |Table Ids|getTableIds|-|-|addTableIdsListener|
566
+ * |Table|getTable|setTable|delTable|addTableListener|
567
+ * |Row Ids|getRowIds|-|-|addRowIdsListener|
568
+ * |Row|getRow|setRow|delRow|addRowListener|
569
+ * |Cell Ids|getCellIds|-|-|addCellIdsListener|
570
+ * |Cell|getCell|setCell|delCell|addCellListener|
571
+ *
572
+ * Additionally, there are two extra methods to manipulate Row objects. The
573
+ * addRow method is like the setRow method but automatically assigns it a new
574
+ * unique Id. And the setPartialRow method lets you update multiple Cell values
575
+ * in a Row without affecting the others.
576
+ *
577
+ * You can listen to attempts to write invalid data to a Cell with the
578
+ * addInvalidCellListener method.
579
+ *
580
+ * The transaction method is used to wrap multiple changes to the Store so that
581
+ * the relevant listeners only fire once.
582
+ *
583
+ * The setJson method and the getJson method allow you to work with a
584
+ * JSON-encoded representation of the entire Store, which is useful for
585
+ * persisting it.
586
+ *
587
+ * Finally, the callListener method provides a way for you to manually provoke a
588
+ * listener to be called, even if the underlying data hasn't changed. This is
589
+ * useful when you are using mutator listeners to guarantee that data conforms
590
+ * to programmatic conditions, and those conditions change such that you need to
591
+ * update the Store in bulk.
592
+ *
593
+ * Read more about setting and changing data in The Basics guides, and about
594
+ * listeners in the Listening to Stores guide.
595
+ *
596
+ * # Creating a Schema
597
+ *
598
+ * You can set a Schema on a Store when you create it with createStore function,
599
+ * or at a later stage with the setSchema method. A Schema constrains the Table
600
+ * Ids the Store can have, and the types of Cell data in each Table. Each Cell
601
+ * requires its type to be specified, and can also take a default value for when
602
+ * it's not specified.
603
+ *
604
+ * You can also get a serialization of the Schema out of the Store with the
605
+ * getSchemaJson method, and remove the Schema altogether with the delSchema
606
+ * method.
607
+ *
608
+ * Read more about schemas in the Using Schemas guide.
609
+ *
610
+ * # Convenience methods
611
+ *
612
+ * There are a few additional helper methods to make it easier to work with a
613
+ * Store. There are methods for easily checking the existence of a Table, Row,
614
+ * or Cell, and iterators that let you act on the children of a common parent:
615
+ *
616
+ * ||Checking existence|Iterator|
617
+ * |-|-|-|
618
+ * |Table|hasTable|forEachTable|
619
+ * |Row|hasRow|forEachRow|
620
+ * |Cell|hasCell|forEachCell|
621
+ *
622
+ * Finally, the getListenerStats method describes the current state of the
623
+ * Store's listeners for debugging purposes.
624
+ *
625
+ * @example
626
+ * This example shows a very simple lifecycle of a Store: from creation, to
627
+ * adding and getting some data, and then registering and removing a listener.
628
+ *
629
+ * ```js
630
+ * const store = createStore().setTables({pets: {fido: {species: 'dog'}}});
631
+ * console.log(store.getRow('pets', 'fido'));
632
+ * // -> {species: 'dog'}
633
+ *
634
+ * store.setCell('pets', 'fido', 'color', 'brown');
635
+ * console.log(store.getCell('pets', 'fido', 'color'));
636
+ * // -> 'brown'
637
+ *
638
+ * const listenerId = store.addTableListener('pets', () => {
639
+ * console.log('changed');
640
+ * });
641
+ *
642
+ * store.setCell('pets', 'fido', 'sold', false);
643
+ * // -> 'changed'
644
+ *
645
+ * store.delListener(listenerId);
646
+ * ```
647
+ * @see The Basics guides
648
+ * @see Using Schemas guides
649
+ * @see Hello World demos
650
+ * @see Todo App demos
651
+ * @category Store
652
+ */
653
+ export interface Store {
654
+ /**
655
+ * The getTables method returns a Tables object containing the entire data of
656
+ * the Store.
657
+ *
658
+ * Note that this returns a copy of, rather than a reference to the underlying
659
+ * data, so changes made to the returned object are not made to the Store
660
+ * itself.
661
+ *
662
+ * @returns A Tables object containing the entire data of the Store.
663
+ * @example
664
+ * This example retrieves the data in a Store.
665
+ *
666
+ * ```js
667
+ * const store = createStore().setTables({
668
+ * pets: {fido: {species: 'dog'}},
669
+ * species: {dog: {price: 5}},
670
+ * });
671
+ * console.log(store.getTables());
672
+ * // -> {pets: {fido: {species: 'dog'}}, species: {dog: {price: 5}}}
673
+ * ```
674
+ * @example
675
+ * This example retrieves the Tables of an empty Store, returning an empty
676
+ * object.
677
+ *
678
+ * ```js
679
+ * const store = createStore();
680
+ * console.log(store.getTables());
681
+ * // -> {}
682
+ * ```
683
+ * @see
684
+ * # Guides
685
+ * Creating a Store
686
+ * @see Indexes
687
+ * @category Getter
688
+ */
689
+ getTables(): Tables;
690
+
691
+ /**
692
+ * The getTableIds method returns the Ids of every Table in the Store.
693
+ *
694
+ * Note that this returns a copy of, rather than a reference, to the list of
695
+ * Ids, so changes made to the list are not made to the Store itself. Although
696
+ * the order of Ids have no meaning, this method is expected to return them in
697
+ * the order in which each Table was added.
698
+ *
699
+ * @returns An array of the Ids of every Table in the Store.
700
+ * @example
701
+ * This example retrieves the Table Ids in a Store.
702
+ *
703
+ * ```js
704
+ * const store = createStore().setTables({
705
+ * pets: {fido: {species: 'dog'}},
706
+ * species: {dog: {price: 5}},
707
+ * });
708
+ * console.log(store.getTableIds());
709
+ * // -> ['pets', 'species']
710
+ * ```
711
+ * @example
712
+ * This example retrieves the Table Ids of an empty Store, returning an empty
713
+ * array.
714
+ *
715
+ * ```js
716
+ * const store = createStore();
717
+ * console.log(store.getTableIds());
718
+ * // -> []
719
+ * ```
720
+ * @category Getter
721
+ */
722
+ getTableIds(): Ids;
723
+
724
+ /**
725
+ * The getTable method returns an object containing the entire data of a
726
+ * single Table in the Store.
727
+ *
728
+ * Note that this returns a copy of, rather than a reference to the underlying
729
+ * data, so changes made to the returned object are not made to the Store
730
+ * itself.
731
+ *
732
+ * @param tableId The Id of the Table in the Store.
733
+ * @returns An object containing the entire data of the Table.
734
+ * @example
735
+ * This example retrieves the data in a single Table.
736
+ *
737
+ * ```js
738
+ * const store = createStore().setTables({
739
+ * pets: {fido: {species: 'dog'}},
740
+ * species: {dog: {price: 5}},
741
+ * });
742
+ * console.log(store.getTable('pets'));
743
+ * // -> {fido: {species: 'dog'}}
744
+ * ```
745
+ * @example
746
+ * This example retrieves a Table that does not exist, returning an empty
747
+ * object.
748
+ *
749
+ * ```js
750
+ * const store = createStore().setTables({pets: {fido: {species: 'dog'}}});
751
+ * console.log(store.getTable('employees'));
752
+ * // -> {}
753
+ * ```
754
+ * @category Getter
755
+ */
756
+ getTable(tableId: Id): Table;
757
+
758
+ /**
759
+ * The getRowIds method returns the Ids of every Row in a given Table.
760
+ *
761
+ * Note that this returns a copy of, rather than a reference, to the list of
762
+ * Ids, so changes made to the list are not made to the Store itself. Although
763
+ * the order of Ids have no meaning, this method is expected to return them in
764
+ * the order in which each Row was added.
765
+ *
766
+ * @param tableId The Id of the Table in the Store.
767
+ * @returns An array of the Ids of every Row in the Table.
768
+ * @example
769
+ * This example retrieves the Row Ids in a Table.
770
+ *
771
+ * ```js
772
+ * const store = createStore().setTables({
773
+ * pets: {
774
+ * fido: {species: 'dog'},
775
+ * felix: {species: 'cat'},
776
+ * },
777
+ * });
778
+ * console.log(store.getRowIds('pets'));
779
+ * // -> ['fido', 'felix']
780
+ * ```
781
+ * @example
782
+ * This example retrieves the Row Ids of a Table that does not exist,
783
+ * returning an empty array.
784
+ *
785
+ * ```js
786
+ * const store = createStore().setTables({pets: {fido: {species: 'dog'}}});
787
+ * console.log(store.getRowIds('employees'));
788
+ * // -> []
789
+ * ```
790
+ * @category Getter
791
+ */
792
+ getRowIds(tableId: Id): Ids;
793
+
794
+ /**
795
+ * The getRow method returns an object containing the entire data of a single
796
+ * Row in a given Table.
797
+ *
798
+ * Note that this returns a copy of, rather than a reference to the underlying
799
+ * data, so changes made to the returned object are not made to the Store
800
+ * itself.
801
+ *
802
+ * @param tableId The Id of the Table in the Store.
803
+ * @param rowId The Id of the Row in the Table.
804
+ * @returns An object containing the entire data of the Row.
805
+ * @example
806
+ * This example retrieves the data in a single Row.
807
+ *
808
+ * ```js
809
+ * const store = createStore().setTables({
810
+ * pets: {
811
+ * fido: {species: 'dog'},
812
+ * felix: {species: 'cat'},
813
+ * },
814
+ * });
815
+ * console.log(store.getRow('pets', 'fido'));
816
+ * // -> {species: 'dog'}
817
+ * ```
818
+ * @example
819
+ * This example retrieves a Row that does not exist, returning an empty
820
+ * object.
821
+ *
822
+ * ```js
823
+ * const store = createStore().setTables({pets: {fido: {species: 'dog'}}});
824
+ * console.log(store.getRow('pets', 'felix'));
825
+ * // -> {}
826
+ * ```
827
+ * @category Getter
828
+ */
829
+ getRow(tableId: Id, rowId: Id): Row;
830
+
831
+ /**
832
+ * The getCellIds method returns the Ids of every Cell in a given Row, in a
833
+ * given Table.
834
+ *
835
+ * Note that this returns a copy of, rather than a reference, to the list of
836
+ * Ids, so changes made to the list are not made to the Store itself. Although
837
+ * the order of Ids have no meaning, this method is expected to return them in
838
+ * the order in which each Row was added.
839
+ *
840
+ * @param tableId The Id of the Table in the Store.
841
+ * @param rowId The Id of the Row in the Table.
842
+ * @returns An array of the Ids of every Cell in the Row.
843
+ * @example
844
+ * This example retrieves the Cell Ids in a Row.
845
+ *
846
+ * ```js
847
+ * const store = createStore().setTables({
848
+ * pets: {
849
+ * fido: {species: 'dog', color: 'brown'},
850
+ * },
851
+ * });
852
+ * console.log(store.getCellIds('pets', 'fido'));
853
+ * // -> ['species', 'color']
854
+ * ```
855
+ * @example
856
+ * This example retrieves the Cell Ids of a Cell that does not exist,
857
+ * returning an empty array.
858
+ *
859
+ * ```js
860
+ * const store = createStore().setTables({pets: {fido: {species: 'dog'}}});
861
+ * console.log(store.getCellIds('pets', 'felix'));
862
+ * // -> []
863
+ * ```
864
+ * @category Getter
865
+ */
866
+ getCellIds(tableId: Id, rowId: Id): Ids;
867
+
868
+ /**
869
+ * The getCell method returns an object containing the value of a single Cell
870
+ * in a given Row, in a given Table.
871
+ *
872
+ * Note that this returns a copy of, rather than a reference to the underlying
873
+ * data, so changes made to the returned object are not made to the Store
874
+ * itself.
875
+ *
876
+ * @param tableId The Id of the Table in the Store.
877
+ * @param rowId The Id of the Row in the Table.
878
+ * @param cellId The Id of the Cell in the Row.
879
+ * @returns The value of the Cell.
880
+ * @example
881
+ * This example retrieves a single Cell.
882
+ *
883
+ * ```js
884
+ * const store = createStore().setTables({
885
+ * pets: {fido: {species: 'dog', color: 'brown'}},
886
+ * });
887
+ * console.log(store.getCell('pets', 'fido', 'species'));
888
+ * // -> 'dog'
889
+ * ```
890
+ * @example
891
+ * This example retrieves a Cell that does not exist, returning `undefined`.
892
+ *
893
+ * ```js
894
+ * const store = createStore().setTables({pets: {fido: {species: 'dog'}}});
895
+ * console.log(store.getCell('pets', 'fido', 'color'));
896
+ * // -> undefined
897
+ * ```
898
+ * @category Getter
899
+ */
900
+ getCell(tableId: Id, rowId: Id, cellId: Id): Cell | undefined;
901
+
902
+ /**
903
+ * The hasTables method returns a boolean indicating whether any Table objects
904
+ * exist in the Store.
905
+ *
906
+ * @returns Whether any Tables exist.
907
+ * @example
908
+ * This example shows simple existence checks.
909
+ *
910
+ * ```js
911
+ * const store = createStore();
912
+ * console.log(store.hasTables());
913
+ * // -> false
914
+ * store.setTables({pets: {fido: {species: 'dog'}}});
915
+ * console.log(store.hasTables());
916
+ * // -> true
917
+ * ```
918
+ * @category Getter
919
+ */
920
+ hasTables(): boolean;
921
+
922
+ /**
923
+ * The hasTable method returns a boolean indicating whether a given Table
924
+ * exists in the Store.
925
+ *
926
+ * @param tableId The Id of a possible Table in the Store.
927
+ * @returns Whether a Table with that Id exists.
928
+ * @example
929
+ * This example shows two simple Table existence checks.
930
+ *
931
+ * ```js
932
+ * const store = createStore().setTables({pets: {fido: {species: 'dog'}}});
933
+ * console.log(store.hasTable('pets'));
934
+ * // -> true
935
+ * console.log(store.hasTable('employees'));
936
+ * // -> false
937
+ * ```
938
+ * @category Getter
939
+ */
940
+ hasTable(tableId: Id): boolean;
941
+
942
+ /**
943
+ * The hasRow method returns a boolean indicating whether a given Row exists
944
+ * in the Store.
945
+ *
946
+ * @param tableId The Id of a possible Table in the Store.
947
+ * @param rowId The Id of a possible Row in the Table.
948
+ * @returns Whether a Row with that Id exists in that Table.
949
+ * @example
950
+ * This example shows two simple Row existence checks.
951
+ *
952
+ * ```js
953
+ * const store = createStore().setTables({pets: {fido: {species: 'dog'}}});
954
+ * console.log(store.hasRow('pets', 'fido'));
955
+ * // -> true
956
+ * console.log(store.hasRow('pets', 'felix'));
957
+ * // -> false
958
+ * ```
959
+ * @category Getter
960
+ */
961
+ hasRow(tableId: Id, rowId: Id): boolean;
962
+
963
+ /**
964
+ * The hasCell method returns a boolean indicating whether a given Cell exists
965
+ * in the Store.
966
+ *
967
+ * @param tableId The Id of a possible Table in the Store.
968
+ * @param rowId The Id of a possible Row in the Table.
969
+ * @param cellId The Id of a possible Cell in the Row.
970
+ * @returns Whether a Cell with that Id exists in that Row in that Table.
971
+ * @example
972
+ * This example shows two simple Cell existence checks.
973
+ *
974
+ * ```js
975
+ * const store = createStore().setTables({pets: {fido: {species: 'dog'}}});
976
+ * console.log(store.hasCell('pets', 'fido', 'species'));
977
+ * // -> true
978
+ * console.log(store.hasCell('pets', 'fido', 'color'));
979
+ * // -> false
980
+ * ```
981
+ * @category Getter
982
+ */
983
+ hasCell(tableId: Id, rowId: Id, cellId: Id): boolean;
984
+
985
+ /**
986
+ * The getJson method returns a string serialization of all of the Tables in
987
+ * the Store.
988
+ *
989
+ * @returns A string serialization of all of the Tables in the Store.
990
+ * @example
991
+ * This example serializes the contents of a Store.
992
+ *
993
+ * ```js
994
+ * const store = createStore().setTables({pets: {fido: {species: 'dog'}}});
995
+ * console.log(store.getJson());
996
+ * // -> '{"pets":{"fido":{"species":"dog"}}}'
997
+ * ```
998
+ * @example
999
+ * This example serializes the contents of an empty Store.
1000
+ *
1001
+ * ```js
1002
+ * const store = createStore();
1003
+ * console.log(store.getJson());
1004
+ * // -> '{}'
1005
+ * ```
1006
+ * @category Getter
1007
+ */
1008
+ getJson(): Json;
1009
+
1010
+ /**
1011
+ * The getSchemaJson method returns a string serialization of the Schema of
1012
+ * the Store.
1013
+ *
1014
+ * If no Schema has been set on the Store (or if it has been removed with the
1015
+ * delSchema method), then it will return the serialization of an empty
1016
+ * object, `{}`.
1017
+ *
1018
+ * @returns A string serialization of the Schema of the Store.
1019
+ * @example
1020
+ * This example serializes the Schema of a Store.
1021
+ *
1022
+ * ```js
1023
+ * const store = createStore().setSchema({
1024
+ * pets: {
1025
+ * species: {type: 'string'},
1026
+ * sold: {type: 'boolean'},
1027
+ * },
1028
+ * });
1029
+ * console.log(store.getSchemaJson());
1030
+ * // -> '{"pets":{"species":{"type":"string"},"sold":{"type":"boolean"}}}'
1031
+ * ```
1032
+ * @example
1033
+ * This example serializes the Schema of an empty Store.
1034
+ *
1035
+ * ```js
1036
+ * const store = createStore();
1037
+ * console.log(store.getSchemaJson());
1038
+ * // -> '{}'
1039
+ * ```
1040
+ * @category Getter
1041
+ */
1042
+ getSchemaJson(): Json;
1043
+
1044
+ /**
1045
+ * The setTables method takes an object and sets the entire data of the Store.
1046
+ *
1047
+ * This method will cause listeners to be called for any Table, Row, Cell, or
1048
+ * Id changes resulting from it.
1049
+ *
1050
+ * Any part of the provided object that is invalid (either according to the
1051
+ * Tables type, or because it does not match a Schema associated with the
1052
+ * Store), will be ignored silently.
1053
+ *
1054
+ * Assuming that at least some of the provided Tables object is valid, any
1055
+ * data that was already present in the Store will be completely overwritten.
1056
+ * If the object is completely invalid, no change will be made to the Store.
1057
+ *
1058
+ * The method returns a reference to the Store to that subsequent operations
1059
+ * can be chained in a fluent style.
1060
+ *
1061
+ * @param tables The data of the Store to be set.
1062
+ * @example
1063
+ * This example sets the data of a Store.
1064
+ *
1065
+ * ```js
1066
+ * const store = createStore().setTables({
1067
+ * pets: {fido: {species: 'dog'}},
1068
+ * species: {dog: {price: 5}},
1069
+ * });
1070
+ * console.log(store.getTables());
1071
+ * // -> {pets: {fido: {species: 'dog'}}, species: {dog: {price: 5}}}
1072
+ * ```
1073
+ * @example
1074
+ * This example attempts to set the data of an existing Store with partly
1075
+ * invalid, and then completely invalid, Tables objects.
1076
+ *
1077
+ * ```js
1078
+ * const store = createStore().setTables({pets: {fido: {species: 'dog'}}});
1079
+ *
1080
+ * store.setTables({pets: {felix: {species: 'cat', bug: []}}});
1081
+ * console.log(store.getTables());
1082
+ * // -> {pets: {felix: {species: 'cat'}}}
1083
+ *
1084
+ * store.setTables({meaning: 42});
1085
+ * console.log(store.getTables());
1086
+ * // -> {pets: {felix: {species: 'cat'}}}
1087
+ * ```
1088
+ * @category Setter
1089
+ */
1090
+ setTables(tables: Tables): Store;
1091
+
1092
+ /**
1093
+ * The setTable method takes an object and sets the entire data of a single
1094
+ * Table in the Store.
1095
+ *
1096
+ * This method will cause listeners to be called for any Table, Row, Cell, or
1097
+ * Id changes resulting from it.
1098
+ *
1099
+ * Any part of the provided object that is invalid (either according to the
1100
+ * Table type, or because it does not match a Schema associated with the
1101
+ * Store), will be ignored silently.
1102
+ *
1103
+ * Assuming that at least some of the provided Table object is valid, any data
1104
+ * that was already present in the Store for that Table will be completely
1105
+ * overwritten. If the object is completely invalid, no change will be made to
1106
+ * the Store.
1107
+ *
1108
+ * The method returns a reference to the Store to that subsequent operations
1109
+ * can be chained in a fluent style.
1110
+ *
1111
+ * @param tableId The Id of the Table in the Store.
1112
+ * @param table The data of a single Table to be set.
1113
+ * @example
1114
+ * This example sets the data of a single Table.
1115
+ *
1116
+ * ```js
1117
+ * const store = createStore().setTable('pets', {
1118
+ * fido: {species: 'dog'},
1119
+ * felix: {species: 'cat'},
1120
+ * });
1121
+ * console.log(store.getTables());
1122
+ * // -> {pets: {fido: {species: 'dog'}, felix: {species: 'cat'}}}
1123
+ * ```
1124
+ * @example
1125
+ * This example attempts to set the data of an existing Store with partly
1126
+ * invalid, and then completely invalid, Table objects.
1127
+ *
1128
+ * ```js
1129
+ * const store = createStore().setTables({pets: {fido: {species: 'dog'}}});
1130
+ *
1131
+ * store.setTable('pets', {felix: {species: 'cat', bug: []}});
1132
+ * console.log(store.getTables());
1133
+ * // -> {pets: {felix: {species: 'cat'}}}
1134
+ *
1135
+ * store.setTable('pets', {meaning: 42});
1136
+ * console.log(store.getTables());
1137
+ * // -> {pets: {felix: {species: 'cat'}}}
1138
+ * ```
1139
+ * @category Setter
1140
+ */
1141
+ setTable(tableId: Id, table: Table): Store;
1142
+
1143
+ /**
1144
+ * The setRow method takes an object and sets the entire data of a single Row
1145
+ * in the Store.
1146
+ *
1147
+ * This method will cause listeners to be called for any Table, Row, Cell, or
1148
+ * Id changes resulting from it.
1149
+ *
1150
+ * Any part of the provided object that is invalid (either according to the
1151
+ * Row type, or because it does not match a Schema associated with the Store),
1152
+ * will be ignored silently.
1153
+ *
1154
+ * Assuming that at least some of the provided Row object is valid, any data
1155
+ * that was already present in the Store for that Row will be completely
1156
+ * overwritten. If the object is completely invalid, no change will be made to
1157
+ * the Store.
1158
+ *
1159
+ * The method returns a reference to the Store to that subsequent operations
1160
+ * can be chained in a fluent style.
1161
+ *
1162
+ * @param tableId The Id of the Table in the Store.
1163
+ * @param rowId The Id of the Row in the Table.
1164
+ * @param row The data of a single Row to be set.
1165
+ * @returns A reference to the Store.
1166
+ * @example
1167
+ * This example sets the data of a single Row.
1168
+ *
1169
+ * ```js
1170
+ * const store = createStore().setRow('pets', 'fido', {species: 'dog'});
1171
+ * console.log(store.getTables());
1172
+ * // -> {pets: {fido: {species: 'dog'}}}
1173
+ * ```
1174
+ * @example
1175
+ * This example attempts to set the data of an existing Store with partly
1176
+ * invalid, and then completely invalid, Row objects.
1177
+ *
1178
+ * ```js
1179
+ * const store = createStore().setTables({pets: {fido: {species: 'dog'}}});
1180
+ *
1181
+ * store.setRow('pets', 'fido', {color: 'brown', bug: []});
1182
+ * console.log(store.getTables());
1183
+ * // -> {pets: {fido: {color: 'brown'}}}
1184
+ *
1185
+ * store.setRow('pets', 'fido', 42);
1186
+ * console.log(store.getTables());
1187
+ * // -> {pets: {fido: {color: 'brown'}}}
1188
+ * ```
1189
+ * @category Setter
1190
+ */
1191
+ setRow(tableId: Id, rowId: Id, row: Row): Store;
1192
+
1193
+ /**
1194
+ * The addRow method takes an object and creates a new Row in the Store,
1195
+ * returning the unique Id assigned to it.
1196
+ *
1197
+ * This method will cause listeners to be called for any Table, Row, Cell, or
1198
+ * Id changes resulting from it.
1199
+ *
1200
+ * Any part of the provided object that is invalid (either according to the
1201
+ * Row type, or because it does not match a Schema associated with the Store),
1202
+ * will be ignored silently.
1203
+ *
1204
+ * Assuming that at least some of the provided Row object is valid, a new Row
1205
+ * will be created. If the object is completely invalid, no change will be
1206
+ * made to the Store and the method will return `undefined`
1207
+ *
1208
+ * You should not guarantee the form of the unique Id that is generated when a
1209
+ * Row is added to the Table. However it is likely to be a string
1210
+ * representation of an increasing integer.
1211
+ *
1212
+ * @param tableId The Id of the Table in the Store.
1213
+ * @param row The data of a single Row to be added.
1214
+ * @returns A reference to the Store.
1215
+ * @example
1216
+ * This example adds a single Row.
1217
+ *
1218
+ * ```js
1219
+ * const store = createStore();
1220
+ * console.log(store.addRow('pets', {species: 'dog'}));
1221
+ * // -> '0'
1222
+ * console.log(store.getTables());
1223
+ * // -> {pets: {'0': {species: 'dog'}}}
1224
+ * ```
1225
+ * @example
1226
+ * This example attempts to add Rows to an existing Store with partly invalid,
1227
+ * and then completely invalid, Row objects.
1228
+ *
1229
+ * ```js
1230
+ * const store = createStore().setTables({pets: {'0': {species: 'dog'}}});
1231
+ *
1232
+ * console.log(store.addRow('pets', {species: 'cat', bug: []}));
1233
+ * // -> '1'
1234
+ * console.log(store.getTables());
1235
+ * // -> {pets: {'0': {species: 'dog'}, '1': {species: 'cat'}}}
1236
+ *
1237
+ * console.log(store.addRow('pets', 42));
1238
+ * // -> undefined
1239
+ * console.log(store.getTables());
1240
+ * // -> {pets: {'0': {species: 'dog'}, '1': {species: 'cat'}}}
1241
+ * ```
1242
+ * @category Setter
1243
+ */
1244
+ addRow(tableId: Id, row: Row): Id | undefined;
1245
+
1246
+ /**
1247
+ * The setPartialRow method takes an object and sets partial data of a single
1248
+ * Row in the Store, leaving other Cell values unaffected.
1249
+ *
1250
+ * This method will cause listeners to be called for any Table, Row, Cell, or
1251
+ * Id changes resulting from it.
1252
+ *
1253
+ * Any part of the provided object that is invalid (either according to the
1254
+ * Row type, or because, when combined with the current Row data, it does not
1255
+ * match a Schema associated with the Store), will be ignored silently.
1256
+ *
1257
+ * Assuming that at least some of the provided Row object is valid, it will be
1258
+ * merged with the data that was already present in the Store. If the object
1259
+ * is completely invalid, no change will be made to the Store.
1260
+ *
1261
+ * The method returns a reference to the Store to that subsequent operations
1262
+ * can be chained in a fluent style.
1263
+ *
1264
+ * @param tableId The Id of the Table in the Store.
1265
+ * @param rowId The Id of the Row in the Table.
1266
+ * @param partialRow The partial data of a single Row to be set.
1267
+ * @returns A reference to the Store.
1268
+ * @example
1269
+ * This example sets some of the data of a single Row.
1270
+ *
1271
+ * ```js
1272
+ * const store = createStore().setTables({
1273
+ * pets: {fido: {species: 'dog', color: 'brown'}},
1274
+ * });
1275
+ * store.setPartialRow('pets', 'fido', {color: 'walnut', visits: 1});
1276
+ * console.log(store.getTables());
1277
+ * // -> {pets: {fido: {species: 'dog', color: 'walnut', visits: 1}}}
1278
+ * ```
1279
+ * @example
1280
+ * This example attempts to set some of the data of an existing Store with
1281
+ * partly invalid, and then completely invalid, Row objects.
1282
+ *
1283
+ * ```js
1284
+ * const store = createStore().setTables({pets: {fido: {species: 'dog'}}});
1285
+ *
1286
+ * store.setPartialRow('pets', 'fido', {color: 'brown', bug: []});
1287
+ * console.log(store.getTables());
1288
+ * // -> {pets: {fido: {species: 'dog', color: 'brown'}}}
1289
+ *
1290
+ * store.setPartialRow('pets', 'fido', 42);
1291
+ * console.log(store.getTables());
1292
+ * // -> {pets: {fido: {species: 'dog', color: 'brown'}}}
1293
+ * ```
1294
+ * @category Setter
1295
+ */
1296
+ setPartialRow(tableId: Id, rowId: Id, partialRow: Row): Store;
1297
+
1298
+ /**
1299
+ * The setCell method sets the value of a single Cell in the Store.
1300
+ *
1301
+ * This method will cause listeners to be called for any Table, Row, Cell, or
1302
+ * Id changes resulting from it.
1303
+ *
1304
+ * If the Cell value is invalid (either because of its type, or because it
1305
+ * does not match a Schema associated with the Store), will be ignored
1306
+ * silently.
1307
+ *
1308
+ * As well as string, number, or boolean Cell types, this method can also take
1309
+ * a MapCell function that takes the current Cell value as a parameter and
1310
+ * maps it. This is useful if you want to efficiently increment a value
1311
+ * without fetching it first, for example.
1312
+ *
1313
+ * The method returns a reference to the Store to that subsequent operations
1314
+ * can be chained in a fluent style.
1315
+ *
1316
+ * @param tableId The Id of the Table in the Store.
1317
+ * @param rowId The Id of the Row in the Table.
1318
+ * @param cellId The Id of the Cell in the Row.
1319
+ * @param cell The value of the Cell to be set, or a MapCell function to
1320
+ * update it.
1321
+ * @returns A reference to the Store.
1322
+ * @example
1323
+ * This example sets the value of a single Cell.
1324
+ *
1325
+ * ```js
1326
+ * const store = createStore().setCell('pets', 'fido', 'species', 'dog');
1327
+ * console.log(store.getTables());
1328
+ * // -> {pets: {fido: {species: 'dog'}}}
1329
+ * ```
1330
+ * @example
1331
+ * This example sets the data of a single Cell by mapping the existing value.
1332
+ *
1333
+ * ```js
1334
+ * const increment = (cell) => cell + 1;
1335
+ * const store = createStore().setTables({pets: {fido: {visits: 1}}});
1336
+ *
1337
+ * store.setCell('pets', 'fido', 'visits', increment);
1338
+ * console.log(store.getCell('pets', 'fido', 'visits'));
1339
+ * // -> 2
1340
+ * ```
1341
+ * @example
1342
+ * This example attempts to set the data of an existing Store with an invalid
1343
+ * Cell value.
1344
+ *
1345
+ * ```js
1346
+ * const store = createStore().setTables({pets: {fido: {species: 'dog'}}});
1347
+ *
1348
+ * store.setCell('pets', 'fido', 'bug', []);
1349
+ * console.log(store.getTables());
1350
+ * // -> {pets: {fido: {species: 'dog'}}}
1351
+ * ```
1352
+ * @category Setter
1353
+ */
1354
+ setCell(tableId: Id, rowId: Id, cellId: Id, cell: Cell | MapCell): Store;
1355
+
1356
+ /**
1357
+ * The setJson method takes a string serialization of all of the Tables in the
1358
+ * Store and attempts to update it to that value
1359
+ *
1360
+ * If the JSON cannot be parsed, this will fail silently. If it can be parsed,
1361
+ * it will then be subject to the same validation rules as the setTables
1362
+ * method (according to the Tables type, and matching any Schema associated
1363
+ * with the Store).
1364
+ *
1365
+ * @param json A string serialization of all of the Tables in the Store.
1366
+ * @returns A reference to the Store.
1367
+ * @example
1368
+ * This example sets the contents of a Store from a serialization.
1369
+ *
1370
+ * ```js
1371
+ * const store = createStore();
1372
+ * store.setJson('{"pets":{"fido":{"species":"dog"}}}');
1373
+ * console.log(store.getTables());
1374
+ * // -> {pets: {fido: {species: 'dog'}}}
1375
+ * ```
1376
+ * @example
1377
+ * This example attempts to set the contents of a Store from an invalid
1378
+ * serialization.
1379
+ *
1380
+ * ```js
1381
+ * const store = createStore();
1382
+ * store.setJson('{"pets":{"fido":{');
1383
+ * console.log(store.getTables());
1384
+ * // -> {}
1385
+ * ```
1386
+ * @category Setter
1387
+ */
1388
+ setJson(json: Json): Store;
1389
+
1390
+ /**
1391
+ * The setSchema method lets you specify the Schema of the Store.
1392
+ *
1393
+ * Note that this may result in a change to data in the Store, as defaults are
1394
+ * applied or as invalid Table, Row, or Cell objects are removed. These
1395
+ * changes will fire any listeners to that data, as expected.
1396
+ *
1397
+ * When no longer needed, you can also completely remove an existing Schema
1398
+ * with the delSchema method.
1399
+ *
1400
+ * @param schema The Schema to be set for the Store.
1401
+ * @returns A reference to the Store.
1402
+ * @example
1403
+ * This example sets the Schema of a Store after it has been created.
1404
+ *
1405
+ * ```js
1406
+ * const store = createStore().setSchema({
1407
+ * pets: {
1408
+ * species: {type: 'string'},
1409
+ * sold: {type: 'boolean', default: false},
1410
+ * },
1411
+ * });
1412
+ * store.addRow('pets', {species: 'dog', color: 'brown', sold: 'maybe'});
1413
+ * console.log(store.getTables());
1414
+ * // -> {pets: {0: {species: 'dog', sold: false}}}
1415
+ * ```
1416
+ * @category Setter
1417
+ */
1418
+ setSchema(tablesSchema: Schema): Store;
1419
+
1420
+ /**
1421
+ * The delTables method lets you remove all of the data in a Store.
1422
+ *
1423
+ * @returns A reference to the Store.
1424
+ * @example
1425
+ * This example removes the data of a Store.
1426
+ *
1427
+ * ```js
1428
+ * const store = createStore().setTables({pets: {fido: {species: 'dog'}}});
1429
+ *
1430
+ * store.delTables();
1431
+ * console.log(store.getTables());
1432
+ * // -> {}
1433
+ * ```
1434
+ * @category Deleter
1435
+ */
1436
+ delTables(): Store;
1437
+
1438
+ /**
1439
+ * The delTable method lets you remove a single Table from the Store.
1440
+ *
1441
+ * @param tableId The Id of the Table in the Store.
1442
+ * @returns A reference to the Store.
1443
+ * @example
1444
+ * This example removes a Table from a Store.
1445
+ *
1446
+ * ```js
1447
+ * const store = createStore().setTables({
1448
+ * pets: {fido: {species: 'dog'}},
1449
+ * species: {dog: {price: 5}},
1450
+ * });
1451
+ * store.delTable('pets');
1452
+ *
1453
+ * console.log(store.getTables());
1454
+ * // -> {species: {dog: {price: 5}}}
1455
+ * ```
1456
+ * @category Deleter
1457
+ */
1458
+ delTable(tableId: Id): Store;
1459
+
1460
+ /**
1461
+ * The delRow method lets you remove a single Row from a Table.
1462
+ *
1463
+ * If this is the last Row in its Table, then that Table will be removed.
1464
+ *
1465
+ * @param tableId The Id of the Table in the Store.
1466
+ * @param rowId The Id of the Row in the Table.
1467
+ * @returns A reference to the Store.
1468
+ * @example
1469
+ * This example removes a Row from a Table.
1470
+ *
1471
+ * ```js
1472
+ * const store = createStore().setTables({
1473
+ * pets: {fido: {species: 'dog'}, felix: {species: 'cat'}},
1474
+ * });
1475
+ * store.delRow('pets', 'fido');
1476
+ *
1477
+ * console.log(store.getTables());
1478
+ * // -> {pets: {felix: {species: 'cat'}}}
1479
+ * ```
1480
+ * @category Deleter
1481
+ */
1482
+ delRow(tableId: Id, rowId: Id): Store;
1483
+
1484
+ /**
1485
+ * The delCell method lets you remove a single Cell from a Row.
1486
+ *
1487
+ * When there is no Schema applied to the Store, then if this is the last Cell
1488
+ * in its Row, then that Row will be removed. If, in turn, that is the last
1489
+ * Row in its Table, then that Table will be removed.
1490
+ *
1491
+ * If there is a Schema applied to the Store and it specifies a default value
1492
+ * for this Cell, then deletion will result in it being set back to its
1493
+ * default value. To override this, use the `forceDel` parameter, as described
1494
+ * below.
1495
+ *
1496
+ * The `forceDel` parameter is an optional flag that is only relevant if a
1497
+ * Schema provides a default value for this Cell. Under such circumstances,
1498
+ * deleting a Cell value will normally restore it to the default value. If
1499
+ * this flag is set to `true`, the complete removal of the Cell is instead
1500
+ * guaranteed. But since doing do so would result in an invalid Row (according
1501
+ * to the Schema), in fact the whole Row is deleted to retain the integrity of
1502
+ * the Table. Therefore, this flag should be used with caution.
1503
+ *
1504
+ * @param tableId The Id of the Table in the Store.
1505
+ * @param rowId The Id of the Row in the Table.
1506
+ * @param cellId The Id of the Cell in the Row.
1507
+ * @param forceDel An optional flag to indicate that the whole Row should be
1508
+ * deleted, even if a Schema provides a default value for this Cell. Defaults
1509
+ * to `false`.
1510
+ * @returns A reference to the Store.
1511
+ * @example
1512
+ * This example removes a Cell from a Row without a Schema.
1513
+ *
1514
+ * ```js
1515
+ * const store = createStore().setTables({
1516
+ * pets: {fido: {species: 'dog', sold: true}},
1517
+ * });
1518
+ * store.delCell('pets', 'fido', 'sold');
1519
+ *
1520
+ * console.log(store.getTables());
1521
+ * // -> {pets: {fido: {species: 'dog'}}}
1522
+ * ```
1523
+ * @example
1524
+ * This example removes a Cell from a Row with a Schema that defaults its
1525
+ * value.
1526
+ *
1527
+ * ```js
1528
+ * const store = createStore()
1529
+ * .setTables({
1530
+ * pets: {fido: {species: 'dog', sold: true}},
1531
+ * })
1532
+ * .setSchema({
1533
+ * pets: {
1534
+ * species: {type: 'string'},
1535
+ * sold: {type: 'boolean', default: false},
1536
+ * },
1537
+ * });
1538
+ * store.delCell('pets', 'fido', 'sold');
1539
+ *
1540
+ * console.log(store.getTables());
1541
+ * // -> {pets: {fido: {species: 'dog', sold: false}}}
1542
+ * ```
1543
+ * @example
1544
+ * This example removes a Cell from a Row with a Schema that defaults its
1545
+ * value, but uses the `forceDel` parameter to override it.
1546
+ *
1547
+ * ```js
1548
+ * const store = createStore()
1549
+ * .setTables({
1550
+ * pets: {fido: {species: 'dog', sold: true}, felix: {species: 'cat'}},
1551
+ * })
1552
+ * .setSchema({
1553
+ * pets: {
1554
+ * species: {type: 'string'},
1555
+ * sold: {type: 'boolean', default: false},
1556
+ * },
1557
+ * });
1558
+ * store.delCell('pets', 'fido', 'sold', true);
1559
+ *
1560
+ * console.log(store.getTables());
1561
+ * // -> {pets: {felix: {species: 'cat', sold: false}}}
1562
+ * ```
1563
+ * @category Deleter
1564
+ */
1565
+ delCell(tableId: Id, rowId: Id, cellId: Id, forceDel?: boolean): Store;
1566
+
1567
+ /**
1568
+ * The delSchema method lets you remove the Schema of the Store.
1569
+ *
1570
+ * @returns A reference to the Store.
1571
+ * @example
1572
+ * This example removes the Schema of a Store.
1573
+ *
1574
+ * ```js
1575
+ * const store = createStore().setSchema({pets: {species: {type: 'string'}}});
1576
+ * store.delSchema();
1577
+ * console.log(store.getSchemaJson());
1578
+ * // -> '{}'
1579
+ * ```
1580
+ * @category Deleter
1581
+ */
1582
+ delSchema(): Store;
1583
+
1584
+ /**
1585
+ * The transaction method takes a function that makes multiple mutations to
1586
+ * the store, buffering all calls to the relevant listeners until it
1587
+ * completes.
1588
+ *
1589
+ * This method is useful for making bulk changes to the data in a Store, and
1590
+ * when you don't want listeners to be called as you make each change. Changes
1591
+ * are made silently during the transaction, and listeners relevant to the
1592
+ * changes you have made will instead only be called when the whole
1593
+ * transaction is complete.
1594
+ *
1595
+ * If multiple changes are made to a piece of Store data throughout the
1596
+ * transaction, a relevant listener will only be called with the final value
1597
+ * (assuming it is different to the value at the start of the transaction),
1598
+ * regardless of the changes that happened in between. For example, if a Cell
1599
+ * had a value `'b'` and then, within a transaction, it was changed to `'b'`
1600
+ * and then `'c'`, any CellListener registered for that cell would be called
1601
+ * once as if there had been a single change from `'a'` to `'c'`.
1602
+ *
1603
+ * Transactions can be nested. Relevant listeners will be called only when the
1604
+ * outermost one completes.
1605
+ *
1606
+ * @param actions The function to be executed as a transaction.
1607
+ * @returns Whatever value the provided transaction function returns.
1608
+ * @example
1609
+ * This example makes changes to two Cells, first outside, and secondly
1610
+ * within, a transaction. In the second case, the Row listener is only called
1611
+ * once.
1612
+ *
1613
+ * ```js
1614
+ * const store = createStore().setTables({pets: {fido: {species: 'dog'}}});
1615
+ * store.addRowListener('pets', 'fido', () => console.log('Fido changed'));
1616
+ *
1617
+ * store.setCell('pets', 'fido', 'color', 'brown');
1618
+ * store.setCell('pets', 'fido', 'sold', false);
1619
+ * // -> 'Fido changed'
1620
+ * // -> 'Fido changed'
1621
+ *
1622
+ * store.transaction(() => {
1623
+ * store.setCell('pets', 'fido', 'color', 'walnut');
1624
+ * store.setCell('pets', 'fido', 'sold', true);
1625
+ * });
1626
+ * // -> 'Fido changed'
1627
+ * ```
1628
+ * @example
1629
+ * This example makes multiple changes to one Cell. The Cell listener is
1630
+ * called once - and with the final value - only if there is a net overall
1631
+ * change.
1632
+ *
1633
+ * ```js
1634
+ * const store = createStore().setTables({pets: {fido: {species: 'dog'}}});
1635
+ * store.addCellListener(
1636
+ * 'pets',
1637
+ * 'fido',
1638
+ * 'color',
1639
+ * (store, tableId, rowId, cellId, newCell) => console.log(newCell),
1640
+ * );
1641
+ *
1642
+ * store.transaction(() => {
1643
+ * store.setCell('pets', 'fido', 'color', 'black');
1644
+ * store.setCell('pets', 'fido', 'color', 'brown');
1645
+ * store.setCell('pets', 'fido', 'color', 'walnut');
1646
+ * });
1647
+ * // -> 'walnut'
1648
+ *
1649
+ * store.transaction(() => {
1650
+ * store.setCell('pets', 'fido', 'color', 'black');
1651
+ * store.setCell('pets', 'fido', 'color', 'walnut');
1652
+ * });
1653
+ * // -> undefined
1654
+ * // No net change during the transaction, so the listener is not called.
1655
+ * ```
1656
+ * @category Setter
1657
+ */
1658
+ transaction<Return>(actions: () => Return): Return;
1659
+
1660
+ /**
1661
+ * The forEachTable method takes a function that it will then call for each
1662
+ * Table in the Store.
1663
+ *
1664
+ * This method is useful for iterating over the Table structure of the Store
1665
+ * in a functional style. The `tableCallback` parameter is a TableCallback
1666
+ * function that will be called with the Id of each Table, and with a function
1667
+ * that can then be used to iterate over each Row of the Table, should you
1668
+ * wish.
1669
+ *
1670
+ * @param tableCallback The function that should be called for every Table.
1671
+ * @example
1672
+ * This example iterates over each Table in a Store, and lists each Row Id
1673
+ * within them.
1674
+ *
1675
+ * ```js
1676
+ * const store = createStore().setTables({
1677
+ * pets: {fido: {species: 'dog'}},
1678
+ * species: {dog: {price: 5}},
1679
+ * });
1680
+ * store.forEachTable((tableId, forEachRow) => {
1681
+ * console.log(tableId);
1682
+ * forEachRow((rowId) => console.log(`- ${rowId}`));
1683
+ * });
1684
+ * // -> 'pets'
1685
+ * // -> '- fido'
1686
+ * // -> 'species'
1687
+ * // -> '- dog'
1688
+ * ```
1689
+ * @category Iterator
1690
+ */
1691
+ forEachTable(tableCallback: TableCallback): void;
1692
+
1693
+ /**
1694
+ * The forEachRow method takes a function that it will then call for each Row
1695
+ * in a specified Table.
1696
+ *
1697
+ * This method is useful for iterating over the Row structure of the Table in
1698
+ * a functional style. The `rowCallback` parameter is a RowCallback function
1699
+ * that will be called with the Id of each Row, and with a function that can
1700
+ * then be used to iterate over each Cell of the Row, should you wish.
1701
+ *
1702
+ * @param tableId The Id of the Table to iterate over.
1703
+ * @param rowCallback The function that should be called for every Row.
1704
+ * @example
1705
+ * This example iterates over each Row in a Table, and lists each Cell Id
1706
+ * within them.
1707
+ *
1708
+ * ```js
1709
+ * const store = createStore().setTables({
1710
+ * pets: {
1711
+ * fido: {species: 'dog'},
1712
+ * felix: {color: 'black'},
1713
+ * },
1714
+ * });
1715
+ * store.forEachRow('pets', (rowId, forEachCell) => {
1716
+ * console.log(rowId);
1717
+ * forEachCell((cellId) => console.log(`- ${cellId}`));
1718
+ * });
1719
+ * // -> 'fido'
1720
+ * // -> '- species'
1721
+ * // -> 'felix'
1722
+ * // -> '- color'
1723
+ * ```
1724
+ * @category Iterator
1725
+ */
1726
+ forEachRow(tableId: Id, rowCallback: RowCallback): void;
1727
+
1728
+ /**
1729
+ * The forEachCell method takes a function that it will then call for each
1730
+ * Cell in a specified Row.
1731
+ *
1732
+ * This method is useful for iterating over the Cell structure of the Row in a
1733
+ * functional style. The `cellCallback` parameter is a CellCallback function
1734
+ * that will be called with the Id and value of each Cell.
1735
+ *
1736
+ * @param tableId The Id of the Table containing the Row to iterate over.
1737
+ * @param rowId The Id of the Row to iterate over.
1738
+ * @param cellCallback The function that should be called for every Cell.
1739
+ * @example
1740
+ * This example iterates over each Cell in a Row, and lists its value.
1741
+ *
1742
+ * ```js
1743
+ * const store = createStore().setTables({
1744
+ * pets: {fido: {species: 'dog', color: 'brown'}},
1745
+ * });
1746
+ * store.forEachCell('pets', 'fido', (cellId, cell) => {
1747
+ * console.log(`${cellId}: ${cell}`);
1748
+ * });
1749
+ * // -> 'species: dog'
1750
+ * // -> 'color: brown'
1751
+ * ```
1752
+ * @category Iterator
1753
+ */
1754
+ forEachCell(tableId: Id, rowId: Id, cellCallback: CellCallback): void;
1755
+
1756
+ /**
1757
+ * The addTablesListener method registers a listener function with the Store
1758
+ * that will be called whenever data in the Store changes.
1759
+ *
1760
+ * The provided listener is a TablesListener function, and will be called with
1761
+ * a reference to the Store and a GetCellChange function in case you need to
1762
+ * inspect any changes that occurred.
1763
+ *
1764
+ * Use the optional mutator parameter to indicate that there is code in the
1765
+ * listener that will mutate Store data. If set to `false` (or omitted), such
1766
+ * mutations will be silently ignored. All relevant mutator listeners (with
1767
+ * this flag set to `true`) are called _before_ any non-mutator listeners
1768
+ * (since the latter may become relevant due to changes made in the former).
1769
+ * The changes made by mutator listeners do not fire other mutating listeners,
1770
+ * though they will fire non-mutator listeners.
1771
+ *
1772
+ * @param listener The function that will be called whenever data in the Store
1773
+ * changes.
1774
+ * @param mutator An optional boolean that indicates that the listener mutates
1775
+ * Store data.
1776
+ * @returns A unique Id for the listener that can later be used to call it
1777
+ * explicitly, or to remove it.
1778
+ * @example
1779
+ * This example registers a listener that responds to any changes to the whole
1780
+ * Store.
1781
+ *
1782
+ * ```js
1783
+ * const store = createStore().setTables({
1784
+ * pets: {fido: {species: 'dog', color: 'brown'}},
1785
+ * });
1786
+ * const listenerId = store.addTablesListener((store, getCellChange) => {
1787
+ * console.log('Tables changed');
1788
+ * console.log(getCellChange('pets', 'fido', 'color'));
1789
+ * });
1790
+ *
1791
+ * store.setCell('pets', 'fido', 'color', 'walnut');
1792
+ * // -> 'Tables changed'
1793
+ * // -> [true, 'brown', 'walnut']
1794
+ *
1795
+ * store.delListener(listenerId);
1796
+ * ```
1797
+ * @example
1798
+ * This example registers a listener that responds to any changes to the whole
1799
+ * Store, and which also mutates the Store itself.
1800
+ *
1801
+ * ```js
1802
+ * const store = createStore().setTables({
1803
+ * pets: {fido: {species: 'dog', color: 'brown'}},
1804
+ * });
1805
+ * const listenerId = store.addTablesListener(
1806
+ * (store) => store.setCell('meta', 'update', 'store', true),
1807
+ * true,
1808
+ * );
1809
+ *
1810
+ * store.delCell('pets', 'fido', 'color');
1811
+ * console.log(store.getTable('meta'));
1812
+ * // -> {update: {store: true}}
1813
+ *
1814
+ * store.delListener(listenerId);
1815
+ * ```
1816
+ * @category Listener
1817
+ */
1818
+ addTablesListener(listener: TablesListener, mutator?: boolean): Id;
1819
+
1820
+ /**
1821
+ * The addTableIdsListener method registers a listener function with the Store
1822
+ * that will be called whenever the Table Ids in the Store change.
1823
+ *
1824
+ * Such a listener is only called when a Table is added or removed. To listen
1825
+ * to all changes in the Store, use the addTablesListener method.
1826
+ *
1827
+ * The provided listener is a TableIdsListener function, and will be called
1828
+ * with a reference to the Store.
1829
+ *
1830
+ * Use the optional mutator parameter to indicate that there is code in the
1831
+ * listener that will mutate Store data. If set to `false` (or omitted), such
1832
+ * mutations will be silently ignored. All relevant mutator listeners (with
1833
+ * this flag set to `true`) are called _before_ any non-mutator listeners
1834
+ * (since the latter may become relevant due to changes made in the former).
1835
+ * The changes made by mutator listeners do not fire other mutating listeners,
1836
+ * though they will fire non-mutator listeners.
1837
+ *
1838
+ * @param listener The function that will be called whenever the Table Ids in
1839
+ * the Store change.
1840
+ * @param mutator An optional boolean that indicates that the listener mutates
1841
+ * Store data.
1842
+ * @returns A unique Id for the listener that can later be used to call it
1843
+ * explicitly, or to remove it.
1844
+ * @example
1845
+ * This example registers a listener that responds to any change to the Table
1846
+ * Ids.
1847
+ *
1848
+ * ```js
1849
+ * const store = createStore().setTables({pets: {fido: {species: 'dog'}}});
1850
+ * const listenerId = store.addTableIdsListener((store) => {
1851
+ * console.log('Table Ids changed');
1852
+ * console.log(store.getTableIds());
1853
+ * });
1854
+ *
1855
+ * store.setTable('species', {dog: {price: 5}});
1856
+ * // -> 'Table Ids changed'
1857
+ * // -> ['pets', 'species']
1858
+ *
1859
+ * store.delListener(listenerId);
1860
+ * ```
1861
+ * @example
1862
+ * This example registers a listener that responds to any change to the Table
1863
+ * Ids, and which also mutates the Store itself.
1864
+ *
1865
+ * ```js
1866
+ * const store = createStore().setTables({pets: {fido: {species: 'dog'}}});
1867
+ * const listenerId = store.addTableIdsListener(
1868
+ * (store) => store.setCell('meta', 'update', 'store', true),
1869
+ * true,
1870
+ * );
1871
+ *
1872
+ * store.setTable('species', {dog: {price: 5}});
1873
+ * console.log(store.getTable('meta'));
1874
+ * // -> {update: {store: true}}
1875
+ *
1876
+ * store.delListener(listenerId);
1877
+ * ```
1878
+ * @category Listener
1879
+ */
1880
+ addTableIdsListener(listener: TableIdsListener, mutator?: boolean): Id;
1881
+
1882
+ /**
1883
+ * The addTableListener method registers a listener function with the Store
1884
+ * that will be called whenever data in a Table changes.
1885
+ *
1886
+ * You can either listen to a single Table (by specifying its Id as the
1887
+ * method's first parameter) or changes to any Table (by providing a `null`
1888
+ * wildcard).
1889
+ *
1890
+ * The provided listener is a TableListener function, and will be called with
1891
+ * a reference to the Store, the Id of the Table that changed, and a
1892
+ * GetCellChange function in case you need to inspect any changes that
1893
+ * occurred.
1894
+ *
1895
+ * Use the optional mutator parameter to indicate that there is code in the
1896
+ * listener that will mutate Store data. If set to `false` (or omitted), such
1897
+ * mutations will be silently ignored. All relevant mutator listeners (with
1898
+ * this flag set to `true`) are called _before_ any non-mutator listeners
1899
+ * (since the latter may become relevant due to changes made in the former).
1900
+ * The changes made by mutator listeners do not fire other mutating listeners,
1901
+ * though they will fire non-mutator listeners.
1902
+ *
1903
+ * @param tableId The Id of the Table to listen to, or `null` as a wildcard.
1904
+ * @param listener The function that will be called whenever data in the
1905
+ * matching Table changes.
1906
+ * @param mutator An optional boolean that indicates that the listener mutates
1907
+ * Store data.
1908
+ * @returns A unique Id for the listener that can later be used to call it
1909
+ * explicitly, or to remove it.
1910
+ * @example
1911
+ * This example registers a listener that responds to any changes to a
1912
+ * specific Table.
1913
+ *
1914
+ * ```js
1915
+ * const store = createStore().setTables({
1916
+ * pets: {fido: {species: 'dog', color: 'brown'}},
1917
+ * });
1918
+ * const listenerId = store.addTableListener(
1919
+ * 'pets',
1920
+ * (store, tableId, getCellChange) => {
1921
+ * console.log('pets table changed');
1922
+ * console.log(getCellChange('pets', 'fido', 'color'));
1923
+ * },
1924
+ * );
1925
+ *
1926
+ * store.setCell('pets', 'fido', 'color', 'walnut');
1927
+ * // -> 'pets table changed'
1928
+ * // -> [true, 'brown', 'walnut']
1929
+ *
1930
+ * store.delListener(listenerId);
1931
+ * ```
1932
+ * @example
1933
+ * This example registers a listener that responds to any changes to any
1934
+ * Table.
1935
+ *
1936
+ * ```js
1937
+ * const store = createStore().setTables({
1938
+ * pets: {fido: {species: 'dog', color: 'brown'}},
1939
+ * });
1940
+ * const listenerId = store.addTableListener(null, (store, tableId) => {
1941
+ * console.log(`${tableId} table changed`);
1942
+ * });
1943
+ *
1944
+ * store.setCell('pets', 'fido', 'color', 'walnut');
1945
+ * // -> 'pets table changed'
1946
+ * store.setTable('species', {dog: {price: 5}});
1947
+ * // -> 'species table changed'
1948
+ *
1949
+ * store.delListener(listenerId);
1950
+ * ```
1951
+ * @example
1952
+ * This example registers a listener that responds to any changes to a
1953
+ * specific Table, and which also mutates the Store itself.
1954
+ *
1955
+ * ```js
1956
+ * const store = createStore().setTables({
1957
+ * pets: {fido: {species: 'dog', color: 'brown'}},
1958
+ * });
1959
+ * const listenerId = store.addTableListener(
1960
+ * 'pets',
1961
+ * (store, tableId) => store.setCell('meta', 'update', tableId, true),
1962
+ * true,
1963
+ * );
1964
+ *
1965
+ * store.delCell('pets', 'fido', 'color');
1966
+ * console.log(store.getTable('meta'));
1967
+ * // -> {update: {pets: true}}
1968
+ *
1969
+ * store.delListener(listenerId);
1970
+ * ```
1971
+ * @category Listener
1972
+ */
1973
+ addTableListener(
1974
+ tableId: IdOrNull,
1975
+ listener: TableListener,
1976
+ mutator?: boolean,
1977
+ ): Id;
1978
+
1979
+ /**
1980
+ * The addRowIdsListener method registers a listener function with the Store
1981
+ * that will be called whenever the Row Ids in a Table change.
1982
+ *
1983
+ * Such a listener is only called when a Row is added or removed. To listen to
1984
+ * all changes in the Table, use the addTableListener method.
1985
+ *
1986
+ * You can either listen to a single Table (by specifying its Id as the
1987
+ * method's first parameter) or changes to any Table (by providing `null`).
1988
+ *
1989
+ * The provided listener is a RowIdsListener function, and will be called with
1990
+ * a reference to the Store and the Id of the Table that changed.
1991
+ *
1992
+ * Use the optional mutator parameter to indicate that there is code in the
1993
+ * listener that will mutate Store data. If set to `false` (or omitted), such
1994
+ * mutations will be silently ignored. All relevant mutator listeners (with
1995
+ * this flag set to `true`) are called _before_ any non-mutator listeners
1996
+ * (since the latter may become relevant due to changes made in the former).
1997
+ * The changes made by mutator listeners do not fire other mutating listeners,
1998
+ * though they will fire non-mutator listeners.
1999
+ *
2000
+ * @param tableId The Id of the Table to listen to, or `null` as a wildcard.
2001
+ * @param listener The function that will be called whenever the Row Ids in
2002
+ * the Table change.
2003
+ * @param mutator An optional boolean that indicates that the listener mutates
2004
+ * Store data.
2005
+ * @returns A unique Id for the listener that can later be used to call it
2006
+ * explicitly, or to remove it.
2007
+ * @example
2008
+ * This example registers a listener that responds to any change to the Row
2009
+ * Ids of a specific Table.
2010
+ *
2011
+ * ```js
2012
+ * const store = createStore().setTables({pets: {fido: {species: 'dog'}}});
2013
+ * const listenerId = store.addRowIdsListener('pets', (store) => {
2014
+ * console.log('Row Ids for pets table changed');
2015
+ * console.log(store.getRowIds('pets'));
2016
+ * });
2017
+ *
2018
+ * store.setRow('pets', 'felix', {species: 'cat'});
2019
+ * // -> 'Row Ids for pets table changed'
2020
+ * // -> ['fido', 'felix']
2021
+ *
2022
+ * store.delListener(listenerId);
2023
+ * ```
2024
+ * @example
2025
+ * This example registers a listener that responds to any change to the Row
2026
+ * Ids of any Table.
2027
+ *
2028
+ * ```js
2029
+ * const store = createStore().setTables({pets: {fido: {species: 'dog'}}});
2030
+ * const listenerId = store.addRowIdsListener(null, (store, tableId) => {
2031
+ * console.log(`Row Ids for ${tableId} table changed`);
2032
+ * console.log(store.getRowIds(tableId));
2033
+ * });
2034
+ *
2035
+ * store.setRow('pets', 'felix', {species: 'cat'});
2036
+ * // -> 'Row Ids for pets table changed'
2037
+ * // -> ['fido', 'felix']
2038
+ * store.setRow('species', 'dog', {price: 5});
2039
+ * // -> 'Row Ids for species table changed'
2040
+ * // -> ['dog']
2041
+ *
2042
+ * store.delListener(listenerId);
2043
+ * ```
2044
+ * @example
2045
+ * This example registers a listener that responds to any change to the Row
2046
+ * Ids of a specific Table, and which also mutates the Store itself.
2047
+ *
2048
+ * ```js
2049
+ * const store = createStore().setTables({pets: {fido: {species: 'dog'}}});
2050
+ * const listenerId = store.addRowIdsListener(
2051
+ * 'pets',
2052
+ * (store, tableId) => store.setCell('meta', 'update', tableId, true),
2053
+ * true,
2054
+ * );
2055
+ *
2056
+ * store.setRow('pets', 'felix', {species: 'cat'});
2057
+ * console.log(store.getTable('meta'));
2058
+ * // -> {update: {pets: true}}
2059
+ *
2060
+ * store.delListener(listenerId);
2061
+ * ```
2062
+ * @category Listener
2063
+ */
2064
+ addRowIdsListener(
2065
+ tableId: IdOrNull,
2066
+ listener: RowIdsListener,
2067
+ mutator?: boolean,
2068
+ ): Id;
2069
+
2070
+ /**
2071
+ * The addRowListener method registers a listener function with the Store that
2072
+ * will be called whenever data in a Row changes.
2073
+ *
2074
+ * You can either listen to a single Row (by specifying the Table Id and Row
2075
+ * Id as the method's first two parameters) or changes to any Row (by
2076
+ * providing `null` wildcards).
2077
+ *
2078
+ * Both, either, or neither of the `tableId` and `rowId` parameters can be
2079
+ * wildcarded with `null`. You can listen to a specific Row in a specific
2080
+ * Table, any Row in a specific Table, a specific Row in any Table, or any Row
2081
+ * in any Table.
2082
+ *
2083
+ * The provided listener is a RowListener function, and will be called with a
2084
+ * reference to the Store, the Id of the Table that changed, the Id of the Row
2085
+ * that changed, and a GetCellChange function in case you need to inspect any
2086
+ * changes that occurred.
2087
+ *
2088
+ * Use the optional mutator parameter to indicate that there is code in the
2089
+ * listener that will mutate Store data. If set to `false` (or omitted), such
2090
+ * mutations will be silently ignored. All relevant mutator listeners (with
2091
+ * this flag set to `true`) are called _before_ any non-mutator listeners
2092
+ * (since the latter may become relevant due to changes made in the former).
2093
+ * The changes made by mutator listeners do not fire other mutating listeners,
2094
+ * though they will fire non-mutator listeners.
2095
+ *
2096
+ * @param tableId The Id of the Table to listen to, or `null` as a wildcard.
2097
+ * @param rowId The Id of the Row to listen to, or `null` as a wildcard.
2098
+ * @param listener The function that will be called whenever data in the
2099
+ * matching Row changes.
2100
+ * @param mutator An optional boolean that indicates that the listener mutates
2101
+ * Store data.
2102
+ * @returns A unique Id for the listener that can later be used to call it
2103
+ * explicitly, or to remove it.
2104
+ * @example
2105
+ * This example registers a listener that responds to any changes to a
2106
+ * specific Row.
2107
+ *
2108
+ * ```js
2109
+ * const store = createStore().setTables({
2110
+ * pets: {fido: {species: 'dog', color: 'brown'}},
2111
+ * });
2112
+ * const listenerId = store.addRowListener(
2113
+ * 'pets',
2114
+ * 'fido',
2115
+ * (store, tableId, rowId, getCellChange) => {
2116
+ * console.log('fido row in pets table changed');
2117
+ * console.log(getCellChange('pets', 'fido', 'color'));
2118
+ * },
2119
+ * );
2120
+ *
2121
+ * store.setCell('pets', 'fido', 'color', 'walnut');
2122
+ * // -> 'fido row in pets table changed'
2123
+ * // -> [true, 'brown', 'walnut']
2124
+ *
2125
+ * store.delListener(listenerId);
2126
+ * ```
2127
+ * @example
2128
+ * This example registers a listener that responds to any changes to any Row.
2129
+ *
2130
+ * ```js
2131
+ * const store = createStore().setTables({
2132
+ * pets: {fido: {species: 'dog', color: 'brown'}},
2133
+ * });
2134
+ * const listenerId = store.addRowListener(
2135
+ * null,
2136
+ * null,
2137
+ * (store, tableId, rowId) => {
2138
+ * console.log(`${rowId} row in ${tableId} table changed`);
2139
+ * },
2140
+ * );
2141
+ *
2142
+ * store.setCell('pets', 'fido', 'color', 'walnut');
2143
+ * // -> 'fido row in pets table changed'
2144
+ * store.setTable('species', {dog: {price: 5}});
2145
+ * // -> 'dog row in species table changed'
2146
+ *
2147
+ * store.delListener(listenerId);
2148
+ * ```
2149
+ * @example
2150
+ * This example registers a listener that responds to any changes to a
2151
+ * specific Row, and which also mutates the Store itself.
2152
+ *
2153
+ * ```js
2154
+ * const store = createStore().setTables({
2155
+ * pets: {fido: {species: 'dog', color: 'brown'}},
2156
+ * });
2157
+ * const listenerId = store.addRowListener(
2158
+ * 'pets',
2159
+ * 'fido',
2160
+ * (store, tableId, rowId) =>
2161
+ * store.setCell('meta', 'update', `${tableId}_${rowId}`, true),
2162
+ * true,
2163
+ * );
2164
+ *
2165
+ * store.delCell('pets', 'fido', 'color');
2166
+ * console.log(store.getTable('meta'));
2167
+ * // -> {update: {pets_fido: true}}
2168
+ *
2169
+ * store.delListener(listenerId);
2170
+ * ```
2171
+ * @category Listener
2172
+ */
2173
+ addRowListener(
2174
+ tableId: IdOrNull,
2175
+ rowId: IdOrNull,
2176
+ listener: RowListener,
2177
+ mutator?: boolean,
2178
+ ): Id;
2179
+
2180
+ /**
2181
+ * The addCellIdsListener method registers a listener function with the Store
2182
+ * that will be called whenever the Cell Ids in a Row change.
2183
+ *
2184
+ * Such a listener is only called when a Cell is added or removed. To listen
2185
+ * to all changes in the Row, use the addRowListener method.
2186
+ *
2187
+ * You can either listen to a single Row (by specifying the Table Id and Row
2188
+ * Id as the method's first two parameters) or changes to any Row (by
2189
+ * providing `null`).
2190
+ *
2191
+ * Both, either, or neither of the `tableId` and `rowId` parameters can be
2192
+ * wildcarded with `null`. You can listen to a specific Row in a specific
2193
+ * Table, any Row in a specific Table, a specific Row in any Table, or any Row
2194
+ * in any Table.
2195
+ *
2196
+ * The provided listener is a CellIdsListener function, and will be called
2197
+ * with a reference to the Store, the Id of the Table, and the Id of the Row
2198
+ * that changed.
2199
+ *
2200
+ * Use the optional mutator parameter to indicate that there is code in the
2201
+ * listener that will mutate Store data. If set to `false` (or omitted), such
2202
+ * mutations will be silently ignored. All relevant mutator listeners (with
2203
+ * this flag set to `true`) are called _before_ any non-mutator listeners
2204
+ * (since the latter may become relevant due to changes made in the former).
2205
+ * The changes made by mutator listeners do not fire other mutating listeners,
2206
+ * though they will fire non-mutator listeners.
2207
+ *
2208
+ * @param tableId The Id of the Table to listen to, or `null` as a wildcard.
2209
+ * @param rowId The Id of the Row to listen to, or `null` as a wildcard.
2210
+ * @param listener The function that will be called whenever the Cell Ids in
2211
+ * the Row change.
2212
+ * @param mutator An optional boolean that indicates that the listener mutates
2213
+ * Store data.
2214
+ * @returns A unique Id for the listener that can later be used to call it
2215
+ * explicitly, or to remove it.
2216
+ * @example
2217
+ * This example registers a listener that responds to any change to the Cell
2218
+ * Ids of a specific Row.
2219
+ *
2220
+ * ```js
2221
+ * const store = createStore().setTables({pets: {fido: {species: 'dog'}}});
2222
+ * const listenerId = store.addCellIdsListener('pets', 'fido', (store) => {
2223
+ * console.log('Cell Ids for fido row in pets table changed');
2224
+ * console.log(store.getCellIds('pets', 'fido'));
2225
+ * });
2226
+ *
2227
+ * store.setCell('pets', 'fido', 'color', 'brown');
2228
+ * // -> 'Cell Ids for fido row in pets table changed'
2229
+ * // -> ['species', 'color']
2230
+ *
2231
+ * store.delListener(listenerId);
2232
+ * ```
2233
+ * @example
2234
+ * This example registers a listener that responds to any change to the Cell
2235
+ * Ids of any Row.
2236
+ *
2237
+ * ```js
2238
+ * const store = createStore().setTables({pets: {fido: {species: 'dog'}}});
2239
+ * const listenerId = store.addCellIdsListener(
2240
+ * null,
2241
+ * null,
2242
+ * (store, tableId, rowId) => {
2243
+ * console.log(`Cell Ids for ${rowId} row in ${tableId} table changed`);
2244
+ * console.log(store.getCellIds(tableId, rowId));
2245
+ * },
2246
+ * );
2247
+ *
2248
+ * store.setCell('pets', 'fido', 'color', 'brown');
2249
+ * // -> 'Cell Ids for fido row in pets table changed'
2250
+ * // -> ['species', 'color']
2251
+ * store.setCell('species', 'dog', 'price', 5);
2252
+ * // -> 'Cell Ids for dog row in species table changed'
2253
+ * // -> ['price']
2254
+ *
2255
+ * store.delListener(listenerId);
2256
+ * ```
2257
+ * @example
2258
+ * This example registers a listener that responds to any change to the Cell
2259
+ * Ids of a specific Row, and which also mutates the Store itself.
2260
+ *
2261
+ * ```js
2262
+ * const store = createStore().setTables({pets: {fido: {species: 'dog'}}});
2263
+ * const listenerId = store.addCellIdsListener(
2264
+ * 'pets',
2265
+ * 'fido',
2266
+ * (store, tableId, rowId) =>
2267
+ * store.setCell('meta', 'update', `${tableId}_${rowId}`, true),
2268
+ * true,
2269
+ * );
2270
+ *
2271
+ * store.setCell('pets', 'fido', 'color', 'brown');
2272
+ * console.log(store.getTable('meta'));
2273
+ * // -> {update: {pets_fido: true}}
2274
+ *
2275
+ * store.delListener(listenerId);
2276
+ * ```
2277
+ * @category Listener
2278
+ */
2279
+ addCellIdsListener(
2280
+ tableId: IdOrNull,
2281
+ rowId: IdOrNull,
2282
+ listener: CellIdsListener,
2283
+ mutator?: boolean,
2284
+ ): Id;
2285
+
2286
+ /**
2287
+ * The addCellListener method registers a listener function with the Store
2288
+ * that will be called whenever data in a Cell changes.
2289
+ *
2290
+ * You can either listen to a single Cell (by specifying the Table Id, Row Id,
2291
+ * and Cell Id as the method's first three parameters) or changes to any Cell
2292
+ * (by providing `null` wildcards).
2293
+ *
2294
+ * All, some, or none of the `tableId`, `rowId`, and `cellId` parameters can
2295
+ * be wildcarded with `null`. You can listen to a specific Cell in a specific
2296
+ * Row in a specific Table, any Cell in any Row in any Table, for example - or
2297
+ * every other combination of wildcards.
2298
+ *
2299
+ * The provided listener is a CellListener function, and will be called with a
2300
+ * reference to the Store, the Id of the Table that changed, the Id of the Row
2301
+ * that changed, the Id of the Cell that changed, the new Cell value, the old
2302
+ * Cell value, and a GetCellChange function in case you need to inspect any
2303
+ * changes that occurred.
2304
+ *
2305
+ * Use the optional mutator parameter to indicate that there is code in the
2306
+ * listener that will mutate Store data. If set to `false` (or omitted), such
2307
+ * mutations will be silently ignored. All relevant mutator listeners (with
2308
+ * this flag set to `true`) are called _before_ any non-mutator listeners
2309
+ * (since the latter may become relevant due to changes made in the former).
2310
+ * The changes made by mutator listeners do not fire other mutating listeners,
2311
+ * though they will fire non-mutator listeners.
2312
+ *
2313
+ * @param tableId The Id of the Table to listen to, or `null` as a wildcard.
2314
+ * @param rowId The Id of the Row to listen to, or `null` as a wildcard.
2315
+ * @param cellId The Id of the Cell to listen to, or `null` as a wildcard.
2316
+ * @param listener The function that will be called whenever data in the
2317
+ * matching Cell changes.
2318
+ * @param mutator An optional boolean that indicates that the listener mutates
2319
+ * Store data.
2320
+ * @returns A unique Id for the listener that can later be used to call it
2321
+ * explicitly, or to remove it.
2322
+ * @example
2323
+ * This example registers a listener that responds to any changes to a
2324
+ * specific Cell.
2325
+ *
2326
+ * ```js
2327
+ * const store = createStore().setTables({
2328
+ * pets: {fido: {species: 'dog', color: 'brown'}},
2329
+ * });
2330
+ * const listenerId = store.addCellListener(
2331
+ * 'pets',
2332
+ * 'fido',
2333
+ * 'color',
2334
+ * (store, tableId, rowId, cellId, newCell, oldCell, getCellChange) => {
2335
+ * console.log('color cell in fido row in pets table changed');
2336
+ * console.log([oldCell, newCell]);
2337
+ * console.log(getCellChange('pets', 'fido', 'color'));
2338
+ * },
2339
+ * );
2340
+ *
2341
+ * store.setCell('pets', 'fido', 'color', 'walnut');
2342
+ * // -> 'color cell in fido row in pets table changed'
2343
+ * // -> ['brown', 'walnut']
2344
+ * // -> [true, 'brown', 'walnut']
2345
+ *
2346
+ * store.delListener(listenerId);
2347
+ * ```
2348
+ * @example
2349
+ * This example registers a listener that responds to any changes to any Cell.
2350
+ *
2351
+ * ```js
2352
+ * const store = createStore().setTables({
2353
+ * pets: {fido: {species: 'dog', color: 'brown'}},
2354
+ * });
2355
+ * const listenerId = store.addCellListener(
2356
+ * null,
2357
+ * null,
2358
+ * null,
2359
+ * (store, tableId, rowId, cellId) => {
2360
+ * console.log(
2361
+ * `${cellId} cell in ${rowId} row in ${tableId} table changed`,
2362
+ * );
2363
+ * },
2364
+ * );
2365
+ *
2366
+ * store.setCell('pets', 'fido', 'color', 'walnut');
2367
+ * // -> 'color cell in fido row in pets table changed'
2368
+ * store.setTable('species', {dog: {price: 5}});
2369
+ * // -> 'price cell in dog row in species table changed'
2370
+ *
2371
+ * store.delListener(listenerId);
2372
+ * ```
2373
+ * @example
2374
+ * This example registers a listener that responds to any changes to a
2375
+ * specific Cell, and which also mutates the Store itself.
2376
+ *
2377
+ * ```js
2378
+ * const store = createStore().setTables({
2379
+ * pets: {fido: {species: 'dog', color: 'brown'}},
2380
+ * });
2381
+ * const listenerId = store.addCellListener(
2382
+ * 'pets',
2383
+ * 'fido',
2384
+ * 'color',
2385
+ * (store, tableId, rowId, cellId) =>
2386
+ * store.setCell('meta', 'update', `${tableId}_${rowId}_${cellId}`, true),
2387
+ * true,
2388
+ * );
2389
+ *
2390
+ * store.delCell('pets', 'fido', 'color');
2391
+ * console.log(store.getTable('meta'));
2392
+ * // -> {update: {pets_fido_color: true}}
2393
+ *
2394
+ * store.delListener(listenerId);
2395
+ * ```
2396
+ * @category Listener
2397
+ */
2398
+ addCellListener(
2399
+ tableId: IdOrNull,
2400
+ rowId: IdOrNull,
2401
+ cellId: IdOrNull,
2402
+ listener: CellListener,
2403
+ mutator?: boolean,
2404
+ ): Id;
2405
+
2406
+ /**
2407
+ * The addInvalidCellListener method registers a listener function with the
2408
+ * Store that will be called whenever invalid data was attempted to be written
2409
+ * to a Cell.
2410
+ *
2411
+ * You can either listen to a single Cell (by specifying the Table Id, Row Id,
2412
+ * and Cell Id as the method's first three parameters) or invalid attempts to
2413
+ * change any Cell (by providing `null` wildcards).
2414
+ *
2415
+ * All, some, or none of the `tableId`, `rowId`, and `cellId` parameters can
2416
+ * be wildcarded with `null`. You can listen to a specific Cell in a specific
2417
+ * Row in a specific Table, any Cell in any Row in any Table, for example - or
2418
+ * every other combination of wildcards.
2419
+ *
2420
+ * The provided listener is an InvalidCellListener function, and will be
2421
+ * called with a reference to the Store, the Id of the Table, the Id of the
2422
+ * Row, and the Id of Cell that were being attempted to be changed. It is also
2423
+ * given the invalid value of the Cell, which could have been of absolutely
2424
+ * any type. Since there could have been multiple failed attempts to set the
2425
+ * Cell within a single transaction, this is an array containing each attempt,
2426
+ * chronologically.
2427
+ *
2428
+ * Use the optional mutator parameter to indicate that there is code in the
2429
+ * listener that will mutate Store data. If set to `false` (or omitted), such
2430
+ * mutations will be silently ignored. All relevant mutator listeners (with
2431
+ * this flag set to `true`) are called _before_ any non-mutator listeners
2432
+ * (since the latter may become relevant due to changes made in the former).
2433
+ * The changes made by mutator listeners do not fire other mutating listeners,
2434
+ * though they will fire non-mutator listeners.
2435
+ *
2436
+ * @param tableId The Id of the Table to listen to, or `null` as a wildcard.
2437
+ * @param rowId The Id of the Row to listen to, or `null` as a wildcard.
2438
+ * @param cellId The Id of the Cell to listen to, or `null` as a wildcard.
2439
+ * @param listener The function that will be called whenever an attempt to
2440
+ * write invalid data to the matching Cell was made.
2441
+ * @param mutator An optional boolean that indicates that the listener mutates
2442
+ * Store data.
2443
+ * @returns A unique Id for the listener that can later be used to call it
2444
+ * explicitly, or to remove it.
2445
+ * @example
2446
+ * This example registers a listener that responds to any invalid changes to a
2447
+ * specific Cell.
2448
+ *
2449
+ * ```js
2450
+ * const store = createStore().setTables({
2451
+ * pets: {fido: {species: 'dog', color: 'brown'}},
2452
+ * });
2453
+ * const listenerId = store.addInvalidCellListener(
2454
+ * 'pets',
2455
+ * 'fido',
2456
+ * 'color',
2457
+ * (store, tableId, rowId, cellId, invalidCells) => {
2458
+ * console.log('Invalid color cell in fido row in pets table');
2459
+ * console.log(invalidCells);
2460
+ * },
2461
+ * );
2462
+ *
2463
+ * store.setCell('pets', 'fido', 'color', {r: '96', g: '4B', b: '00'});
2464
+ * // -> 'Invalid color cell in fido row in pets table'
2465
+ * // -> [{r: '96', g: '4B', b: '00'}]
2466
+ *
2467
+ * store.delListener(listenerId);
2468
+ * ```
2469
+ * @example
2470
+ * This example registers a listener that responds to any invalid changes to
2471
+ * any Cell.
2472
+ *
2473
+ * ```js
2474
+ * const store = createStore().setTables({
2475
+ * pets: {fido: {species: 'dog', color: 'brown'}},
2476
+ * });
2477
+ * const listenerId = store.addInvalidCellListener(
2478
+ * null,
2479
+ * null,
2480
+ * null,
2481
+ * (store, tableId, rowId, cellId) => {
2482
+ * console.log(
2483
+ * `Invalid ${cellId} cell in ${rowId} row in ${tableId} table`,
2484
+ * );
2485
+ * },
2486
+ * );
2487
+ *
2488
+ * store.setCell('pets', 'fido', 'color', {r: '96', g: '4B', b: '00'});
2489
+ * // -> 'Invalid color cell in fido row in pets table'
2490
+ * store.setTable('sales', {fido: {date: new Date()}});
2491
+ * // -> 'Invalid date cell in fido row in sales table'
2492
+ *
2493
+ * store.delListener(listenerId);
2494
+ * ```
2495
+ * @example
2496
+ * This example registers a listener that responds to any changes to a
2497
+ * specific Cell, and which also mutates the Store itself.
2498
+ *
2499
+ * ```js
2500
+ * const store = createStore().setTables({
2501
+ * pets: {fido: {species: 'dog', color: 'brown'}},
2502
+ * });
2503
+ * const listenerId = store.addInvalidCellListener(
2504
+ * 'pets',
2505
+ * 'fido',
2506
+ * 'color',
2507
+ * (store, tableId, rowId, cellId, invalidCells) =>
2508
+ * store.setCell(
2509
+ * 'meta',
2510
+ * 'invalid_updates',
2511
+ * `${tableId}_${rowId}_${cellId}`,
2512
+ * JSON.stringify(invalidCells[0]),
2513
+ * ),
2514
+ * true,
2515
+ * );
2516
+ *
2517
+ * store.setCell('pets', 'fido', 'color', {r: '96', g: '4B', b: '00'});
2518
+ * console.log(store.getRow('meta', 'invalid_updates'));
2519
+ * // -> {'pets_fido_color': '{"r":"96","g":"4B","b":"00"}'}
2520
+ *
2521
+ * store.delListener(listenerId);
2522
+ * ```
2523
+ * @category Listener
2524
+ */
2525
+ addInvalidCellListener(
2526
+ tableId: IdOrNull,
2527
+ rowId: IdOrNull,
2528
+ cellId: IdOrNull,
2529
+ listener: InvalidCellListener,
2530
+ mutator?: boolean,
2531
+ ): Id;
2532
+
2533
+ /**
2534
+ * The callListener method provides a way for you to manually provoke a
2535
+ * listener to be called, even if the underlying data hasn't changed.
2536
+ *
2537
+ * This is useful when you are using mutator listeners to guarantee that data
2538
+ * conforms to programmatic conditions, and those conditions change such that
2539
+ * you need to update the Store in bulk.
2540
+ *
2541
+ * @param listenerId The Id of the listener to call.
2542
+ * @returns A reference to the Store.
2543
+ * @example
2544
+ * This example registers a listener that ensures a Cell has one of list of a
2545
+ * valid values. After that list changes, the listener is called to apply the
2546
+ * condition to the existing data.
2547
+ *
2548
+ * ```js
2549
+ * const validColors = ['walnut', 'brown', 'black'];
2550
+ * const store = createStore();
2551
+ * const listenerId = store.addCellListener(
2552
+ * 'pets',
2553
+ * null,
2554
+ * 'color',
2555
+ * (store, tableId, rowId, cellId, color) => {
2556
+ * if (!validColors.includes(color)) {
2557
+ * store.setCell(tableId, rowId, cellId, validColors[0]);
2558
+ * }
2559
+ * },
2560
+ * true,
2561
+ * );
2562
+ *
2563
+ * store.setRow('pets', 'fido', {species: 'dog', color: 'honey'});
2564
+ * console.log(store.getRow('pets', 'fido'));
2565
+ * // -> {species: 'dog', color: 'walnut'}
2566
+ *
2567
+ * validColors.shift();
2568
+ * console.log(validColors);
2569
+ * // -> ['brown', 'black']
2570
+ *
2571
+ * store.callListener(listenerId);
2572
+ * console.log(store.getRow('pets', 'fido'));
2573
+ * // -> {species: 'dog', color: 'brown'}
2574
+ *
2575
+ * store.delListener(listenerId);
2576
+ * ```
2577
+ * @category Listener
2578
+ */
2579
+ callListener(listenerId: Id): Store;
2580
+
2581
+ /**
2582
+ * The delListener method removes a listener that was previously added to the
2583
+ * Store.
2584
+ *
2585
+ * Use the Id returned by whichever method was used to add the listener. Note
2586
+ * that the Store may re-use this Id for future listeners added to it.
2587
+ *
2588
+ * @param listenerId The Id of the listener to remove.
2589
+ * @returns A reference to the Store.
2590
+ * @example
2591
+ * This example registers a listener and then removes it.
2592
+ *
2593
+ * ```js
2594
+ * const store = createStore().setTables({
2595
+ * pets: {fido: {species: 'dog', color: 'brown'}},
2596
+ * });
2597
+ * const listenerId = store.addTablesListener(() => {
2598
+ * console.log('Tables changed');
2599
+ * });
2600
+ *
2601
+ * store.setCell('pets', 'fido', 'color', 'walnut');
2602
+ * // -> 'Tables changed'
2603
+ *
2604
+ * store.delListener(listenerId);
2605
+ *
2606
+ * store.setCell('pets', 'fido', 'color', 'honey');
2607
+ * // -> undefined
2608
+ * // The listener is not called.
2609
+ * ```
2610
+ * @category Listener
2611
+ */
2612
+ delListener(listenerId: Id): Store;
2613
+
2614
+ /**
2615
+ * The getListenerStats method provides a set of statistics about the
2616
+ * listeners registered with the Store, and is used for debugging purposes.
2617
+ *
2618
+ * The StoreListenerStats object contains a breakdown of the different types
2619
+ * of listener. Totals include both mutator and non-mutator listeners.
2620
+ *
2621
+ * The statistics are only populated in a debug build: production builds
2622
+ * return an empty object. The method is intended to be used during
2623
+ * development to ensure your application is not leaking listener
2624
+ * registrations, for example.
2625
+ *
2626
+ * @returns A StoreListenerStats object containing Store listener statistics.
2627
+ * @example
2628
+ * This example gets the listener statistics of a small and simple Store.
2629
+ *
2630
+ * ```js
2631
+ * const store = createStore();
2632
+ * store.addTablesListener(() => console.log('Tables changed'));
2633
+ * store.addRowIdsListener(() => console.log('Row Ids changed'));
2634
+ *
2635
+ * const listenerStats = store.getListenerStats();
2636
+ * console.log(listenerStats.rowIds);
2637
+ * // -> 1
2638
+ * console.log(listenerStats.tables);
2639
+ * // -> 1
2640
+ * ```
2641
+ * @category Development
2642
+ */
2643
+ getListenerStats(): StoreListenerStats;
2644
+ }
2645
+
2646
+ /**
2647
+ * The createStore function creates a Store, and is the main entry point into
2648
+ * the store module.
2649
+ *
2650
+ * Since (or perhaps _because_) it is the most important function in the whole
2651
+ * module, it is trivially simple.
2652
+ *
2653
+ * @returns A reference to the new Store.
2654
+ * @example
2655
+ * This example creates a Store.
2656
+ *
2657
+ * ```js
2658
+ * const store = createStore();
2659
+ * console.log(store.getTables());
2660
+ * // -> {}
2661
+ * ```
2662
+ * @example
2663
+ * This example creates a Store with some initial data:
2664
+ *
2665
+ * ```js
2666
+ * const store = createStore().setTables({pets: {fido: {species: 'dog'}}});
2667
+ * console.log(store.getTables());
2668
+ * // -> {pets: {fido: {species: 'dog'}}}
2669
+ * ```
2670
+ * @example
2671
+ * This example creates a Store with some initial data and a Schema:
2672
+ *
2673
+ * ```js
2674
+ * const store = createStore()
2675
+ * .setTables({pets: {fido: {species: 'dog'}}})
2676
+ * .setSchema({
2677
+ * pets: {
2678
+ * species: {type: 'string'},
2679
+ * sold: {type: 'boolean', default: false},
2680
+ * },
2681
+ * });
2682
+ * console.log(store.getTables());
2683
+ * // -> {pets: {fido: {species: 'dog', sold: false}}}
2684
+ * ```
2685
+ * @see The Basics guides
2686
+ * @category Creation
2687
+ */
2688
+ export function createStore(): Store;