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