tinybase 1.0.1 → 1.0.2

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