tinybase 2.0.0-beta.5 → 2.0.1

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