tinybase 2.0.0 → 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 +1 -1
  95. package/readme.md +1 -1
@@ -0,0 +1,717 @@
1
+ /**
2
+ * The persisters module of the TinyBase project provides a simple framework for
3
+ * saving and loading Store data, to and from different destinations, or
4
+ * underlying storage types.
5
+ *
6
+ * Several entry points are provided, each of which returns a new Persister
7
+ * object that can load and save a Store:
8
+ *
9
+ * - The createSessionPersister function returns a Persister that uses the
10
+ * browser's session storage.
11
+ * - The createLocalPersister function returns a Persister that uses the
12
+ * browser's local storage.
13
+ * - The createRemotePersister function returns a Persister that uses a remote
14
+ * server.
15
+ * - The createFilePersister function returns a Persister that uses a local file
16
+ * (in an appropriate environment).
17
+ *
18
+ * Since persistence requirements can be different for every app, the
19
+ * createCustomPersister function can also be used to easily create a fully
20
+ * customized way to save and load Store data.
21
+ *
22
+ * @see Persisting Data guide
23
+ * @see Countries demo
24
+ * @see Todo App demos
25
+ * @see Drawing demo
26
+ * @packageDocumentation
27
+ * @module persisters
28
+ */
29
+
30
+ import {Store, Tables} from './store.d';
31
+ import {Callback} from './common.d';
32
+
33
+ /**
34
+ * The PersisterStats type describes the number of times a Persister object has
35
+ * loaded or saved data.
36
+ *
37
+ * A PersisterStats object is returned from the getStats method, and is only
38
+ * populated in a debug build.
39
+ *
40
+ * @category Development
41
+ */
42
+ export type PersisterStats = {
43
+ /**
44
+ * The number of times data has been loaded.
45
+ */
46
+ loads?: number;
47
+ /**
48
+ * The number of times data has been saved.
49
+ */
50
+ saves?: number;
51
+ };
52
+
53
+ /**
54
+ * A Persister object lets you save and load Store data to and from different
55
+ * locations, or underlying storage types.
56
+ *
57
+ * This is useful for preserving Store data between browser sessions or reloads,
58
+ * saving or loading browser state to or from a server, or saving Store data to
59
+ * disk in a environment with filesystem access.
60
+ *
61
+ * Creating a Persister depends on the choice of underlying storage where the
62
+ * data is to be stored. Options include the createSessionPersister function,
63
+ * the createLocalPersister, the createRemotePersister function, and the
64
+ * createFilePersister function. The createCustomPersister function can also be
65
+ * used to easily create a fully customized way to save and load Store data.
66
+ *
67
+ * A Persister lets you explicit save or load data, with the save method and the
68
+ * load method respectively. These methods are both asynchronous (since the
69
+ * underlying data storage may also be) and return promises. As a result you
70
+ * should use the `await` keyword to call them in a way that guarantees
71
+ * subsequent execution order.
72
+ *
73
+ * When you don't want to deal with explicit persistence operations, a Persister
74
+ * object also provides automatic saving and loading. Automatic saving listens
75
+ * for changes to the Store and persists the data immediately. Automatic loading
76
+ * listens (or polls) for changes to the persisted data and reflects those
77
+ * changes in the Store.
78
+ *
79
+ * You can start automatic saving or loading with the startAutoSave method and
80
+ * startAutoLoad method. Both are asynchronous since they will do an immediate
81
+ * save and load before starting to listen for subsequent changes. You can stop
82
+ * the behavior with the stopAutoSave method and stopAutoLoad method (which are
83
+ * synchronous).
84
+ *
85
+ * You may often want to have both automatic saving and loading of a Store so
86
+ * that changes are constantly synchronized (allowing basic state preservation
87
+ * between browser tabs, for example). The framework has some basic provisions
88
+ * to prevent race conditions - for example it will not attempt to save data if
89
+ * it is currently loading it and vice-versa.
90
+ *
91
+ * Be aware, however, that the default implementations do not provide complex
92
+ * synchronization heuristics and you should comprehensively test your
93
+ * persistence strategy to understand the opportunity for data loss (in the case
94
+ * of trying to save data to a server under poor network conditions, for
95
+ * example).
96
+ *
97
+ * @example
98
+ * This example creates a Store, persists it to the browser's session storage as
99
+ * a JSON string, changes the persisted data, updates the Store from it, and
100
+ * finally destroys the Persister again.
101
+ *
102
+ * ```js
103
+ * const store = createStore().setTables({pets: {fido: {species: 'dog'}}});
104
+ * const persister = createSessionPersister(store, 'pets');
105
+ *
106
+ * await persister.save();
107
+ * console.log(sessionStorage.getItem('pets'));
108
+ * // -> '{"pets":{"fido":{"species":"dog"}}}'
109
+ *
110
+ * sessionStorage.setItem('pets', '{"pets":{"toto":{"species":"dog"}}}');
111
+ * await persister.load();
112
+ * console.log(store.getTables());
113
+ * // -> {pets: {toto: {species: 'dog'}}}
114
+ *
115
+ * persister.destroy();
116
+ * sessionStorage.clear();
117
+ * ```
118
+ * @example
119
+ * This example creates a Store, and automatically saves and loads it to the
120
+ * browser's session storage as a JSON string. Changes to the Store data, or the
121
+ * persisted data (implicitly firing a StorageEvent), are reflected accordingly.
122
+ *
123
+ * ```js
124
+ * const store = createStore();
125
+ * const persister = createSessionPersister(store, 'pets');
126
+ *
127
+ * await persister.startAutoLoad({pets: {fido: {species: 'dog'}}});
128
+ * await persister.startAutoSave();
129
+ *
130
+ * store.setTables({pets: {felix: {species: 'cat'}}});
131
+ * // ...
132
+ * console.log(sessionStorage.getItem('pets'));
133
+ * // -> '{"pets":{"felix":{"species":"cat"}}}'
134
+ *
135
+ * // In another browser tab:
136
+ * sessionStorage.setItem('pets', '{"pets":{"toto":{"species":"dog"}}}');
137
+ * // -> StorageEvent('storage', {storageArea: sessionStorage, key: 'pets'})
138
+ *
139
+ * // ...
140
+ * console.log(store.getTables());
141
+ * // -> {pets: {toto: {species: 'dog'}}}
142
+ *
143
+ * persister.destroy();
144
+ * sessionStorage.clear();
145
+ * ```
146
+ * @category Persister
147
+ */
148
+ export interface Persister {
149
+ /**
150
+ * The load method gets persisted data from storage, and loads it into the
151
+ * Store with which the Persister is associated, once.
152
+ *
153
+ * The optional parameter allows you to specify what the initial Tables object
154
+ * for the Store will be if there is nothing currently persisted. Using this
155
+ * instead of the `initialTables` parameter in the regular createStore
156
+ * function allows you to easily instantiate a Store whether it's loading from
157
+ * previously persisted storage or being run for the first time.
158
+ *
159
+ * This method is asynchronous because the persisted data may be on a remote
160
+ * machine or a filesystem. Even for those storage types that are synchronous
161
+ * (like browser storage) it is still recommended that you `await` calls to
162
+ * this method or handle the return type natively as a Promise.
163
+ *
164
+ * @param initialTables An optional Tables object used when the underlying
165
+ * storage has not previously been populated.
166
+ * @returns A Promise containing a reference to the Persister object.
167
+ * @example
168
+ * This example creates an empty Store, and loads data into it from the
169
+ * browser's session storage, which for the purposes of this example has been
170
+ * previously populated.
171
+ *
172
+ * ```js
173
+ * sessionStorage.setItem('pets', '{"pets":{"fido":{"species":"dog"}}}');
174
+ *
175
+ * const store = createStore();
176
+ * const persister = createSessionPersister(store, 'pets');
177
+ *
178
+ * await persister.load();
179
+ * console.log(store.getTables());
180
+ * // -> {pets: {fido: {species: 'dog'}}}
181
+ *
182
+ * sessionStorage.clear();
183
+ * ```
184
+ * @example
185
+ * This example creates an empty Store, and loads data into it from the
186
+ * browser's session storage, which is at first empty, so the optional
187
+ * parameter is used. The second time the load method is called, data has
188
+ * previously been persisted and instead, that is loaded.
189
+ *
190
+ * ```js
191
+ * const store = createStore();
192
+ * const persister = createSessionPersister(store, 'pets');
193
+ *
194
+ * await persister.load({pets: {fido: {species: 'dog'}}});
195
+ * console.log(store.getTables());
196
+ * // -> {pets: {fido: {species: 'dog'}}}
197
+ *
198
+ * sessionStorage.setItem('pets', '{"pets":{"toto":{"species":"dog"}}}');
199
+ * await persister.load({pets: {fido: {species: 'dog'}}});
200
+ * console.log(store.getTables());
201
+ * // -> {pets: {toto: {species: 'dog'}}}
202
+ *
203
+ * sessionStorage.clear();
204
+ * ```
205
+ * @category Load
206
+ */
207
+ load(initialTables?: Tables): Promise<Persister>;
208
+
209
+ /**
210
+ * The startAutoLoad method gets persisted data from storage, and loads it
211
+ * into the Store with which the Persister is associated, once, and then
212
+ * continuously.
213
+ *
214
+ * The optional parameter allows you to specify what the initial Tables object
215
+ * for the Store will be if there is nothing at first persisted. Using this
216
+ * instead of the `initialTables` parameter in the regular createStore
217
+ * function allows you to easily instantiate a Store whether it's loading from
218
+ * previously persisted storage or being run for the first time.
219
+ *
220
+ * This method first runs a single call to the load method to ensure the data
221
+ * is in sync with the persisted storage. It then continues to watch for
222
+ * changes to the underlying data (either through events or polling, depending
223
+ * on the storage type), automatically loading the data into the Store.
224
+ *
225
+ * This method is asynchronous because it starts by making a single call to
226
+ * the asynchronous load method. Even for those storage types that are
227
+ * synchronous (like browser storage) it is still recommended that you `await`
228
+ * calls to this method or handle the return type natively as a Promise.
229
+ *
230
+ * @param initialTables An optional Tables object used when the underlying
231
+ * storage has not previously been populated.
232
+ * @returns A Promise containing a reference to the Persister object.
233
+ * @example
234
+ * This example creates an empty Store, and loads data into it from the
235
+ * browser's session storage, which at first is empty (so the `initialTables`
236
+ * parameter is used). Subsequent changes to the underlying storage are then
237
+ * reflected in the Store (in this case through detection of StorageEvents
238
+ * from session storage changes made in another browser tab).
239
+ *
240
+ * ```js
241
+ * const store = createStore();
242
+ * const persister = createSessionPersister(store, 'pets');
243
+ *
244
+ * await persister.startAutoLoad({pets: {fido: {species: 'dog'}}});
245
+ * console.log(store.getTables());
246
+ * // -> {pets: {fido: {species: 'dog'}}}
247
+ *
248
+ * // In another browser tab:
249
+ * sessionStorage.setItem('pets', '{"pets":{"toto":{"species":"dog"}}}');
250
+ * // -> StorageEvent('storage', {storageArea: sessionStorage, key: 'pets'})
251
+ *
252
+ * // ...
253
+ * console.log(store.getTables());
254
+ * // -> {pets: {toto: {species: 'dog'}}}
255
+ *
256
+ * persister.destroy();
257
+ * sessionStorage.clear();
258
+ * ```
259
+ * @category Load
260
+ */
261
+ startAutoLoad(initialTables?: Tables): Promise<Persister>;
262
+
263
+ /**
264
+ * The stopAutoLoad method stops the automatic loading of data from storage
265
+ * previously started with the startAutoLoad method.
266
+ *
267
+ * If the Persister is not currently set to automatically load, this method
268
+ * has no effect.
269
+ *
270
+ * @returns A reference to the Persister object.
271
+ * @example
272
+ * This example creates an empty Store, and starts automatically loading data
273
+ * into it from the browser's session storage. Once the automatic loading is
274
+ * stopped, subsequent changes are not reflected in the Store.
275
+ *
276
+ * ```js
277
+ * const store = createStore();
278
+ * const persister = createSessionPersister(store, 'pets');
279
+ * await persister.startAutoLoad();
280
+ *
281
+ * // In another browser tab:
282
+ * sessionStorage.setItem('pets', '{"pets":{"toto":{"species":"dog"}}}');
283
+ * // -> StorageEvent('storage', {storageArea: sessionStorage, key: 'pets'})
284
+ * // ...
285
+ * console.log(store.getTables());
286
+ * // -> {pets: {toto: {species: 'dog'}}}
287
+ *
288
+ * persister.stopAutoLoad();
289
+ *
290
+ * // In another browser tab:
291
+ * sessionStorage.setItem('pets', '{"pets":{"felix":{"species":"cat"}}}');
292
+ * // -> StorageEvent('storage', {storageArea: sessionStorage, key: 'pets'})
293
+ * // ...
294
+ * console.log(store.getTables());
295
+ * // -> {pets: {toto: {species: 'dog'}}}
296
+ * // Storage change has not been automatically loaded.
297
+ *
298
+ * persister.destroy();
299
+ * sessionStorage.clear();
300
+ * ```
301
+ * @category Load
302
+ */
303
+ stopAutoLoad(): Persister;
304
+
305
+ /**
306
+ * The save method takes data from the Store with which the Persister is
307
+ * associated and persists it into storage, once.
308
+ *
309
+ * This method is asynchronous because the persisted data may be on a remote
310
+ * machine or a filesystem. Even for those storage types that are synchronous
311
+ * (like browser storage) it is still recommended that you `await` calls to
312
+ * this method or handle the return type natively as a Promise.
313
+ *
314
+ * @returns A Promise containing a reference to the Persister object.
315
+ * @example
316
+ * This example creates a Store with some data, and saves into the browser's
317
+ * session storage.
318
+ *
319
+ * ```js
320
+ * const store = createStore().setTables({pets: {fido: {species: 'dog'}}});
321
+ * const persister = createSessionPersister(store, 'pets');
322
+ *
323
+ * await persister.save();
324
+ * console.log(sessionStorage.getItem('pets'));
325
+ * // -> '{"pets":{"fido":{"species":"dog"}}}'
326
+ *
327
+ * persister.destroy();
328
+ * sessionStorage.clear();
329
+ * ```
330
+ * @category Save
331
+ */
332
+ save(): Promise<Persister>;
333
+
334
+ /**
335
+ * The save method takes data from the Store with which the Persister is
336
+ * associated and persists it into storage, once, and then continuously.
337
+ *
338
+ * This method first runs a single call to the save method to ensure the data
339
+ * is in sync with the persisted storage. It then continues to watch for
340
+ * changes to the Store, automatically saving the data to storage.
341
+ *
342
+ * This method is asynchronous because it starts by making a single call to
343
+ * the asynchronous save method. Even for those storage types that are
344
+ * synchronous (like browser storage) it is still recommended that you `await`
345
+ * calls to this method or handle the return type natively as a Promise.
346
+ *
347
+ * @returns A Promise containing a reference to the Persister object.
348
+ * @example
349
+ * This example creates a Store with some data, and saves into the browser's
350
+ * session storage. Subsequent changes to the Store are then automatically
351
+ * saved to the underlying storage.
352
+ *
353
+ * ```js
354
+ * const store = createStore().setTables({pets: {fido: {species: 'dog'}}});
355
+ * const persister = createSessionPersister(store, 'pets');
356
+ *
357
+ * await persister.startAutoSave();
358
+ * console.log(sessionStorage.getItem('pets'));
359
+ * // -> '{"pets":{"fido":{"species":"dog"}}}'
360
+ *
361
+ * store.setTables({pets: {toto: {species: 'dog'}}});
362
+ * // ...
363
+ * console.log(sessionStorage.getItem('pets'));
364
+ * // -> '{"pets":{"toto":{"species":"dog"}}}'
365
+ *
366
+ * sessionStorage.clear();
367
+ * ```
368
+ * @category Save
369
+ */
370
+ startAutoSave(): Promise<Persister>;
371
+
372
+ /**
373
+ * The stopAutoSave method stops the automatic save of data to storage
374
+ * previously started with the startAutoSave method.
375
+ *
376
+ * If the Persister is not currently set to automatically save, this method
377
+ * has no effect.
378
+ *
379
+ * @returns A reference to the Persister object.
380
+ * @example
381
+ * This example creates a Store with some data, and saves into the browser's
382
+ * session storage. Subsequent changes to the Store are then automatically
383
+ * saved to the underlying storage. Once the automatic saving is
384
+ * stopped, subsequent changes are not reflected.
385
+ *
386
+ * ```js
387
+ * const store = createStore().setTables({pets: {fido: {species: 'dog'}}});
388
+ * const persister = createSessionPersister(store, 'pets');
389
+ * await persister.startAutoSave();
390
+ *
391
+ * store.setTables({pets: {toto: {species: 'dog'}}});
392
+ * // ...
393
+ * console.log(sessionStorage.getItem('pets'));
394
+ * // -> '{"pets":{"toto":{"species":"dog"}}}'
395
+ *
396
+ * persister.stopAutoSave();
397
+ *
398
+ * store.setTables({pets: {felix: {species: 'cat'}}});
399
+ * // ...
400
+ * console.log(sessionStorage.getItem('pets'));
401
+ * // -> '{"pets":{"toto":{"species":"dog"}}}'
402
+ * // Store change has not been automatically saved.
403
+ *
404
+ * sessionStorage.clear();
405
+ * ```
406
+ * @category Save
407
+ */
408
+ stopAutoSave(): Persister;
409
+
410
+ /**
411
+ * The getStore method returns a reference to the underlying Store that is
412
+ * backing this Persister object.
413
+ *
414
+ * @returns A reference to the Store.
415
+ * @example
416
+ * This example creates a Persister object against a newly-created Store and
417
+ * then gets its reference in order to update its data.
418
+ *
419
+ * ```js
420
+ * const persister = createSessionPersister(createStore(), 'pets');
421
+ * await persister.startAutoSave();
422
+ *
423
+ * persister.getStore().setTables({pets: {fido: {species: 'dog'}}});
424
+ * // ...
425
+ * console.log(sessionStorage.getItem('pets'));
426
+ * // -> '{"pets":{"fido":{"species":"dog"}}}'
427
+ *
428
+ * sessionStorage.clear();
429
+ * ```
430
+ * @category Getter
431
+ */
432
+ getStore(): Store;
433
+
434
+ /**
435
+ * The destroy method should be called when this Persister object is no longer
436
+ * used.
437
+ *
438
+ * This guarantees that all of the listeners that the object registered with
439
+ * the underlying Store and storage are removed and it can be correctly
440
+ * garbage collected. It is equivalent to running the stopAutoLoad method and
441
+ * the stopAutoSave method in succession.
442
+ *
443
+ * @example
444
+ * This example creates a Store, associates a Persister object with it (that
445
+ * registers a TablesListener with the underlying Store), and then destroys it
446
+ * again, removing the listener.
447
+ *
448
+ * ```js
449
+ * const store = createStore();
450
+ * const persister = createSessionPersister(store, 'pets');
451
+ * await persister.startAutoSave();
452
+ *
453
+ * console.log(store.getListenerStats().tables);
454
+ * // -> 1
455
+ *
456
+ * persister.destroy();
457
+ *
458
+ * console.log(store.getListenerStats().tables);
459
+ * // -> 0
460
+ * ```
461
+ * @category Lifecycle
462
+ */
463
+ destroy(): Persister;
464
+
465
+ /**
466
+ * The getStats method provides a set of statistics about the Persister, and
467
+ * is used for debugging purposes.
468
+ *
469
+ * The PersisterStats object contains a count of the number of times the
470
+ * Persister has loaded and saved data.
471
+ *
472
+ * The statistics are only populated in a debug build: production builds
473
+ * return an empty object. The method is intended to be used during
474
+ * development to ensure your persistence layer is acting as expected, for
475
+ * example.
476
+ *
477
+ * @returns A PersisterStats object containing Persister load and save
478
+ * statistics.
479
+ * @example
480
+ * This example gets the load and save statistics of a Persister object.
481
+ * Remember that the startAutoLoad method invokes an explicit load when it
482
+ * starts, and the startAutoSave method invokes an explicit save when it
483
+ * starts - so those numbers are included in addition to the loads and saves
484
+ * invoked by changes to the Store and to the underlying storage.
485
+ *
486
+ * ```js
487
+ * const store = createStore();
488
+ * const persister = createSessionPersister(store, 'pets');
489
+ *
490
+ * await persister.startAutoLoad({pets: {fido: {species: 'dog'}}});
491
+ * await persister.startAutoSave();
492
+ *
493
+ * store.setTables({pets: {felix: {species: 'cat'}}});
494
+ * // ...
495
+ *
496
+ * sessionStorage.setItem('pets', '{"pets":{"toto":{"species":"dog"}}}');
497
+ * // -> StorageEvent('storage', {storageArea: sessionStorage, key: 'pets'})
498
+ * // ...
499
+ *
500
+ * console.log(persister.getStats());
501
+ * // -> {loads: 2, saves: 2}
502
+ *
503
+ * persister.destroy();
504
+ * sessionStorage.clear();
505
+ * ```
506
+ * @category Development
507
+ */
508
+ getStats(): PersisterStats;
509
+ }
510
+
511
+ /**
512
+ * The createSessionPersister function creates a Persister object that can
513
+ * persist the Store to the browser's session storage.
514
+ *
515
+ * As well as providing a reference to the Store to persist, you must provide a
516
+ * `storageName` parameter which is unique to your application. This is the key
517
+ * that the browser uses to identify the storage location.
518
+ *
519
+ * @param store The Store to persist.
520
+ * @param storageName The unique key to identify the storage location.
521
+ * @returns A reference to the new Persister object.
522
+ * @example
523
+ * This example creates a Persister object and persists the Store to the
524
+ * browser's session storage.
525
+ *
526
+ * ```js
527
+ * const store = createStore().setTables({pets: {fido: {species: 'dog'}}});
528
+ * const persister = createSessionPersister(store, 'pets');
529
+ *
530
+ * await persister.save();
531
+ * console.log(sessionStorage.getItem('pets'));
532
+ * // -> '{"pets":{"fido":{"species":"dog"}}}'
533
+ *
534
+ * persister.destroy();
535
+ * sessionStorage.clear();
536
+ * ```
537
+ * @category Creation
538
+ */
539
+ export function createSessionPersister(
540
+ store: Store,
541
+ storageName: string,
542
+ ): Persister;
543
+
544
+ /**
545
+ * The createLocalPersister function creates a Persister object that can
546
+ * persist the Store to the browser's local storage.
547
+ *
548
+ * As well as providing a reference to the Store to persist, you must provide a
549
+ * `storageName` parameter which is unique to your application. This is the key
550
+ * that the browser uses to identify the storage location.
551
+ *
552
+ * @param store The Store to persist.
553
+ * @param storageName The unique key to identify the storage location.
554
+ * @returns A reference to the new Persister object.
555
+ * @example
556
+ * This example creates a Persister object and persists the Store to the
557
+ * browser's local storage.
558
+ *
559
+ * ```js
560
+ * const store = createStore().setTables({pets: {fido: {species: 'dog'}}});
561
+ * const persister = createLocalPersister(store, 'pets');
562
+ *
563
+ * await persister.save();
564
+ * console.log(localStorage.getItem('pets'));
565
+ * // -> '{"pets":{"fido":{"species":"dog"}}}'
566
+ *
567
+ * persister.destroy();
568
+ * localStorage.clear();
569
+ * ```
570
+ * @category Creation
571
+ */
572
+ export function createLocalPersister(
573
+ store: Store,
574
+ storageName: string,
575
+ ): Persister;
576
+
577
+ /**
578
+ * The createRemotePersister function creates a Persister object that can
579
+ * persist the Store to a remote server.
580
+ *
581
+ * As well as providing a reference to the Store to persist, you must provide
582
+ * `loadUrl` and `saveUrl` parameters. These identify the endpoints of the
583
+ * server that support the `GET` method (to fetch the Store JSON to load) and
584
+ * the `POST` method (to send the Store JSON to save) respectively.
585
+ *
586
+ * For when you choose to enable automatic loading for the Persister (with the
587
+ * startAutoLoad method), it will poll the loadUrl for changes. The
588
+ * `autoLoadIntervalSeconds` method is used to indicate how often to do this.
589
+ *
590
+ * @param store The Store to persist.
591
+ * @param loadUrl The endpoint that supports a `GET` method to load JSON.
592
+ * @param saveUrl The endpoint that supports a `POST` method to save JSON.
593
+ * @param autoLoadIntervalSeconds How often to poll the `loadUrl` when
594
+ * automatically loading changes from the server.
595
+ * @returns A reference to the new Persister object.
596
+ * @example
597
+ * This example creates a Persister object and persists the Store to a remote
598
+ * server.
599
+ *
600
+ * ```js yolo
601
+ * const store = createStore().setTables({pets: {fido: {species: 'dog'}}});
602
+ * const persister = createRemotePersister(
603
+ * store,
604
+ * 'https://example.com/load',
605
+ * 'https://example.com/save',
606
+ * 5,
607
+ * );
608
+ *
609
+ * await persister.save();
610
+ * // Store JSON will be sent to server in a POST request.
611
+ *
612
+ * await persister.load();
613
+ * // Store JSON will be fetched from server with a GET request.
614
+ *
615
+ * persister.destroy();
616
+ * ```
617
+ * @category Creation
618
+ */
619
+ export function createRemotePersister(
620
+ store: Store,
621
+ loadUrl: string,
622
+ saveUrl: string,
623
+ autoLoadIntervalSeconds: number,
624
+ ): Persister;
625
+
626
+ /**
627
+ * The createFilePersister function creates a Persister object that can persist
628
+ * the Store to a local file (in an appropriate environment).
629
+ *
630
+ * As well as providing a reference to the Store to persist, you must provide
631
+ * `filePath` parameter which identifies the file to persist it to.
632
+ *
633
+ * @param store The Store to persist.
634
+ * @param filePath The location of the local file to persist the Store to.
635
+ * @returns A reference to the new Persister object.
636
+ * @example
637
+ * This example creates a Persister object and persists the Store to a local
638
+ * file.
639
+ *
640
+ * ```js yolo
641
+ * const store = createStore().setTables({pets: {fido: {species: 'dog'}}});
642
+ * const persister = createFilePersister(store, '/app/persisted.json');
643
+ *
644
+ * await persister.save();
645
+ * // Store JSON will be saved to the file.
646
+ *
647
+ * await persister.load();
648
+ * // Store JSON will be loaded from the file.
649
+ *
650
+ * persister.destroy();
651
+ * ```
652
+ * @category Creation
653
+ */
654
+ export function createFilePersister(store: Store, filePath: string): Persister;
655
+
656
+ /**
657
+ * The createCustomPersister function creates a Persister object that you can
658
+ * configure to persist the Store in any way you wish.
659
+ *
660
+ * As well as providing a reference to the Store to persist, you must provide
661
+ * functions that handle how to fetch, write, and listen to, the persistence
662
+ * layer.
663
+ *
664
+ * The other creation functions (such as the createSessionPersister function and
665
+ * createFilePersister function, for example) all use this function under the
666
+ * covers. See those implementations for ideas on how to implement your own
667
+ * Persister types.
668
+ *
669
+ * @param store The Store to persist.
670
+ * @param getPersisted An asynchronous function which will fetch JSON from the
671
+ * persistence layer (or `null` or `undefined` if not present).
672
+ * @param setPersisted An asynchronous function which will send JSON to the
673
+ * persistence layer.
674
+ * @param startListeningToPersisted A function that will register a `didChange`
675
+ * listener on underlying changes to the persistence layer.
676
+ * @param stopListeningToPersisted A function that will unregister the listener
677
+ * from the underlying changes to the persistence layer.
678
+ * @returns A reference to the new Persister object.
679
+ * @example
680
+ * This example creates a custom Persister object and persists the Store to a
681
+ * local string called `storeJson` and which would automatically load by polling
682
+ * for changes every second.
683
+ *
684
+ * ```js
685
+ * const store = createStore().setTables({pets: {fido: {species: 'dog'}}});
686
+ * let storeJson;
687
+ * let interval;
688
+ *
689
+ * const persister = createCustomPersister(
690
+ * store,
691
+ * async () => storeJson,
692
+ * async (json) => (storeJson = json),
693
+ * (didChange) => (interval = setInterval(didChange, 1000)),
694
+ * () => clearInterval(interval),
695
+ * );
696
+ *
697
+ * await persister.save();
698
+ * console.log(storeJson);
699
+ * // -> '{"pets":{"fido":{"species":"dog"}}}'
700
+ *
701
+ * storeJson = '{"pets":{"fido":{"species":"dog","color":"brown"}}}';
702
+ * await persister.load();
703
+ *
704
+ * console.log(store.getTables());
705
+ * // -> {pets: {fido: {species: 'dog', color: 'brown'}}}
706
+ *
707
+ * persister.destroy();
708
+ * ```
709
+ * @category Creation
710
+ */
711
+ export function createCustomPersister(
712
+ store: Store,
713
+ getPersisted: () => Promise<string | null | undefined>,
714
+ setPersisted: (json: string) => Promise<void>,
715
+ startListeningToPersisted: (didChange: Callback) => void,
716
+ stopListeningToPersisted: Callback,
717
+ ): Persister;