tinybase 0.0.0 → 0.9.3

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