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
@@ -0,0 +1,945 @@
1
+ /**
2
+ * The checkpoints module of the TinyBase project provides the ability to
3
+ * create and track checkpoints made to the data in Store objects.
4
+ *
5
+ * The main entry point to this module is the createCheckpoints function, which
6
+ * returns a new Checkpoints object. From there, you can create new checkpoints,
7
+ * go forwards or backwards to others, and register listeners for when the list
8
+ * of checkpoints change.
9
+ *
10
+ * @packageDocumentation
11
+ * @module checkpoints
12
+ */
13
+
14
+ import {Id, IdOrNull, Ids} from './common.d';
15
+ import {Store} from './store.d';
16
+
17
+ /**
18
+ * The CheckpointIds type is a representation of the list of checkpoint Ids
19
+ * stored in a Checkpoints object.
20
+ *
21
+ * There are three parts to a CheckpointsIds array:
22
+ *
23
+ * - The 'backward' checkpoint Ids that can be rolled backward to (in other
24
+ * words, the checkpoints in the undo stack for this Store). They are in
25
+ * chronological order with the oldest checkpoint at the start of the array.
26
+ * - The current checkpoint Id of the Store's state, or `undefined` if the
27
+ * current state has not been checkpointed.
28
+ * - The 'forward' checkpoint Ids that can be rolled forward to (in other words,
29
+ * the checkpoints in the redo stack for this Store). They are in
30
+ * chronological order with the newest checkpoint at the end of the array.
31
+ *
32
+ * @category Identity
33
+ */
34
+ export type CheckpointIds = [Ids, Id | undefined, Ids];
35
+
36
+ /**
37
+ * The CheckpointCallback type describes a function that takes a Checkpoint's
38
+ * Id.
39
+ *
40
+ * A CheckpointCallback is provided when using the forEachCheckpoint method,
41
+ * so that you can do something based on every Checkpoint in the Checkpoints
42
+ * object. See that method for specific examples.
43
+ *
44
+ * @param checkpointId The Id of the Checkpoint that the callback can operate
45
+ * on.
46
+ * @category Callback
47
+ */
48
+ export type CheckpointCallback = (checkpointId: Id, label?: string) => void;
49
+
50
+ /**
51
+ * The CheckpointIdsListener type describes a function that is used to listen to
52
+ * changes to the checkpoint Ids in a Checkpoints object.
53
+ *
54
+ * A CheckpointIdsListener is provided when using the addCheckpointIdsListener
55
+ * method. See that method for specific examples.
56
+ *
57
+ * When called, a CheckpointIdsListener is given a reference to the Checkpoints
58
+ * object.
59
+ *
60
+ * @param checkpoints A reference to the Checkpoints object that changed.
61
+ * @category Listener
62
+ */
63
+ export type CheckpointIdsListener = (checkpoints: Checkpoints) => void;
64
+
65
+ /**
66
+ * The CheckpointListener type describes a function that is used to listen to
67
+ * changes to a checkpoint's label in a Checkpoints object.
68
+ *
69
+ * A CheckpointListener is provided when using the addCheckpointListener method.
70
+ * See that method for specific examples.
71
+ *
72
+ * When called, a CheckpointListener is given a reference to the Checkpoints
73
+ * object, and the Id of the checkpoint whose label changed.
74
+ *
75
+ * @param checkpoints A reference to the Checkpoints object that changed.
76
+ * @param checkpointId The Id of the checkpoint that changed.
77
+ * @category Listener
78
+ */
79
+ export type CheckpointListener = (
80
+ checkpoints: Checkpoints,
81
+ checkpointId: Id,
82
+ ) => void;
83
+
84
+ /**
85
+ * The CheckpointsListenerStats type describes the number of listeners
86
+ * registered with the Checkpoints object, and can be used for debugging
87
+ * purposes.
88
+ *
89
+ * A CheckpointsListenerStats object is returned from the getListenerStats
90
+ * method, and is only populated in a debug build.
91
+ *
92
+ * @category Development
93
+ */
94
+ export type CheckpointsListenerStats = {
95
+ /**
96
+ * The number of CheckpointIdsListeners registered with the Checkpoints
97
+ * object.
98
+ */
99
+ checkpointIds?: number;
100
+ /**
101
+ * The number of CheckpointListeners registered with the Checkpoints object.
102
+ */
103
+ checkpoint?: number;
104
+ };
105
+
106
+ /**
107
+ * A Checkpoints object lets you set checkpoints on a Store, and move forward
108
+ * and backward through them to create undo and redo functionality.
109
+ *
110
+ * Create a Checkpoints object easily with the createCheckpoints function. From
111
+ * there, you can set checkpoints (with the addCheckpoint method), query the
112
+ * checkpoints available (with the getCheckpointIds method), move forward and
113
+ * backward through them (with the goBackward method, goForward method, and goTo
114
+ * method), and add listeners for when the list checkpoints changes (with the
115
+ * addCheckpointIdsListener method).
116
+ *
117
+ * Every checkpoint can be given a label which can be used to describe the
118
+ * actions that changed the Store before this checkpoint. This can be useful for
119
+ * interfaces that let users 'Undo [last action]'.
120
+ *
121
+ * You
122
+ *
123
+ * @example
124
+ * This example shows a simple lifecycle of a Checkpoints object: from creation,
125
+ * to adding a checkpoint, getting the list of available checkpoints, and then
126
+ * registering and removing a listener for them.
127
+ *
128
+ * ```js
129
+ * const store = createStore().setTables({pets: {fido: {sold: false}}});
130
+ *
131
+ * const checkpoints = createCheckpoints(store);
132
+ * checkpoints.setSize(200);
133
+ * console.log(checkpoints.getCheckpointIds());
134
+ * // -> [[], '0', []]
135
+ *
136
+ * store.setCell('pets', 'fido', 'sold', true);
137
+ * checkpoints.addCheckpoint('sale');
138
+ * console.log(checkpoints.getCheckpointIds());
139
+ * // -> [['0'], '1', []]
140
+ *
141
+ * checkpoints.goBackward();
142
+ * console.log(store.getCell('pets', 'fido', 'sold'));
143
+ * // -> false
144
+ * console.log(checkpoints.getCheckpointIds());
145
+ * // -> [[], '0', ['1']]
146
+ *
147
+ * const listenerId = checkpoints.addCheckpointIdsListener(() => {
148
+ * console.log(checkpoints.getCheckpointIds());
149
+ * });
150
+ * store.setCell('pets', 'fido', 'species', 'dog');
151
+ * // -> [['0'], undefined, []]
152
+ * checkpoints.addCheckpoint();
153
+ * // -> [['0'], '2', []]
154
+ * // Previous redo of checkpoint '1' is now not possible.
155
+ *
156
+ * checkpoints.delListener(listenerId);
157
+ * checkpoints.destroy();
158
+ * ```
159
+ * @see Relationships And Checkpoints guides
160
+ * @see Todo App demos
161
+ * @see TinyDraw demo
162
+ * @category Checkpoints
163
+ */
164
+ export interface Checkpoints {
165
+ /**
166
+ * The setSize method lets you specify how many checkpoints the Checkpoints
167
+ * object will store.
168
+ *
169
+ * If you set more checkpoints than this size, the oldest checkpoints will be
170
+ * pruned to make room for more recent ones.
171
+ *
172
+ * The default size for a newly-created Checkpoints object is 100.
173
+ *
174
+ * @param size The number of checkpoints that this Checkpoints object should
175
+ * hold.
176
+ * @returns A reference to the Checkpoints object.
177
+ * @example
178
+ * This example creates a Store, adds a Checkpoints object, reduces the size
179
+ * of the Checkpoints object dramatically and then creates more than that
180
+ * number of checkpoints to demonstrate the oldest being pruned.
181
+ *
182
+ * ```js
183
+ * const store = createStore().setTables({pets: {fido: {views: 0}}});
184
+ *
185
+ * const checkpoints = createCheckpoints(store);
186
+ * checkpoints.setSize(2);
187
+ * console.log(checkpoints.getCheckpointIds());
188
+ * // -> [[], '0', []]
189
+ *
190
+ * store.setCell('pets', 'fido', 'views', 1);
191
+ * checkpoints.addCheckpoint();
192
+ * console.log(checkpoints.getCheckpointIds());
193
+ * // -> [['0'], '1', []]
194
+ *
195
+ * store.setCell('pets', 'fido', 'views', 2);
196
+ * checkpoints.addCheckpoint();
197
+ * console.log(checkpoints.getCheckpointIds());
198
+ * // -> [['0', '1'], '2', []]
199
+ *
200
+ * store.setCell('pets', 'fido', 'views', 3);
201
+ * checkpoints.addCheckpoint();
202
+ * console.log(checkpoints.getCheckpointIds());
203
+ * // -> [['1', '2'], '3', []]
204
+ * ```
205
+ * @category Configuration
206
+ */
207
+ setSize(size: number): Checkpoints;
208
+
209
+ /**
210
+ * The addCheckpoint method records a checkpoint of the Store into the
211
+ * Checkpoints object that can be reverted to in the future.
212
+ *
213
+ * If no changes have been made to the Store since the last time a checkpoint
214
+ * was made, this method will have no effect.
215
+ *
216
+ * The optional `label` parameter can be used to describe the actions that
217
+ * changed the Store before this checkpoint. This can be useful for interfaces
218
+ * that let users 'Undo [last action]'.
219
+ *
220
+ * @param label An optional label to describe the actions leading up to this
221
+ * checkpoint.
222
+ * @returns The Id of the newly-created checkpoint.
223
+ * @example
224
+ * This example creates a Store, adds a Checkpoints object, and adds two
225
+ * checkpoints, one with a label.
226
+ *
227
+ * ```js
228
+ * const store = createStore().setTables({pets: {fido: {sold: false}}});
229
+ *
230
+ * const checkpoints = createCheckpoints(store);
231
+ * console.log(checkpoints.getCheckpointIds());
232
+ * // -> [[], '0', []]
233
+ *
234
+ * store.setCell('pets', 'fido', 'species', 'dog');
235
+ * const checkpointId1 = checkpoints.addCheckpoint();
236
+ * console.log(checkpointId1);
237
+ * // -> '1'
238
+ *
239
+ * console.log(checkpoints.getCheckpointIds());
240
+ * // -> [['0'], '1', []]
241
+ *
242
+ * store.setCell('pets', 'fido', 'sold', true);
243
+ * checkpoints.addCheckpoint('sale');
244
+ * console.log(checkpoints.getCheckpointIds());
245
+ * // -> [['0', '1'], '2', []]
246
+ *
247
+ * console.log(checkpoints.getCheckpoint('2'));
248
+ * // -> 'sale'
249
+ * ```
250
+ * @category Setter
251
+ */
252
+ addCheckpoint(label?: string): Id;
253
+
254
+ /**
255
+ * The setCheckpoint method updates the label for a checkpoint in the
256
+ * Checkpoints object after it has been created
257
+ *
258
+ * The `label` parameter can be used to describe the actions that changed the
259
+ * Store before the given checkpoint. This can be useful for interfaces that
260
+ * let users 'Undo [last action]'.
261
+ *
262
+ * Generally you will provide the `label` parameter when the addCheckpoint
263
+ * method is called. Use this setCheckpoint method only when you need to
264
+ * change the label at a later point.
265
+ *
266
+ * You cannot add a label to a checkpoint that does not yet exist.
267
+ *
268
+ * @param checkpointId The Id of the checkpoint to set the label for.
269
+ * @param label A label to describe the actions leading up to this checkpoint
270
+ * or left undefined if you want to clear the current label.
271
+ * @returns A reference to the Checkpoints object.
272
+ * @example
273
+ * This example creates a Store, adds a Checkpoints object, and sets two
274
+ * checkpoints, one with a label, which are both then re-labelled.
275
+ *
276
+ * ```js
277
+ * const store = createStore().setTables({pets: {fido: {sold: false}}});
278
+ *
279
+ * const checkpoints = createCheckpoints(store);
280
+ * console.log(checkpoints.getCheckpointIds());
281
+ * // -> [[], '0', []]
282
+ *
283
+ * store.setCell('pets', 'fido', 'species', 'dog');
284
+ * checkpoints.addCheckpoint();
285
+ * store.setCell('pets', 'fido', 'sold', true);
286
+ * checkpoints.addCheckpoint('sale');
287
+ *
288
+ * console.log(checkpoints.getCheckpoint('1'));
289
+ * // -> ''
290
+ * console.log(checkpoints.getCheckpoint('2'));
291
+ * // -> 'sale'
292
+ *
293
+ * checkpoints.setCheckpoint('1', 'identified');
294
+ * checkpoints.setCheckpoint('2', '');
295
+ *
296
+ * console.log(checkpoints.getCheckpoint('1'));
297
+ * // -> 'identified'
298
+ * console.log(checkpoints.getCheckpoint('2'));
299
+ * // -> ''
300
+ *
301
+ * checkpoints.setCheckpoint('3', 'unknown');
302
+ * console.log(checkpoints.getCheckpoint('3'));
303
+ * // -> undefined
304
+ * ```
305
+ * @category Setter
306
+ */
307
+ setCheckpoint(checkpointId: Id, label: string): Checkpoints;
308
+
309
+ /**
310
+ * The getStore method returns a reference to the underlying Store that is
311
+ * backing this Checkpoints object.
312
+ *
313
+ * @returns A reference to the Store.
314
+ * @example
315
+ * This example creates a Checkpoints object against a newly-created Store
316
+ * and then gets its reference in order to update its data and set a
317
+ * checkpoint.
318
+ *
319
+ * ```js
320
+ * const checkpoints = createCheckpoints(createStore());
321
+ * checkpoints.getStore().setCell('pets', 'fido', 'species', 'dog');
322
+ * checkpoints.addCheckpoint();
323
+ * console.log(checkpoints.getCheckpointIds());
324
+ * // -> [['0'], '1', []]
325
+ * ```
326
+ * @category Getter
327
+ */
328
+ getStore(): Store;
329
+
330
+ /**
331
+ * The getCheckpointIds method returns an array of the checkpoint Ids being
332
+ * managed by this Checkpoints object.
333
+ *
334
+ * The returned CheckpointIds array contains 'backward' checkpoint Ids, the
335
+ * current checkpoint Id (if present), and the 'forward' checkpointIds.
336
+ * Together, these are sufficient to understand the state of the Checkpoints
337
+ * object and what movement is possible backward or forward through the
338
+ * checkpoint stack.
339
+ *
340
+ * @returns A CheckpointIds array, containing the checkpoint Ids managed by
341
+ * this Checkpoints object.
342
+ * @example
343
+ * This example creates a Store, adds a Checkpoints object, and then gets the
344
+ * Ids of the checkpoints as it sets them and moves around the stack.
345
+ *
346
+ * ```js
347
+ * const store = createStore().setTables({pets: {fido: {sold: false}}});
348
+ *
349
+ * const checkpoints = createCheckpoints(store);
350
+ * console.log(checkpoints.getCheckpointIds());
351
+ * // -> [[], '0', []]
352
+ *
353
+ * store.setCell('pets', 'fido', 'sold', true);
354
+ * checkpoints.addCheckpoint('sale');
355
+ * console.log(checkpoints.getCheckpointIds());
356
+ * // -> [['0'], '1', []]
357
+ *
358
+ * checkpoints.goBackward();
359
+ * console.log(checkpoints.getCheckpointIds());
360
+ * // -> [[], '0', ['1']]
361
+ *
362
+ * checkpoints.goForward();
363
+ * console.log(checkpoints.getCheckpointIds());
364
+ * // -> [['0'], '1', []]
365
+ * ```
366
+ * @category Getter
367
+ */
368
+ getCheckpointIds(): CheckpointIds;
369
+
370
+ /**
371
+ * The forEachCheckpoint method takes a function that it will then call for
372
+ * each Checkpoint in a specified Checkpoints object.
373
+ *
374
+ * This method is useful for iterating over the structure of the Checkpoints
375
+ * object in a functional style. The `checkpointCallback` parameter is a
376
+ * CheckpointCallback function that will be called with the Id of each
377
+ * Checkpoint.
378
+ *
379
+ * @param checkpointCallback The function that should be called for every
380
+ * Checkpoint.
381
+ * @example
382
+ * This example iterates over each Checkpoint in a Checkpoints object.
383
+ *
384
+ * ```js
385
+ * const store = createStore().setTables({pets: {fido: {sold: false}}});
386
+ * const checkpoints = createCheckpoints(store);
387
+ * store.setCell('pets', 'fido', 'sold', true);
388
+ * checkpoints.addCheckpoint('sale');
389
+ *
390
+ * checkpoints.forEachCheckpoint((checkpointId, label) => {
391
+ * console.log(`${checkpointId}:${label}`);
392
+ * });
393
+ * // -> '0:'
394
+ * // -> '1:sale'
395
+ * ```
396
+ * @category Iterator
397
+ */
398
+ forEachCheckpoint(checkpointCallback: CheckpointCallback): void;
399
+
400
+ /**
401
+ * The hasCheckpoint method returns a boolean indicating whether a given
402
+ * Checkpoint exists in the Checkpoints object.
403
+ *
404
+ * @param checkpointId The Id of a possible Checkpoint in the Checkpoints
405
+ * object.
406
+ * @returns Whether a Checkpoint with that Id exists.
407
+ * @example
408
+ * This example shows two simple Checkpoint existence checks.
409
+ *
410
+ * ```js
411
+ * const store = createStore().setTables({pets: {fido: {sold: false}}});
412
+ * const checkpoints = createCheckpoints(store);
413
+ * console.log(checkpoints.hasCheckpoint('0'));
414
+ * // -> true
415
+ * console.log(checkpoints.hasCheckpoint('1'));
416
+ * // -> false
417
+ * ```
418
+ * @category Getter
419
+ */
420
+ hasCheckpoint(checkpointId: Id): boolean;
421
+
422
+ /**
423
+ * The getCheckpoint method fetches the label for a checkpoint, if it had been
424
+ * provided at the time of the addCheckpoint method or set subsequently with
425
+ * the setCheckpoint method.
426
+ *
427
+ * If the checkpoint has had no label provided, this method will return an
428
+ * empty string.
429
+ *
430
+ * @param checkpointId The Id of the checkpoint to get the label for.
431
+ * @returns A string label for the requested checkpoint, an empty string if it
432
+ * was never set, or `undefined` if the checkpoint does not exist.
433
+ * @example
434
+ * This example creates a Store, adds a Checkpoints object, and sets a
435
+ * checkpoint with a label, before retrieving it again.
436
+ *
437
+ * ```js
438
+ * const store = createStore().setTables({pets: {fido: {sold: false}}});
439
+ *
440
+ * const checkpoints = createCheckpoints(store);
441
+ * store.setCell('pets', 'fido', 'sold', true);
442
+ * console.log(checkpoints.addCheckpoint('sale'));
443
+ * // -> '1'
444
+ *
445
+ * console.log(checkpoints.getCheckpoint('1'));
446
+ * // -> 'sale'
447
+ * ```
448
+ * @example
449
+ * This example creates a Store, adds a Checkpoints object, and sets a
450
+ * checkpoint without a label, setting it subsequently. A non-existent
451
+ * checkpoint return an `undefined` label.
452
+ *
453
+ * ```js
454
+ * const store = createStore().setTables({pets: {fido: {sold: false}}});
455
+ *
456
+ * const checkpoints = createCheckpoints(store);
457
+ * store.setCell('pets', 'fido', 'sold', true);
458
+ * checkpoints.addCheckpoint();
459
+ * console.log(checkpoints.getCheckpoint('1'));
460
+ * // -> ''
461
+ *
462
+ * checkpoints.setCheckpoint('1', 'sold');
463
+ * console.log(checkpoints.getCheckpoint('1'));
464
+ * // -> 'sold'
465
+ *
466
+ * console.log(checkpoints.getCheckpoint('2'));
467
+ * // -> undefined
468
+ * ```
469
+ * @category Getter
470
+ */
471
+ getCheckpoint(checkpointId: Id): string | undefined;
472
+
473
+ /**
474
+ * The addCheckpointIdsListener method registers a listener function with the
475
+ * Checkpoints object that will be called whenever its set of checkpoints
476
+ * changes.
477
+ *
478
+ * The provided listener is a CheckpointIdsListener function, and will be
479
+ * called with a reference to the Checkpoints object.
480
+ *
481
+ * @param listener The function that will be called whenever the checkpoints
482
+ * change.
483
+ * @returns A unique Id for the listener that can later be used to remove it.
484
+ * @example
485
+ * This example creates a Store, a Checkpoints object, and then registers a
486
+ * listener that responds to any changes to the checkpoints.
487
+ *
488
+ * ```js
489
+ * const store = createStore().setTables({pets: {fido: {sold: false}}});
490
+ *
491
+ * const checkpoints = createCheckpoints(store);
492
+ * console.log(checkpoints.getCheckpointIds());
493
+ * // -> [[], '0', []]
494
+ *
495
+ * const listenerId = checkpoints.addCheckpointIdsListener(() => {
496
+ * console.log('Checkpoint Ids changed');
497
+ * console.log(checkpoints.getCheckpointIds());
498
+ * });
499
+ *
500
+ * store.setCell('pets', 'fido', 'species', 'dog');
501
+ * // -> 'Checkpoint Ids changed'
502
+ * // -> [['0'], undefined, []]
503
+ *
504
+ * checkpoints.addCheckpoint();
505
+ * // -> 'Checkpoint Ids changed'
506
+ * // -> [['0'], '1', []]
507
+ *
508
+ * checkpoints.goBackward();
509
+ * // -> 'Checkpoint Ids changed'
510
+ * // -> [[], '0', ['1']]
511
+ *
512
+ * checkpoints.goForward();
513
+ * // -> 'Checkpoint Ids changed'
514
+ * // -> [['0'], '1', []]
515
+ *
516
+ * checkpoints.delListener(listenerId);
517
+ * checkpoints.destroy();
518
+ * ```
519
+ * @category Listener
520
+ */
521
+ addCheckpointIdsListener(listener: CheckpointIdsListener): Id;
522
+
523
+ /**
524
+ * The addCheckpointListener method registers a listener function with the
525
+ * Checkpoints object that will be called whenever the label of a checkpoint
526
+ * changes.
527
+ *
528
+ * You can either listen to a single checkpoint label (by specifying the
529
+ * checkpoint Id as the method's first parameter), or changes to any
530
+ * checkpoint label (by providing a `null` wildcard).
531
+ *
532
+ * The provided listener is a CheckpointListener function, and will be called
533
+ * with a reference to the Checkpoints object, and the Id of the checkpoint
534
+ * whose label changed.
535
+ *
536
+ * @param checkpointId The Id of the checkpoint to listen to, or `null` as a
537
+ * wildcard.
538
+ * @param listener The function that will be called whenever the checkpoint
539
+ * label changes.
540
+ * @returns A unique Id for the listener that can later be used to remove it.
541
+ * @example
542
+ * This example creates a Store, a Checkpoints object, and then registers a
543
+ * listener that responds to any changes to a specific checkpoint label,
544
+ * including when the checkpoint no longer exists.
545
+ *
546
+ * ```js
547
+ * const store = createStore().setTables({pets: {fido: {species: 'dog'}}});
548
+ *
549
+ * const checkpoints = createCheckpoints(store);
550
+ * console.log(checkpoints.getCheckpointIds());
551
+ * // -> [[], '0', []]
552
+ *
553
+ * const listenerId = checkpoints.addCheckpointListener('1', () => {
554
+ * console.log('Checkpoint 1 label changed');
555
+ * console.log(checkpoints.getCheckpoint('1'));
556
+ * });
557
+ *
558
+ * store.setCell('pets', 'fido', 'sold', true);
559
+ * checkpoints.addCheckpoint('sale');
560
+ * // -> 'Checkpoint 1 label changed'
561
+ * // -> 'sale'
562
+ *
563
+ * checkpoints.setCheckpoint('1', 'sold');
564
+ * // -> 'Checkpoint 1 label changed'
565
+ * // -> 'sold'
566
+ *
567
+ * checkpoints.setCheckpoint('1', 'sold');
568
+ * // The listener is not called when the label does not change.
569
+ *
570
+ * checkpoints.goTo('0');
571
+ * store.setCell('pets', 'fido', 'sold', false);
572
+ * // -> 'Checkpoint 1 label changed'
573
+ * // -> undefined
574
+ * // The checkpoint no longer exists.
575
+ *
576
+ * checkpoints.delListener(listenerId);
577
+ * checkpoints.destroy();
578
+ * ```
579
+ * @category Listener
580
+ */
581
+ addCheckpointListener(
582
+ checkpointId: IdOrNull,
583
+ listener: CheckpointListener,
584
+ ): Id;
585
+
586
+ /**
587
+ * The delListener method removes a listener that was previously added to the
588
+ * Checkpoints object.
589
+ *
590
+ * Use the Id returned by the addCheckpointIdsListener method. Note that the
591
+ * Checkpoints object may re-use this Id for future listeners added to it.
592
+ *
593
+ * @param listenerId The Id of the listener to remove.
594
+ * @returns A reference to the Checkpoints object.
595
+ * @example
596
+ * This example creates a Store, a Checkpoints object, registers a listener,
597
+ * and then removes it.
598
+ *
599
+ * ```js
600
+ * const store = createStore().setTables({pets: {fido: {sold: false}}});
601
+ *
602
+ * const checkpoints = createCheckpoints(store);
603
+ * console.log(checkpoints.getCheckpointIds());
604
+ * // -> [[], '0', []]
605
+ *
606
+ * const listenerId = checkpoints.addCheckpointIdsListener(() => {
607
+ * console.log('checkpoints changed');
608
+ * });
609
+ *
610
+ * store.setCell('pets', 'fido', 'species', 'dog');
611
+ * // -> 'checkpoints changed'
612
+ *
613
+ * checkpoints.addCheckpoint();
614
+ * // -> 'checkpoints changed'
615
+ *
616
+ * checkpoints.delListener(listenerId);
617
+ *
618
+ * store.setCell('pets', 'fido', 'sold', 'true');
619
+ * // -> undefined
620
+ * // The listener is not called.
621
+ * ```
622
+ * @category Listener
623
+ */
624
+ delListener(listenerId: Id): Checkpoints;
625
+
626
+ /**
627
+ * The goBackward method moves the state of the underlying Store back to the
628
+ * previous checkpoint, effectively performing an 'undo' on the Store data.
629
+ *
630
+ * If there is no previous checkpoint to return to, this method has no effect.
631
+ *
632
+ * @returns A reference to the Checkpoints object.
633
+ * @example
634
+ * This example creates a Store, a Checkpoints object, makes a change and then
635
+ * goes backward to the state of the Store before the change.
636
+ *
637
+ * ```js
638
+ * const store = createStore().setTables({pets: {fido: {sold: false}}});
639
+ *
640
+ * const checkpoints = createCheckpoints(store);
641
+ * console.log(checkpoints.getCheckpointIds());
642
+ * // -> [[], '0', []]
643
+ *
644
+ * store.setCell('pets', 'fido', 'sold', true);
645
+ * checkpoints.addCheckpoint('sale');
646
+ * console.log(checkpoints.getCheckpointIds());
647
+ * // -> [['0'], '1', []]
648
+ *
649
+ * checkpoints.goBackward();
650
+ * console.log(store.getCell('pets', 'fido', 'sold'));
651
+ * // -> false
652
+ * console.log(checkpoints.getCheckpointIds());
653
+ * // -> [[], '0', ['1']]
654
+ * ```
655
+ * @category Movement
656
+ */
657
+ goBackward(): Checkpoints;
658
+
659
+ /**
660
+ * The goForward method moves the state of the underlying Store forwards to a
661
+ * future checkpoint, effectively performing an 'redo' on the Store data.
662
+ *
663
+ * If there is no future checkpoint to return to, this method has no effect.
664
+ *
665
+ * Note that if you have previously used the goBackward method to undo
666
+ * changes, the forwards 'redo' stack will only exist while you do not make
667
+ * changes to the Store. In general the goForward method is expected to be
668
+ * used to redo changes that were just undone.
669
+ *
670
+ * @returns A reference to the Checkpoints object.
671
+ * @example
672
+ * This example creates a Store, a Checkpoints object, makes a change and then
673
+ * goes backward to the state of the Store before the change. It then goes
674
+ * forward again to restore the state with the changes.
675
+ *
676
+ * ```js
677
+ * const store = createStore().setTables({pets: {fido: {sold: false}}});
678
+ *
679
+ * const checkpoints = createCheckpoints(store);
680
+ * console.log(checkpoints.getCheckpointIds());
681
+ * // -> [[], '0', []]
682
+ *
683
+ * store.setCell('pets', 'fido', 'sold', true);
684
+ * checkpoints.addCheckpoint('sale');
685
+ * console.log(checkpoints.getCheckpointIds());
686
+ * // -> [['0'], '1', []]
687
+ *
688
+ * checkpoints.goBackward();
689
+ * console.log(store.getCell('pets', 'fido', 'sold'));
690
+ * // -> false
691
+ * console.log(checkpoints.getCheckpointIds());
692
+ * // -> [[], '0', ['1']]
693
+ *
694
+ * checkpoints.goForward();
695
+ * console.log(store.getCell('pets', 'fido', 'sold'));
696
+ * // -> true
697
+ * console.log(checkpoints.getCheckpointIds());
698
+ * // -> [['0'], '1', []]
699
+ * ```
700
+ * @example
701
+ * This example creates a Store, a Checkpoints object, makes a change and then
702
+ * goes backward to the state of the Store before the change. It makes a new
703
+ * change, the redo stack disappears, and then the attempt to forward again
704
+ * has no effect.
705
+ *
706
+ * ```js
707
+ * const store = createStore().setTables({pets: {fido: {sold: false}}});
708
+ *
709
+ * const checkpoints = createCheckpoints(store);
710
+ * console.log(checkpoints.getCheckpointIds());
711
+ * // -> [[], '0', []]
712
+ *
713
+ * store.setCell('pets', 'fido', 'sold', true);
714
+ * checkpoints.addCheckpoint('sale');
715
+ * console.log(checkpoints.getCheckpointIds());
716
+ * // -> [['0'], '1', []]
717
+ *
718
+ * checkpoints.goBackward();
719
+ * console.log(store.getCell('pets', 'fido', 'sold'));
720
+ * // -> false
721
+ * console.log(checkpoints.getCheckpointIds());
722
+ * // -> [[], '0', ['1']]
723
+ *
724
+ * store.setCell('pets', 'fido', 'color', 'brown');
725
+ * console.log(checkpoints.getCheckpointIds());
726
+ * // -> [['0'], undefined, []]
727
+ *
728
+ * checkpoints.goForward();
729
+ * console.log(store.getCell('pets', 'fido', 'sold'));
730
+ * // -> false
731
+ * console.log(checkpoints.getCheckpointIds());
732
+ * // -> [['0'], undefined, []]
733
+ * // The original change cannot be redone.
734
+ * ```
735
+ * @category Movement
736
+ */
737
+ goForward(): Checkpoints;
738
+
739
+ /**
740
+ * The goTo method moves the state of the underlying Store backwards or
741
+ * forwards to a specified checkpoint.
742
+ *
743
+ * If there is no checkpoint with the Id specified, this method has no effect.
744
+ *
745
+ * @param checkpointId The Id of the checkpoint to move to.
746
+ * @returns A reference to the Checkpoints object.
747
+ * @example
748
+ * This example creates a Store, a Checkpoints object, makes two changes and
749
+ * then goes directly to the state of the Store before the two changes. It
750
+ * then goes forward again one change, also using the goTo method. Finally it
751
+ * tries to go to a checkpoint that does not exist.
752
+ *
753
+ * ```js
754
+ * const store = createStore().setTables({pets: {fido: {sold: false}}});
755
+ *
756
+ * const checkpoints = createCheckpoints(store);
757
+ * console.log(checkpoints.getCheckpointIds());
758
+ * // -> [[], '0', []]
759
+ *
760
+ * store.setCell('pets', 'fido', 'color', 'brown');
761
+ * checkpoints.addCheckpoint('identification');
762
+ * store.setCell('pets', 'fido', 'sold', true);
763
+ * checkpoints.addCheckpoint('sale');
764
+ * console.log(checkpoints.getCheckpointIds());
765
+ * // -> [['0', '1'], '2', []]
766
+ *
767
+ * checkpoints.goTo('0');
768
+ * console.log(store.getTables());
769
+ * // -> {pets: {fido: {sold: false}}}
770
+ * console.log(checkpoints.getCheckpointIds());
771
+ * // -> [[], '0', ['1', '2']]
772
+ *
773
+ * checkpoints.goTo('1');
774
+ * console.log(store.getTables());
775
+ * // -> {pets: {fido: {sold: false, color: 'brown'}}}
776
+ * console.log(checkpoints.getCheckpointIds());
777
+ * // -> [['0'], '1', ['2']]
778
+ *
779
+ * checkpoints.goTo('3');
780
+ * console.log(store.getTables());
781
+ * // -> {pets: {fido: {sold: false, color: 'brown'}}}
782
+ * console.log(checkpoints.getCheckpointIds());
783
+ * // -> [['0'], '1', ['2']]
784
+ * ```
785
+ * @category Movement
786
+ */
787
+ goTo(checkpointId: Id): Checkpoints;
788
+
789
+ /**
790
+ * The clear method resets this Checkpoints object to its initial state,
791
+ * removing all the checkpoints it has been managing.
792
+ *
793
+ * Obviously this method should be used with caution as it destroys the
794
+ * ability to undo recent changes to the Store (though of course the Store
795
+ * itself is not reset by this method).
796
+ *
797
+ * This method can be useful when a Store is being loaded via a Persister
798
+ * asynchronously after the Checkpoints object has been attached, and you
799
+ * don't want users to be able to undo the initial load of the data. In this
800
+ * you could call the clear method immediately after the initial load so that
801
+ * that is the baseline from which all subsequent changes are tracked.
802
+ *
803
+ * If you are listening to
804
+ *
805
+ * @returns A reference to the Checkpoints object.
806
+ * @example
807
+ * This example creates a Store, a Checkpoints object, adds a listener, makes
808
+ * a change and then clears the checkpoints.
809
+ *
810
+ * ```js
811
+ * const store = createStore().setTables({pets: {fido: {sold: false}}});
812
+ *
813
+ * const checkpoints = createCheckpoints(store);
814
+ * console.log(checkpoints.getCheckpointIds());
815
+ * // -> [[], '0', []]
816
+ *
817
+ * const listenerId = checkpoints.addCheckpointIdsListener(() => {
818
+ * console.log('checkpoints changed');
819
+ * });
820
+ *
821
+ * store.setCell('pets', 'fido', 'color', 'brown');
822
+ * // -> 'checkpoints changed'
823
+ * checkpoints.addCheckpoint();
824
+ * // -> 'checkpoints changed'
825
+ * store.setCell('pets', 'fido', 'sold', true);
826
+ * // -> 'checkpoints changed'
827
+ * checkpoints.addCheckpoint();
828
+ * // -> 'checkpoints changed'
829
+ *
830
+ * console.log(store.getTables());
831
+ * // -> {pets: {fido: {sold: true, color: 'brown'}}}
832
+ * console.log(checkpoints.getCheckpointIds());
833
+ * // -> [['0', '1'], '2', []]
834
+ *
835
+ * checkpoints.clear();
836
+ * // -> 'checkpoints changed'
837
+ *
838
+ * console.log(store.getTables());
839
+ * // -> {pets: {fido: {sold: true, color: 'brown'}}}
840
+ * console.log(checkpoints.getCheckpointIds());
841
+ * // -> [[], '0', []]
842
+ * ```
843
+ * @category Lifecycle
844
+ */
845
+ clear(): Checkpoints;
846
+
847
+ /**
848
+ * The destroy method should be called when this Checkpoints object is no
849
+ * longer used.
850
+ *
851
+ * This guarantees that all of the listeners that the object registered with
852
+ * the underlying Store are removed and it can be correctly garbage collected.
853
+ *
854
+ * @example
855
+ * This example creates a Store, adds a Checkpoints object (that registers a
856
+ * CellListener with the underlying Store), and then destroys it again,
857
+ * removing the listener.
858
+ *
859
+ * ```js
860
+ * const store = createStore().setTables({pets: {fido: {sold: false}}});
861
+ *
862
+ * const checkpoints = createCheckpoints(store);
863
+ * console.log(store.getListenerStats().cell);
864
+ * // -> 1
865
+ *
866
+ * checkpoints.destroy();
867
+ *
868
+ * console.log(store.getListenerStats().cell);
869
+ * // -> 0
870
+ * ```
871
+ * @category Lifecycle
872
+ */
873
+ destroy(): void;
874
+
875
+ /**
876
+ * The getListenerStats method provides a set of statistics about the
877
+ * listeners registered with the Checkpoints object, and is used for debugging
878
+ * purposes.
879
+ *
880
+ * The CheckpointsListenerStats object contains a breakdown of the different
881
+ * types of listener.
882
+ *
883
+ * The statistics are only populated in a debug build: production builds
884
+ * return an empty object. The method is intended to be used during
885
+ * development to ensure your application is not leaking listener
886
+ * registrations, for example.
887
+ *
888
+ * @returns A CheckpointsListenerStats object containing Checkpoints listener
889
+ * statistics.
890
+ * @example
891
+ * This example gets the listener statistics of a Checkpoints object.
892
+ *
893
+ * ```js
894
+ * const store = createStore();
895
+ * const checkpoints = createCheckpoints(store);
896
+ * checkpoints.addCheckpointIdsListener(() => {
897
+ * console.log('Checkpoint Ids changed');
898
+ * });
899
+ * checkpoints.addCheckpointListener(null, () => {
900
+ * console.log('Checkpoint label changed');
901
+ * });
902
+ *
903
+ * console.log(checkpoints.getListenerStats());
904
+ * // -> {checkpointIds: 1, checkpoint: 1}
905
+ * ```
906
+ * @category Development
907
+ */
908
+ getListenerStats(): CheckpointsListenerStats;
909
+ }
910
+
911
+ /**
912
+ * The createCheckpoints function creates a Checkpoints object, and is the main
913
+ * entry point into the checkpoints module.
914
+ *
915
+ * It is trivially simple.
916
+ *
917
+ * A given Store can only have one Checkpoints object associated with it. If you
918
+ * call this function twice on the same Store, your second call will return a
919
+ * reference to the Checkpoints object created by the first.
920
+ *
921
+ * @param store The Store for which to set Checkpoints.
922
+ * @returns A reference to the new Checkpoints object.
923
+ * @example
924
+ * This example creates a Checkpoints object.
925
+ *
926
+ * ```js
927
+ * const store = createStore();
928
+ * const checkpoints = createCheckpoints(store);
929
+ * console.log(checkpoints.getCheckpointIds());
930
+ * // -> [[], '0', []]
931
+ * ```
932
+ * @example
933
+ * This example creates a Checkpoints object, and calls the method a second
934
+ * time for the same Store to return the same object.
935
+ *
936
+ * ```js
937
+ * const store = createStore();
938
+ * const checkpoints1 = createCheckpoints(store);
939
+ * const checkpoints2 = createCheckpoints(store);
940
+ * console.log(checkpoints1 === checkpoints2);
941
+ * // -> true
942
+ * ```
943
+ * @category Creation
944
+ */
945
+ export function createCheckpoints(store: Store): Checkpoints;