tinybase 4.0.0-beta.0 → 4.0.0-beta.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.
- package/bin/cli.js +1 -1
- package/lib/checkpoints.js +1 -1
- package/lib/checkpoints.js.gz +0 -0
- package/lib/cjs/checkpoints.cjs +1 -1
- package/lib/cjs/checkpoints.cjs.gz +0 -0
- package/lib/cjs/indexes.cjs +1 -1
- package/lib/cjs/indexes.cjs.gz +0 -0
- package/lib/cjs/metrics.cjs +1 -1
- package/lib/cjs/metrics.cjs.gz +0 -0
- package/lib/cjs/persister-browser.cjs +1 -0
- package/lib/cjs/persister-browser.cjs.gz +0 -0
- package/lib/cjs/persister-file.cjs +1 -0
- package/lib/cjs/persister-file.cjs.gz +0 -0
- package/lib/cjs/persister-remote.cjs +1 -0
- package/lib/cjs/persister-remote.cjs.gz +0 -0
- package/lib/cjs/persister-yjs.cjs +1 -1
- package/lib/cjs/persister-yjs.cjs.gz +0 -0
- package/lib/cjs/persisters.cjs +1 -1
- package/lib/cjs/persisters.cjs.gz +0 -0
- package/lib/cjs/queries.cjs +1 -1
- package/lib/cjs/queries.cjs.gz +0 -0
- package/lib/cjs/relationships.cjs +1 -1
- package/lib/cjs/relationships.cjs.gz +0 -0
- package/lib/cjs/store.cjs +1 -1
- package/lib/cjs/store.cjs.gz +0 -0
- package/lib/cjs/tinybase.cjs +1 -1
- package/lib/cjs/tinybase.cjs.gz +0 -0
- package/lib/cjs/tools.cjs +1 -1
- package/lib/cjs/tools.cjs.gz +0 -0
- package/lib/cjs/ui-react.cjs +1 -1
- package/lib/cjs/ui-react.cjs.gz +0 -0
- package/lib/cjs-es6/checkpoints.cjs +1 -1
- package/lib/cjs-es6/checkpoints.cjs.gz +0 -0
- package/lib/cjs-es6/indexes.cjs +1 -1
- package/lib/cjs-es6/indexes.cjs.gz +0 -0
- package/lib/cjs-es6/metrics.cjs +1 -1
- package/lib/cjs-es6/metrics.cjs.gz +0 -0
- package/lib/cjs-es6/persister-browser.cjs +1 -0
- package/lib/cjs-es6/persister-browser.cjs.gz +0 -0
- package/lib/cjs-es6/persister-file.cjs +1 -0
- package/lib/cjs-es6/persister-file.cjs.gz +0 -0
- package/lib/cjs-es6/persister-remote.cjs +1 -0
- package/lib/cjs-es6/persister-remote.cjs.gz +0 -0
- package/lib/cjs-es6/persister-yjs.cjs +1 -1
- package/lib/cjs-es6/persister-yjs.cjs.gz +0 -0
- package/lib/cjs-es6/persisters.cjs +1 -1
- package/lib/cjs-es6/persisters.cjs.gz +0 -0
- package/lib/cjs-es6/queries.cjs +1 -1
- package/lib/cjs-es6/queries.cjs.gz +0 -0
- package/lib/cjs-es6/relationships.cjs +1 -1
- package/lib/cjs-es6/relationships.cjs.gz +0 -0
- package/lib/cjs-es6/store.cjs +1 -1
- package/lib/cjs-es6/store.cjs.gz +0 -0
- package/lib/cjs-es6/tinybase.cjs +1 -1
- package/lib/cjs-es6/tinybase.cjs.gz +0 -0
- package/lib/cjs-es6/tools.cjs +1 -1
- package/lib/cjs-es6/tools.cjs.gz +0 -0
- package/lib/cjs-es6/ui-react.cjs +1 -1
- package/lib/cjs-es6/ui-react.cjs.gz +0 -0
- package/lib/debug/checkpoints.js +3 -3
- package/lib/debug/indexes.js +3 -3
- package/lib/debug/metrics.js +3 -3
- package/lib/debug/persister-browser.js +147 -0
- package/lib/debug/persister-file.js +140 -0
- package/lib/debug/persister-remote.js +159 -0
- package/lib/debug/persister-yjs.js +234 -40
- package/lib/debug/persisters.js +43 -133
- package/lib/debug/queries.js +5 -5
- package/lib/debug/relationships.js +3 -3
- package/lib/debug/store.js +137 -87
- package/lib/debug/tinybase.js +179 -215
- package/lib/debug/tools.js +9 -9
- package/lib/es6/checkpoints.js +1 -1
- package/lib/es6/checkpoints.js.gz +0 -0
- package/lib/es6/indexes.js +1 -1
- package/lib/es6/indexes.js.gz +0 -0
- package/lib/es6/metrics.js +1 -1
- package/lib/es6/metrics.js.gz +0 -0
- package/lib/es6/persister-browser.js +1 -0
- package/lib/es6/persister-browser.js.gz +0 -0
- package/lib/es6/persister-file.js +1 -0
- package/lib/es6/persister-file.js.gz +0 -0
- package/lib/es6/persister-remote.js +1 -0
- package/lib/es6/persister-remote.js.gz +0 -0
- package/lib/es6/persister-yjs.js +1 -1
- package/lib/es6/persister-yjs.js.gz +0 -0
- package/lib/es6/persisters.js +1 -1
- package/lib/es6/persisters.js.gz +0 -0
- package/lib/es6/queries.js +1 -1
- package/lib/es6/queries.js.gz +0 -0
- package/lib/es6/relationships.js +1 -1
- package/lib/es6/relationships.js.gz +0 -0
- package/lib/es6/store.js +1 -1
- package/lib/es6/store.js.gz +0 -0
- package/lib/es6/tinybase.js +1 -1
- package/lib/es6/tinybase.js.gz +0 -0
- package/lib/es6/tools.js +1 -1
- package/lib/es6/tools.js.gz +0 -0
- package/lib/es6/ui-react.js +1 -1
- package/lib/es6/ui-react.js.gz +0 -0
- package/lib/indexes.js +1 -1
- package/lib/indexes.js.gz +0 -0
- package/lib/metrics.js +1 -1
- package/lib/metrics.js.gz +0 -0
- package/lib/persister-browser.js +1 -0
- package/lib/persister-browser.js.gz +0 -0
- package/lib/persister-file.js +1 -0
- package/lib/persister-file.js.gz +0 -0
- package/lib/persister-remote.js +1 -0
- package/lib/persister-remote.js.gz +0 -0
- package/lib/persister-yjs.js +1 -1
- package/lib/persister-yjs.js.gz +0 -0
- package/lib/persisters.js +1 -1
- package/lib/persisters.js.gz +0 -0
- package/lib/queries.js +1 -1
- package/lib/queries.js.gz +0 -0
- package/lib/relationships.js +1 -1
- package/lib/relationships.js.gz +0 -0
- package/lib/store.js +1 -1
- package/lib/store.js.gz +0 -0
- package/lib/tinybase.js +1 -1
- package/lib/tinybase.js.gz +0 -0
- package/lib/tools.js +1 -1
- package/lib/tools.js.gz +0 -0
- package/lib/types/persister-browser.d.ts +85 -0
- package/lib/types/persister-file.d.ts +41 -0
- package/lib/types/persister-remote.d.ts +60 -0
- package/lib/types/persister-yjs.d.ts +70 -8
- package/lib/types/persisters.d.ts +73 -181
- package/lib/types/queries.d.ts +52 -52
- package/lib/types/store.d.ts +499 -139
- package/lib/types/tools.d.ts +16 -16
- package/lib/types/ui-react.d.ts +56 -47
- package/lib/types/with-schemas/internal/ui-react.d.ts +2 -2
- package/lib/types/with-schemas/persister-browser.d.ts +103 -0
- package/lib/types/with-schemas/persister-file.d.ts +50 -0
- package/lib/types/with-schemas/persister-remote.d.ts +71 -0
- package/lib/types/with-schemas/persister-yjs.d.ts +72 -9
- package/lib/types/with-schemas/persisters.d.ts +99 -223
- package/lib/types/with-schemas/queries.d.ts +52 -52
- package/lib/types/with-schemas/store.d.ts +595 -153
- package/lib/types/with-schemas/tools.d.ts +16 -16
- package/lib/types/with-schemas/ui-react.d.ts +54 -45
- package/lib/ui-react.js +1 -1
- package/lib/ui-react.js.gz +0 -0
- package/lib/umd/checkpoints.js +1 -1
- package/lib/umd/checkpoints.js.gz +0 -0
- package/lib/umd/indexes.js +1 -1
- package/lib/umd/indexes.js.gz +0 -0
- package/lib/umd/metrics.js +1 -1
- package/lib/umd/metrics.js.gz +0 -0
- package/lib/umd/persister-browser.js +1 -0
- package/lib/umd/persister-browser.js.gz +0 -0
- package/lib/umd/persister-file.js +1 -0
- package/lib/umd/persister-file.js.gz +0 -0
- package/lib/umd/persister-remote.js +1 -0
- package/lib/umd/persister-remote.js.gz +0 -0
- package/lib/umd/persister-yjs.js +1 -1
- package/lib/umd/persister-yjs.js.gz +0 -0
- package/lib/umd/persisters.js +1 -1
- package/lib/umd/persisters.js.gz +0 -0
- package/lib/umd/queries.js +1 -1
- package/lib/umd/queries.js.gz +0 -0
- package/lib/umd/relationships.js +1 -1
- package/lib/umd/relationships.js.gz +0 -0
- package/lib/umd/store.js +1 -1
- package/lib/umd/store.js.gz +0 -0
- package/lib/umd/tinybase.js +1 -1
- package/lib/umd/tinybase.js.gz +0 -0
- package/lib/umd/tools.js +1 -1
- package/lib/umd/tools.js.gz +0 -0
- package/lib/umd/ui-react.js +1 -1
- package/lib/umd/ui-react.js.gz +0 -0
- package/lib/umd-es6/checkpoints.js +1 -1
- package/lib/umd-es6/checkpoints.js.gz +0 -0
- package/lib/umd-es6/indexes.js +1 -1
- package/lib/umd-es6/indexes.js.gz +0 -0
- package/lib/umd-es6/metrics.js +1 -1
- package/lib/umd-es6/metrics.js.gz +0 -0
- package/lib/umd-es6/persister-browser.js +1 -0
- package/lib/umd-es6/persister-browser.js.gz +0 -0
- package/lib/umd-es6/persister-file.js +1 -0
- package/lib/umd-es6/persister-file.js.gz +0 -0
- package/lib/umd-es6/persister-remote.js +1 -0
- package/lib/umd-es6/persister-remote.js.gz +0 -0
- package/lib/umd-es6/persister-yjs.js +1 -1
- package/lib/umd-es6/persister-yjs.js.gz +0 -0
- package/lib/umd-es6/persisters.js +1 -1
- package/lib/umd-es6/persisters.js.gz +0 -0
- package/lib/umd-es6/queries.js +1 -1
- package/lib/umd-es6/queries.js.gz +0 -0
- package/lib/umd-es6/relationships.js +1 -1
- package/lib/umd-es6/relationships.js.gz +0 -0
- package/lib/umd-es6/store.js +1 -1
- package/lib/umd-es6/store.js.gz +0 -0
- package/lib/umd-es6/tinybase.js +1 -1
- package/lib/umd-es6/tinybase.js.gz +0 -0
- package/lib/umd-es6/tools.js +1 -1
- package/lib/umd-es6/tools.js.gz +0 -0
- package/lib/umd-es6/ui-react.js +1 -1
- package/lib/umd-es6/ui-react.js.gz +0 -0
- package/package.json +15 -15
- package/readme.md +2 -2
|
@@ -3,21 +3,21 @@
|
|
|
3
3
|
* saving and loading Store data, to and from different destinations, or
|
|
4
4
|
* underlying storage types.
|
|
5
5
|
*
|
|
6
|
-
* Several entry points are provided
|
|
7
|
-
* object that can load and save a Store:
|
|
8
|
-
*
|
|
9
|
-
* - The createSessionPersister function
|
|
10
|
-
* browser's session storage.
|
|
11
|
-
* - The createLocalPersister function
|
|
12
|
-
* browser's local storage.
|
|
13
|
-
* - The createRemotePersister function
|
|
14
|
-
* server.
|
|
15
|
-
* - The createFilePersister function
|
|
16
|
-
* (in an appropriate environment).
|
|
6
|
+
* Several entry points are provided (in separately installed modules), each of
|
|
7
|
+
* which returns a new Persister object that can load and save a Store:
|
|
8
|
+
*
|
|
9
|
+
* - The createSessionPersister function (in the persister-browser module)
|
|
10
|
+
* returns a Persister that uses the browser's session storage.
|
|
11
|
+
* - The createLocalPersister function (in the persister-browser module) returns
|
|
12
|
+
* a Persister that uses the browser's local storage.
|
|
13
|
+
* - The createRemotePersister function (in the persister-remote module) returns
|
|
14
|
+
* a Persister that uses a remote server.
|
|
15
|
+
* - The createFilePersister function (in the persister-file module) returns a
|
|
16
|
+
* Persister that uses a local file (in an appropriate environment).
|
|
17
17
|
*
|
|
18
18
|
* Since persistence requirements can be different for every app, the
|
|
19
|
-
* createCustomPersister function can also be used to easily
|
|
20
|
-
* customized way to save and load Store data.
|
|
19
|
+
* createCustomPersister function in this module can also be used to easily
|
|
20
|
+
* create a fully customized way to save and load Store data.
|
|
21
21
|
*
|
|
22
22
|
* @see Persisting Data guide
|
|
23
23
|
* @see Countries demo
|
|
@@ -27,8 +27,7 @@
|
|
|
27
27
|
* @module persisters
|
|
28
28
|
*/
|
|
29
29
|
|
|
30
|
-
import {Store, Tables, Values} from './store.d';
|
|
31
|
-
import {Callback} from './common.d';
|
|
30
|
+
import {GetTransactionChanges, Store, Tables, Values} from './store.d';
|
|
32
31
|
|
|
33
32
|
/**
|
|
34
33
|
* The PersisterStats type describes the number of times a Persister object has
|
|
@@ -50,6 +49,27 @@ export type PersisterStats = {
|
|
|
50
49
|
saves?: number;
|
|
51
50
|
};
|
|
52
51
|
|
|
52
|
+
/**
|
|
53
|
+
* A PersisterListener is a callback that lets a Persister inform the Store that
|
|
54
|
+
* a change has happened to the underlying data.
|
|
55
|
+
*
|
|
56
|
+
* If the listener has the `getTransactionChanges` parameter, it will be used to
|
|
57
|
+
* make an incremental change to the Store. If not, but the `getContent`
|
|
58
|
+
* function _is_ available, that will be used to make a wholesale change to the
|
|
59
|
+
* Store. If neither are present, the content will be loaded from the
|
|
60
|
+
* Persister's load method.
|
|
61
|
+
*
|
|
62
|
+
* @param getContent An optional function that, if provided, returns an array of
|
|
63
|
+
* Store content and can be used to immediately wholesale update the Store.
|
|
64
|
+
* @param getTransactionChanges An optional function that, if provided, returns
|
|
65
|
+
* a TransactionChanges object and can be used to immediately incrementally
|
|
66
|
+
* update the Store.
|
|
67
|
+
*/
|
|
68
|
+
export type PersisterListener = (
|
|
69
|
+
getContent?: () => [Tables, Values],
|
|
70
|
+
getTransactionChanges?: GetTransactionChanges,
|
|
71
|
+
) => void;
|
|
72
|
+
|
|
53
73
|
/**
|
|
54
74
|
* A Persister object lets you save and load Store data to and from different
|
|
55
75
|
* locations, or underlying storage types.
|
|
@@ -165,7 +185,7 @@ export interface Persister {
|
|
|
165
185
|
* @param initialTables An optional Tables object used when the underlying
|
|
166
186
|
* storage has not previously been populated.
|
|
167
187
|
* @param initialValues An optional Values object used when the underlying
|
|
168
|
-
* storage has not previously been populated, since v3.0.
|
|
188
|
+
* storage has not previously been populated, since v3.0.
|
|
169
189
|
* @returns A Promise containing a reference to the Persister object.
|
|
170
190
|
* @example
|
|
171
191
|
* This example creates an empty Store, and loads data into it from the
|
|
@@ -233,7 +253,7 @@ export interface Persister {
|
|
|
233
253
|
* @param initialTables An optional Tables object used when the underlying
|
|
234
254
|
* storage has not previously been populated.
|
|
235
255
|
* @param initialValues An optional Values object used when the underlying
|
|
236
|
-
* storage has not previously been populated, since v3.0.
|
|
256
|
+
* storage has not previously been populated, since v3.0.
|
|
237
257
|
* @returns A Promise containing a reference to the Persister object.
|
|
238
258
|
* @example
|
|
239
259
|
* This example creates an empty Store, and loads data into it from the
|
|
@@ -461,12 +481,12 @@ export interface Persister {
|
|
|
461
481
|
* const persister = createSessionPersister(store, 'pets');
|
|
462
482
|
* await persister.startAutoSave();
|
|
463
483
|
*
|
|
464
|
-
* console.log(store.getListenerStats().
|
|
484
|
+
* console.log(store.getListenerStats().transaction);
|
|
465
485
|
* // -> 1
|
|
466
486
|
*
|
|
467
487
|
* persister.destroy();
|
|
468
488
|
*
|
|
469
|
-
* console.log(store.getListenerStats().
|
|
489
|
+
* console.log(store.getListenerStats().transaction);
|
|
470
490
|
* // -> 0
|
|
471
491
|
* ```
|
|
472
492
|
* @category Lifecycle
|
|
@@ -520,151 +540,6 @@ export interface Persister {
|
|
|
520
540
|
//
|
|
521
541
|
}
|
|
522
542
|
|
|
523
|
-
/**
|
|
524
|
-
* The createSessionPersister function creates a Persister object that can
|
|
525
|
-
* persist the Store to the browser's session storage.
|
|
526
|
-
*
|
|
527
|
-
* As well as providing a reference to the Store to persist, you must provide a
|
|
528
|
-
* `storageName` parameter which is unique to your application. This is the key
|
|
529
|
-
* that the browser uses to identify the storage location.
|
|
530
|
-
*
|
|
531
|
-
* @param store The Store to persist.
|
|
532
|
-
* @param storageName The unique key to identify the storage location.
|
|
533
|
-
* @returns A reference to the new Persister object.
|
|
534
|
-
* @example
|
|
535
|
-
* This example creates a Persister object and persists the Store to the
|
|
536
|
-
* browser's session storage.
|
|
537
|
-
*
|
|
538
|
-
* ```js
|
|
539
|
-
* const store = createStore().setTables({pets: {fido: {species: 'dog'}}});
|
|
540
|
-
* const persister = createSessionPersister(store, 'pets');
|
|
541
|
-
*
|
|
542
|
-
* await persister.save();
|
|
543
|
-
* console.log(sessionStorage.getItem('pets'));
|
|
544
|
-
* // -> '[{"pets":{"fido":{"species":"dog"}}},{}]'
|
|
545
|
-
*
|
|
546
|
-
* persister.destroy();
|
|
547
|
-
* sessionStorage.clear();
|
|
548
|
-
* ```
|
|
549
|
-
* @category Creation
|
|
550
|
-
*/
|
|
551
|
-
export function createSessionPersister(
|
|
552
|
-
store: Store,
|
|
553
|
-
storageName: string,
|
|
554
|
-
): Persister;
|
|
555
|
-
|
|
556
|
-
/**
|
|
557
|
-
* The createLocalPersister function creates a Persister object that can
|
|
558
|
-
* persist the Store to the browser's local storage.
|
|
559
|
-
*
|
|
560
|
-
* As well as providing a reference to the Store to persist, you must provide a
|
|
561
|
-
* `storageName` parameter which is unique to your application. This is the key
|
|
562
|
-
* that the browser uses to identify the storage location.
|
|
563
|
-
*
|
|
564
|
-
* @param store The Store to persist.
|
|
565
|
-
* @param storageName The unique key to identify the storage location.
|
|
566
|
-
* @returns A reference to the new Persister object.
|
|
567
|
-
* @example
|
|
568
|
-
* This example creates a Persister object and persists the Store to the
|
|
569
|
-
* browser's local storage.
|
|
570
|
-
*
|
|
571
|
-
* ```js
|
|
572
|
-
* const store = createStore().setTables({pets: {fido: {species: 'dog'}}});
|
|
573
|
-
* const persister = createLocalPersister(store, 'pets');
|
|
574
|
-
*
|
|
575
|
-
* await persister.save();
|
|
576
|
-
* console.log(localStorage.getItem('pets'));
|
|
577
|
-
* // -> '[{"pets":{"fido":{"species":"dog"}}},{}]'
|
|
578
|
-
*
|
|
579
|
-
* persister.destroy();
|
|
580
|
-
* localStorage.clear();
|
|
581
|
-
* ```
|
|
582
|
-
* @category Creation
|
|
583
|
-
*/
|
|
584
|
-
export function createLocalPersister(
|
|
585
|
-
store: Store,
|
|
586
|
-
storageName: string,
|
|
587
|
-
): Persister;
|
|
588
|
-
|
|
589
|
-
/**
|
|
590
|
-
* The createRemotePersister function creates a Persister object that can
|
|
591
|
-
* persist the Store to a remote server.
|
|
592
|
-
*
|
|
593
|
-
* As well as providing a reference to the Store to persist, you must provide
|
|
594
|
-
* `loadUrl` and `saveUrl` parameters. These identify the endpoints of the
|
|
595
|
-
* server that support the `GET` method (to fetch the Store JSON to load) and
|
|
596
|
-
* the `POST` method (to send the Store JSON to save) respectively.
|
|
597
|
-
*
|
|
598
|
-
* For when you choose to enable automatic loading for the Persister (with the
|
|
599
|
-
* startAutoLoad method), it will poll the loadUrl for changes. The
|
|
600
|
-
* `autoLoadIntervalSeconds` method is used to indicate how often to do this.
|
|
601
|
-
*
|
|
602
|
-
* @param store The Store to persist.
|
|
603
|
-
* @param loadUrl The endpoint that supports a `GET` method to load JSON.
|
|
604
|
-
* @param saveUrl The endpoint that supports a `POST` method to save JSON.
|
|
605
|
-
* @param autoLoadIntervalSeconds How often to poll the `loadUrl` when
|
|
606
|
-
* automatically loading changes from the server.
|
|
607
|
-
* @returns A reference to the new Persister object.
|
|
608
|
-
* @example
|
|
609
|
-
* This example creates a Persister object and persists the Store to a remote
|
|
610
|
-
* server.
|
|
611
|
-
*
|
|
612
|
-
* ```js yolo
|
|
613
|
-
* const store = createStore().setTables({pets: {fido: {species: 'dog'}}});
|
|
614
|
-
* const persister = createRemotePersister(
|
|
615
|
-
* store,
|
|
616
|
-
* 'https://example.com/load',
|
|
617
|
-
* 'https://example.com/save',
|
|
618
|
-
* 5,
|
|
619
|
-
* );
|
|
620
|
-
*
|
|
621
|
-
* await persister.save();
|
|
622
|
-
* // Store JSON will be sent to server in a POST request.
|
|
623
|
-
*
|
|
624
|
-
* await persister.load();
|
|
625
|
-
* // Store JSON will be fetched from server with a GET request.
|
|
626
|
-
*
|
|
627
|
-
* persister.destroy();
|
|
628
|
-
* ```
|
|
629
|
-
* @category Creation
|
|
630
|
-
*/
|
|
631
|
-
export function createRemotePersister(
|
|
632
|
-
store: Store,
|
|
633
|
-
loadUrl: string,
|
|
634
|
-
saveUrl: string,
|
|
635
|
-
autoLoadIntervalSeconds: number,
|
|
636
|
-
): Persister;
|
|
637
|
-
|
|
638
|
-
/**
|
|
639
|
-
* The createFilePersister function creates a Persister object that can persist
|
|
640
|
-
* the Store to a local file (in an appropriate environment).
|
|
641
|
-
*
|
|
642
|
-
* As well as providing a reference to the Store to persist, you must provide
|
|
643
|
-
* `filePath` parameter which identifies the file to persist it to.
|
|
644
|
-
*
|
|
645
|
-
* @param store The Store to persist.
|
|
646
|
-
* @param filePath The location of the local file to persist the Store to.
|
|
647
|
-
* @returns A reference to the new Persister object.
|
|
648
|
-
* @example
|
|
649
|
-
* This example creates a Persister object and persists the Store to a local
|
|
650
|
-
* file.
|
|
651
|
-
*
|
|
652
|
-
* ```js yolo
|
|
653
|
-
* const store = createStore().setTables({pets: {fido: {species: 'dog'}}});
|
|
654
|
-
* const persister = createFilePersister(store, '/app/persisted.json');
|
|
655
|
-
*
|
|
656
|
-
* await persister.save();
|
|
657
|
-
* // Store JSON will be saved to the file.
|
|
658
|
-
*
|
|
659
|
-
* await persister.load();
|
|
660
|
-
* // Store JSON will be loaded from the file.
|
|
661
|
-
*
|
|
662
|
-
* persister.destroy();
|
|
663
|
-
* ```
|
|
664
|
-
* @category Creation
|
|
665
|
-
*/
|
|
666
|
-
export function createFilePersister(store: Store, filePath: string): Persister;
|
|
667
|
-
|
|
668
543
|
/**
|
|
669
544
|
* The createCustomPersister function creates a Persister object that you can
|
|
670
545
|
* configure to persist the Store in any way you wish.
|
|
@@ -678,18 +553,28 @@ export function createFilePersister(store: Store, filePath: string): Persister;
|
|
|
678
553
|
* covers. See those implementations for ideas on how to implement your own
|
|
679
554
|
* Persister types.
|
|
680
555
|
*
|
|
556
|
+
* This API changed in v4.0. Any custom persisters created on previous
|
|
557
|
+
* versions should be upgraded. Most notably, the `setPersisted` function
|
|
558
|
+
* parameter is provided with a `getContent` function to get the content from
|
|
559
|
+
* the Store itself, rather than being passed pre-serialized JSON. It also
|
|
560
|
+
* receives information about the changes made during a transaction. The
|
|
561
|
+
* `getPersisted` function must return the content (or nothing) rather than
|
|
562
|
+
* JSON. `addPersisterListener` has been renamed `addPersisterListener`, and
|
|
563
|
+
* `addPersisterListener` has been renamed `delPersisterListener`.
|
|
564
|
+
*
|
|
681
565
|
* @param store The Store to persist.
|
|
682
|
-
* @param getPersisted An asynchronous function which will fetch
|
|
683
|
-
* persistence layer (or `
|
|
684
|
-
* @param setPersisted An asynchronous function which will send
|
|
685
|
-
* persistence layer.
|
|
686
|
-
*
|
|
566
|
+
* @param getPersisted An asynchronous function which will fetch content from
|
|
567
|
+
* the persistence layer (or `undefined` if not present).
|
|
568
|
+
* @param setPersisted An asynchronous function which will send content to the
|
|
569
|
+
* persistence layer. Since v4.0, it receives functions for getting the Store
|
|
570
|
+
* content and information about the changes made during a transaction.
|
|
571
|
+
* @param addPersisterListener A function that will register a `listener`
|
|
687
572
|
* listener on underlying changes to the persistence layer. You can return a
|
|
688
|
-
* listening handle that will be provided again when `
|
|
689
|
-
*
|
|
690
|
-
* @param
|
|
691
|
-
*
|
|
692
|
-
*
|
|
573
|
+
* listening handle that will be provided again when `delPersisterListener` is
|
|
574
|
+
* called.
|
|
575
|
+
* @param delPersisterListener A function that will unregister the listener from
|
|
576
|
+
* the underlying changes to the persistence layer. It receives whatever was
|
|
577
|
+
* returned from your `addPersisterListener` implementation.
|
|
693
578
|
* @returns A reference to the new Persister object.
|
|
694
579
|
* @example
|
|
695
580
|
* This example creates a custom Persister object and persists the Store to a
|
|
@@ -702,9 +587,13 @@ export function createFilePersister(store: Store, filePath: string): Persister;
|
|
|
702
587
|
*
|
|
703
588
|
* const persister = createCustomPersister(
|
|
704
589
|
* store,
|
|
705
|
-
* async () =>
|
|
706
|
-
*
|
|
707
|
-
*
|
|
590
|
+
* async () => {
|
|
591
|
+
* try {
|
|
592
|
+
* return JSON.parse(storeJson);
|
|
593
|
+
* } catch {}
|
|
594
|
+
* },
|
|
595
|
+
* async (getContent) => (storeJson = JSON.stringify(getContent())),
|
|
596
|
+
* (listener) => setInterval(listener, 1000),
|
|
708
597
|
* (interval) => clearInterval(interval),
|
|
709
598
|
* );
|
|
710
599
|
*
|
|
@@ -724,8 +613,11 @@ export function createFilePersister(store: Store, filePath: string): Persister;
|
|
|
724
613
|
*/
|
|
725
614
|
export function createCustomPersister<ListeningHandle>(
|
|
726
615
|
store: Store,
|
|
727
|
-
getPersisted: () => Promise<
|
|
728
|
-
setPersisted: (
|
|
729
|
-
|
|
730
|
-
|
|
616
|
+
getPersisted: () => Promise<[Tables, Values] | undefined>,
|
|
617
|
+
setPersisted: (
|
|
618
|
+
getContent: () => [Tables, Values],
|
|
619
|
+
getTransactionChanges?: GetTransactionChanges,
|
|
620
|
+
) => Promise<void>,
|
|
621
|
+
addPersisterListener: (listener: PersisterListener) => ListeningHandle,
|
|
622
|
+
delPersisterListener: (listeningHandle: ListeningHandle) => void,
|
|
731
623
|
): Persister;
|