tinybase 3.2.0-beta.0 → 4.0.0-beta.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 (185) hide show
  1. package/lib/checkpoints.js +1 -1
  2. package/lib/checkpoints.js.gz +0 -0
  3. package/lib/cjs/checkpoints.cjs +1 -1
  4. package/lib/cjs/checkpoints.cjs.gz +0 -0
  5. package/lib/cjs/indexes.cjs +1 -1
  6. package/lib/cjs/indexes.cjs.gz +0 -0
  7. package/lib/cjs/metrics.cjs +1 -1
  8. package/lib/cjs/metrics.cjs.gz +0 -0
  9. package/lib/cjs/persister-browser.cjs +1 -0
  10. package/lib/cjs/persister-browser.cjs.gz +0 -0
  11. package/lib/cjs/persister-file.cjs +1 -0
  12. package/lib/cjs/persister-file.cjs.gz +0 -0
  13. package/lib/cjs/persister-remote.cjs +1 -0
  14. package/lib/cjs/persister-remote.cjs.gz +0 -0
  15. package/lib/cjs/persister-yjs.cjs +1 -0
  16. package/lib/cjs/persister-yjs.cjs.gz +0 -0
  17. package/lib/cjs/persisters.cjs +1 -1
  18. package/lib/cjs/persisters.cjs.gz +0 -0
  19. package/lib/cjs/relationships.cjs +1 -1
  20. package/lib/cjs/relationships.cjs.gz +0 -0
  21. package/lib/cjs/store.cjs +1 -1
  22. package/lib/cjs/store.cjs.gz +0 -0
  23. package/lib/cjs/tinybase.cjs +1 -1
  24. package/lib/cjs/tinybase.cjs.gz +0 -0
  25. package/lib/cjs/tools.cjs +1 -1
  26. package/lib/cjs/tools.cjs.gz +0 -0
  27. package/lib/cjs/ui-react.cjs +1 -1
  28. package/lib/cjs/ui-react.cjs.gz +0 -0
  29. package/lib/cjs-es6/checkpoints.cjs +1 -1
  30. package/lib/cjs-es6/checkpoints.cjs.gz +0 -0
  31. package/lib/cjs-es6/indexes.cjs +1 -1
  32. package/lib/cjs-es6/indexes.cjs.gz +0 -0
  33. package/lib/cjs-es6/metrics.cjs +1 -1
  34. package/lib/cjs-es6/metrics.cjs.gz +0 -0
  35. package/lib/cjs-es6/persister-browser.cjs +1 -0
  36. package/lib/cjs-es6/persister-browser.cjs.gz +0 -0
  37. package/lib/cjs-es6/persister-file.cjs +1 -0
  38. package/lib/cjs-es6/persister-file.cjs.gz +0 -0
  39. package/lib/cjs-es6/persister-remote.cjs +1 -0
  40. package/lib/cjs-es6/persister-remote.cjs.gz +0 -0
  41. package/lib/cjs-es6/persister-yjs.cjs +1 -0
  42. package/lib/cjs-es6/persister-yjs.cjs.gz +0 -0
  43. package/lib/cjs-es6/persisters.cjs +1 -1
  44. package/lib/cjs-es6/persisters.cjs.gz +0 -0
  45. package/lib/cjs-es6/relationships.cjs +1 -1
  46. package/lib/cjs-es6/relationships.cjs.gz +0 -0
  47. package/lib/cjs-es6/store.cjs +1 -1
  48. package/lib/cjs-es6/store.cjs.gz +0 -0
  49. package/lib/cjs-es6/tinybase.cjs +1 -1
  50. package/lib/cjs-es6/tinybase.cjs.gz +0 -0
  51. package/lib/cjs-es6/tools.cjs +1 -1
  52. package/lib/cjs-es6/tools.cjs.gz +0 -0
  53. package/lib/cjs-es6/ui-react.cjs +1 -1
  54. package/lib/cjs-es6/ui-react.cjs.gz +0 -0
  55. package/lib/debug/checkpoints.js +2 -2
  56. package/lib/debug/indexes.js +2 -2
  57. package/lib/debug/metrics.js +2 -2
  58. package/lib/debug/persister-browser.js +148 -0
  59. package/lib/debug/persister-file.js +140 -0
  60. package/lib/debug/persister-remote.js +156 -0
  61. package/lib/debug/persister-yjs.js +119 -0
  62. package/lib/debug/persisters.js +31 -130
  63. package/lib/debug/relationships.js +2 -2
  64. package/lib/debug/store.js +43 -28
  65. package/lib/debug/tinybase.js +73 -155
  66. package/lib/debug/tools.js +5 -4
  67. package/lib/debug/ui-react.js +3 -2
  68. package/lib/es6/checkpoints.js +1 -1
  69. package/lib/es6/checkpoints.js.gz +0 -0
  70. package/lib/es6/indexes.js +1 -1
  71. package/lib/es6/indexes.js.gz +0 -0
  72. package/lib/es6/metrics.js +1 -1
  73. package/lib/es6/metrics.js.gz +0 -0
  74. package/lib/es6/persister-browser.js +1 -0
  75. package/lib/es6/persister-browser.js.gz +0 -0
  76. package/lib/es6/persister-file.js +1 -0
  77. package/lib/es6/persister-file.js.gz +0 -0
  78. package/lib/es6/persister-remote.js +1 -0
  79. package/lib/es6/persister-remote.js.gz +0 -0
  80. package/lib/es6/persister-yjs.js +1 -0
  81. package/lib/es6/persister-yjs.js.gz +0 -0
  82. package/lib/es6/persisters.js +1 -1
  83. package/lib/es6/persisters.js.gz +0 -0
  84. package/lib/es6/relationships.js +1 -1
  85. package/lib/es6/relationships.js.gz +0 -0
  86. package/lib/es6/store.js +1 -1
  87. package/lib/es6/store.js.gz +0 -0
  88. package/lib/es6/tinybase.js +1 -1
  89. package/lib/es6/tinybase.js.gz +0 -0
  90. package/lib/es6/tools.js +1 -1
  91. package/lib/es6/tools.js.gz +0 -0
  92. package/lib/es6/ui-react.js +1 -1
  93. package/lib/es6/ui-react.js.gz +0 -0
  94. package/lib/indexes.js +1 -1
  95. package/lib/indexes.js.gz +0 -0
  96. package/lib/metrics.js +1 -1
  97. package/lib/metrics.js.gz +0 -0
  98. package/lib/persister-browser.js +1 -0
  99. package/lib/persister-browser.js.gz +0 -0
  100. package/lib/persister-file.js +1 -0
  101. package/lib/persister-file.js.gz +0 -0
  102. package/lib/persister-remote.js +1 -0
  103. package/lib/persister-remote.js.gz +0 -0
  104. package/lib/persister-yjs.js +1 -0
  105. package/lib/persister-yjs.js.gz +0 -0
  106. package/lib/persisters.js +1 -1
  107. package/lib/persisters.js.gz +0 -0
  108. package/lib/relationships.js +1 -1
  109. package/lib/relationships.js.gz +0 -0
  110. package/lib/store.js +1 -1
  111. package/lib/store.js.gz +0 -0
  112. package/lib/tinybase.js +1 -1
  113. package/lib/tinybase.js.gz +0 -0
  114. package/lib/tools.js +1 -1
  115. package/lib/tools.js.gz +0 -0
  116. package/lib/types/persister-browser.d.ts +85 -0
  117. package/lib/types/persister-file.d.ts +41 -0
  118. package/lib/types/persister-remote.d.ts +60 -0
  119. package/lib/types/persister-yjs.d.ts +44 -0
  120. package/lib/types/persisters.d.ts +45 -176
  121. package/lib/types/store.d.ts +127 -12
  122. package/lib/types/ui-react.d.ts +11 -0
  123. package/lib/types/with-schemas/persister-browser.d.ts +103 -0
  124. package/lib/types/with-schemas/persister-file.d.ts +50 -0
  125. package/lib/types/with-schemas/persister-remote.d.ts +71 -0
  126. package/lib/types/with-schemas/persister-yjs.d.ts +53 -0
  127. package/lib/types/with-schemas/persisters.d.ts +58 -218
  128. package/lib/types/with-schemas/store.d.ts +147 -12
  129. package/lib/types/with-schemas/ui-react.d.ts +12 -0
  130. package/lib/ui-react.js +1 -1
  131. package/lib/ui-react.js.gz +0 -0
  132. package/lib/umd/checkpoints.js +1 -1
  133. package/lib/umd/checkpoints.js.gz +0 -0
  134. package/lib/umd/indexes.js +1 -1
  135. package/lib/umd/indexes.js.gz +0 -0
  136. package/lib/umd/metrics.js +1 -1
  137. package/lib/umd/metrics.js.gz +0 -0
  138. package/lib/umd/persister-browser.js +1 -0
  139. package/lib/umd/persister-browser.js.gz +0 -0
  140. package/lib/umd/persister-file.js +1 -0
  141. package/lib/umd/persister-file.js.gz +0 -0
  142. package/lib/umd/persister-remote.js +1 -0
  143. package/lib/umd/persister-remote.js.gz +0 -0
  144. package/lib/umd/persister-yjs.js +1 -0
  145. package/lib/umd/persister-yjs.js.gz +0 -0
  146. package/lib/umd/persisters.js +1 -1
  147. package/lib/umd/persisters.js.gz +0 -0
  148. package/lib/umd/relationships.js +1 -1
  149. package/lib/umd/relationships.js.gz +0 -0
  150. package/lib/umd/store.js +1 -1
  151. package/lib/umd/store.js.gz +0 -0
  152. package/lib/umd/tinybase.js +1 -1
  153. package/lib/umd/tinybase.js.gz +0 -0
  154. package/lib/umd/tools.js +1 -1
  155. package/lib/umd/tools.js.gz +0 -0
  156. package/lib/umd/ui-react.js +1 -1
  157. package/lib/umd/ui-react.js.gz +0 -0
  158. package/lib/umd-es6/checkpoints.js +1 -1
  159. package/lib/umd-es6/checkpoints.js.gz +0 -0
  160. package/lib/umd-es6/indexes.js +1 -1
  161. package/lib/umd-es6/indexes.js.gz +0 -0
  162. package/lib/umd-es6/metrics.js +1 -1
  163. package/lib/umd-es6/metrics.js.gz +0 -0
  164. package/lib/umd-es6/persister-browser.js +1 -0
  165. package/lib/umd-es6/persister-browser.js.gz +0 -0
  166. package/lib/umd-es6/persister-file.js +1 -0
  167. package/lib/umd-es6/persister-file.js.gz +0 -0
  168. package/lib/umd-es6/persister-remote.js +1 -0
  169. package/lib/umd-es6/persister-remote.js.gz +0 -0
  170. package/lib/umd-es6/persister-yjs.js +1 -0
  171. package/lib/umd-es6/persister-yjs.js.gz +0 -0
  172. package/lib/umd-es6/persisters.js +1 -1
  173. package/lib/umd-es6/persisters.js.gz +0 -0
  174. package/lib/umd-es6/relationships.js +1 -1
  175. package/lib/umd-es6/relationships.js.gz +0 -0
  176. package/lib/umd-es6/store.js +1 -1
  177. package/lib/umd-es6/store.js.gz +0 -0
  178. package/lib/umd-es6/tinybase.js +1 -1
  179. package/lib/umd-es6/tinybase.js.gz +0 -0
  180. package/lib/umd-es6/tools.js +1 -1
  181. package/lib/umd-es6/tools.js.gz +0 -0
  182. package/lib/umd-es6/ui-react.js +1 -1
  183. package/lib/umd-es6/ui-react.js.gz +0 -0
  184. package/package.json +15 -10
  185. package/readme.md +2 -2
@@ -0,0 +1,71 @@
1
+ /**
2
+ * The persister-remote module of the TinyBase project lets you save and load
3
+ * Store data to and from a remote server.
4
+ *
5
+ * @see Persisting Data guide
6
+ * @packageDocumentation
7
+ * @module persister-remote
8
+ */
9
+
10
+ import {OptionalSchemas, Store} from './store';
11
+ import {Persister} from './persisters';
12
+
13
+ /**
14
+ * The createRemotePersister function creates a Persister object that can
15
+ * persist the Store to a remote server.
16
+ *
17
+ * This has schema-based typing. The following is a simplified representation:
18
+ *
19
+ * ```ts override
20
+ * createRemotePersister(
21
+ * store: Store,
22
+ * loadUrl: string,
23
+ * saveUrl: string,
24
+ * autoLoadIntervalSeconds: number,
25
+ * ): Persister;
26
+ * ```
27
+ *
28
+ * As well as providing a reference to the Store to persist, you must provide
29
+ * `loadUrl` and `saveUrl` parameters. These identify the endpoints of the
30
+ * server that support the `GET` method (to fetch the Store JSON to load) and
31
+ * the `POST` method (to send the Store JSON to save) respectively.
32
+ *
33
+ * For when you choose to enable automatic loading for the Persister (with the
34
+ * startAutoLoad method), it will poll the loadUrl for changes. The
35
+ * `autoLoadIntervalSeconds` method is used to indicate how often to do this.
36
+ *
37
+ * @param store The Store to persist.
38
+ * @param loadUrl The endpoint that supports a `GET` method to load JSON.
39
+ * @param saveUrl The endpoint that supports a `POST` method to save JSON.
40
+ * @param autoLoadIntervalSeconds How often to poll the `loadUrl` when
41
+ * automatically loading changes from the server.
42
+ * @returns A reference to the new Persister object.
43
+ * @example
44
+ * This example creates a Persister object and persists the Store to a remote
45
+ * server.
46
+ *
47
+ * ```js yolo
48
+ * const store = createStore().setTables({pets: {fido: {species: 'dog'}}});
49
+ * const persister = createRemotePersister(
50
+ * store,
51
+ * 'https://example.com/load',
52
+ * 'https://example.com/save',
53
+ * 5,
54
+ * );
55
+ *
56
+ * await persister.save();
57
+ * // Store JSON will be sent to server in a POST request.
58
+ *
59
+ * await persister.load();
60
+ * // Store JSON will be fetched from server with a GET request.
61
+ *
62
+ * persister.destroy();
63
+ * ```
64
+ * @category Creation
65
+ */
66
+ export function createRemotePersister<Schemas extends OptionalSchemas>(
67
+ store: Store<Schemas>,
68
+ loadUrl: string,
69
+ saveUrl: string,
70
+ autoLoadIntervalSeconds: number,
71
+ ): Persister<Schemas>;
@@ -0,0 +1,53 @@
1
+ /**
2
+ * The persister-yjs module of the TinyBase project provides a way to save and
3
+ * load Store data, to and from a Yjs document.
4
+ *
5
+ * A single entry point, the createYjsPersister function, is provided, which
6
+ * returns a new Persister object that can load and save a Store.:
7
+ *
8
+ * @packageDocumentation
9
+ * @module persister-yjs
10
+ */
11
+
12
+ import * as Y from 'yjs';
13
+ import {OptionalSchemas, Store} from './store.d';
14
+ import {Persister} from './persisters.d';
15
+
16
+ /**
17
+ * The createYjsPersister function creates a Persister object that can persist
18
+ * the Store to a Yjs document.
19
+ *
20
+ * This has schema-based typing. The following is a simplified representation:
21
+ *
22
+ * ```ts override
23
+ * createYjsPersister(store: Store, yDoc: Y.Doc): Persister;
24
+ * ```
25
+ *
26
+ * As well as providing a reference to the Store to persist, you must provide
27
+ * the Yjs document to persist it to.
28
+ *
29
+ * @param store The Store to persist.
30
+ * @param yDoc The Yjs document to persist the Store to.
31
+ * @returns A reference to the new Persister object.
32
+ * @example
33
+ * This example creates a Persister object and persists the Store to a Yjs
34
+ * document.
35
+ *
36
+ * ```js yolo
37
+ * const store = createStore().setTables({pets: {fido: {species: 'dog'}}});
38
+ * const persister = createYjsPersister(store, yDoc);
39
+ *
40
+ * await persister.save();
41
+ * // Store JSON will be saved to the document.
42
+ *
43
+ * await persister.load();
44
+ * // Store JSON will be loaded from the document.
45
+ *
46
+ * persister.destroy();
47
+ * ```
48
+ * @category Creation
49
+ */
50
+ export function createYjsPersister<Schemas extends OptionalSchemas>(
51
+ store: Store<Schemas>,
52
+ yDoc: Y.Doc,
53
+ ): Persister<Schemas>;
@@ -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, 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).
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 create a fully
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
@@ -28,7 +28,6 @@
28
28
  */
29
29
 
30
30
  import {OptionalSchemas, Store, Tables, Values} from './store.d';
31
- import {Callback} from './common.d';
32
31
 
33
32
  /**
34
33
  * The PersisterStats type describes the number of times a Persister object has
@@ -50,6 +49,14 @@ 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
+ export type PersisterListener<Schemas extends OptionalSchemas> = (
57
+ content?: [Tables<Schemas[0], true>, Values<Schemas[1], true>],
58
+ ) => void;
59
+
53
60
  /**
54
61
  * A Persister object lets you save and load Store data to and from different
55
62
  * locations, or underlying storage types.
@@ -514,12 +521,12 @@ export interface Persister<in out Schemas extends OptionalSchemas> {
514
521
  * const persister = createSessionPersister(store, 'pets');
515
522
  * await persister.startAutoSave();
516
523
  *
517
- * console.log(store.getListenerStats().tables);
524
+ * console.log(store.getListenerStats().transaction);
518
525
  * // -> 1
519
526
  *
520
527
  * persister.destroy();
521
528
  *
522
- * console.log(store.getListenerStats().tables);
529
+ * console.log(store.getListenerStats().transaction);
523
530
  * // -> 0
524
531
  * ```
525
532
  * @category Lifecycle
@@ -572,189 +579,6 @@ export interface Persister<in out Schemas extends OptionalSchemas> {
572
579
  getStats(): PersisterStats;
573
580
  }
574
581
 
575
- /**
576
- * The createSessionPersister function creates a Persister object that can
577
- * persist the Store to the browser's session storage.
578
- *
579
- * This has schema-based typing. The following is a simplified representation:
580
- *
581
- * ```ts override
582
- * createSessionPersister(
583
- * store: Store,
584
- * storageName: string,
585
- * ): Persister;
586
- * ```
587
- *
588
- * As well as providing a reference to the Store to persist, you must provide a
589
- * `storageName` parameter which is unique to your application. This is the key
590
- * that the browser uses to identify the storage location.
591
- *
592
- * @param store The Store to persist.
593
- * @param storageName The unique key to identify the storage location.
594
- * @returns A reference to the new Persister object.
595
- * @example
596
- * This example creates a Persister object and persists the Store to the
597
- * browser's session storage.
598
- *
599
- * ```js
600
- * const store = createStore().setTables({pets: {fido: {species: 'dog'}}});
601
- * const persister = createSessionPersister(store, 'pets');
602
- *
603
- * await persister.save();
604
- * console.log(sessionStorage.getItem('pets'));
605
- * // -> '[{"pets":{"fido":{"species":"dog"}}},{}]'
606
- *
607
- * persister.destroy();
608
- * sessionStorage.clear();
609
- * ```
610
- * @category Creation
611
- */
612
- export function createSessionPersister<Schemas extends OptionalSchemas>(
613
- store: Store<Schemas>,
614
- storageName: string,
615
- ): Persister<Schemas>;
616
-
617
- /**
618
- * The createLocalPersister function creates a Persister object that can
619
- * persist the Store to the browser's local storage.
620
- *
621
- * This has schema-based typing. The following is a simplified representation:
622
- *
623
- * ```ts override
624
- * createLocalPersister(
625
- * store: Store,
626
- * storageName: string,
627
- * ): Persister;
628
- * ```
629
- *
630
- * As well as providing a reference to the Store to persist, you must provide a
631
- * `storageName` parameter which is unique to your application. This is the key
632
- * that the browser uses to identify the storage location.
633
- *
634
- * @param store The Store to persist.
635
- * @param storageName The unique key to identify the storage location.
636
- * @returns A reference to the new Persister object.
637
- * @example
638
- * This example creates a Persister object and persists the Store to the
639
- * browser's local storage.
640
- *
641
- * ```js
642
- * const store = createStore().setTables({pets: {fido: {species: 'dog'}}});
643
- * const persister = createLocalPersister(store, 'pets');
644
- *
645
- * await persister.save();
646
- * console.log(localStorage.getItem('pets'));
647
- * // -> '[{"pets":{"fido":{"species":"dog"}}},{}]'
648
- *
649
- * persister.destroy();
650
- * localStorage.clear();
651
- * ```
652
- * @category Creation
653
- */
654
- export function createLocalPersister<Schemas extends OptionalSchemas>(
655
- store: Store<Schemas>,
656
- storageName: string,
657
- ): Persister<Schemas>;
658
-
659
- /**
660
- * The createRemotePersister function creates a Persister object that can
661
- * persist the Store to a remote server.
662
- *
663
- * This has schema-based typing. The following is a simplified representation:
664
- *
665
- * ```ts override
666
- * createRemotePersister(
667
- * store: Store,
668
- * loadUrl: string,
669
- * saveUrl: string,
670
- * autoLoadIntervalSeconds: number,
671
- * ): Persister;
672
- * ```
673
- *
674
- * As well as providing a reference to the Store to persist, you must provide
675
- * `loadUrl` and `saveUrl` parameters. These identify the endpoints of the
676
- * server that support the `GET` method (to fetch the Store JSON to load) and
677
- * the `POST` method (to send the Store JSON to save) respectively.
678
- *
679
- * For when you choose to enable automatic loading for the Persister (with the
680
- * startAutoLoad method), it will poll the loadUrl for changes. The
681
- * `autoLoadIntervalSeconds` method is used to indicate how often to do this.
682
- *
683
- * @param store The Store to persist.
684
- * @param loadUrl The endpoint that supports a `GET` method to load JSON.
685
- * @param saveUrl The endpoint that supports a `POST` method to save JSON.
686
- * @param autoLoadIntervalSeconds How often to poll the `loadUrl` when
687
- * automatically loading changes from the server.
688
- * @returns A reference to the new Persister object.
689
- * @example
690
- * This example creates a Persister object and persists the Store to a remote
691
- * server.
692
- *
693
- * ```js yolo
694
- * const store = createStore().setTables({pets: {fido: {species: 'dog'}}});
695
- * const persister = createRemotePersister(
696
- * store,
697
- * 'https://example.com/load',
698
- * 'https://example.com/save',
699
- * 5,
700
- * );
701
- *
702
- * await persister.save();
703
- * // Store JSON will be sent to server in a POST request.
704
- *
705
- * await persister.load();
706
- * // Store JSON will be fetched from server with a GET request.
707
- *
708
- * persister.destroy();
709
- * ```
710
- * @category Creation
711
- */
712
- export function createRemotePersister<Schemas extends OptionalSchemas>(
713
- store: Store<Schemas>,
714
- loadUrl: string,
715
- saveUrl: string,
716
- autoLoadIntervalSeconds: number,
717
- ): Persister<Schemas>;
718
-
719
- /**
720
- * The createFilePersister function creates a Persister object that can persist
721
- * the Store to a local file (in an appropriate environment).
722
- *
723
- * This has schema-based typing. The following is a simplified representation:
724
- *
725
- * ```ts override
726
- * createFilePersister(store: Store, filePath: string): Persister;
727
- * ```
728
- *
729
- * As well as providing a reference to the Store to persist, you must provide
730
- * `filePath` parameter which identifies the file to persist it to.
731
- *
732
- * @param store The Store to persist.
733
- * @param filePath The location of the local file to persist the Store to.
734
- * @returns A reference to the new Persister object.
735
- * @example
736
- * This example creates a Persister object and persists the Store to a local
737
- * file.
738
- *
739
- * ```js yolo
740
- * const store = createStore().setTables({pets: {fido: {species: 'dog'}}});
741
- * const persister = createFilePersister(store, '/app/persisted.json');
742
- *
743
- * await persister.save();
744
- * // Store JSON will be saved to the file.
745
- *
746
- * await persister.load();
747
- * // Store JSON will be loaded from the file.
748
- *
749
- * persister.destroy();
750
- * ```
751
- * @category Creation
752
- */
753
- export function createFilePersister<Schemas extends OptionalSchemas>(
754
- store: Store<Schemas>,
755
- filePath: string,
756
- ): Persister<Schemas>;
757
-
758
582
  /**
759
583
  * The createCustomPersister function creates a Persister object that you can
760
584
  * configure to persist the Store in any way you wish.
@@ -762,12 +586,12 @@ export function createFilePersister<Schemas extends OptionalSchemas>(
762
586
  * This has schema-based typing. The following is a simplified representation:
763
587
  *
764
588
  * ```ts override
765
- * createCustomPersister(
589
+ * createCustomPersister<ListeningHandle>(
766
590
  * store: Store,
767
591
  * getPersisted: () => Promise<string | null | undefined>,
768
- * setPersisted: (json: string) => Promise<void>,
769
- * startListeningToPersisted: (didChange: Callback) => void,
770
- * stopListeningToPersisted: Callback,
592
+ * setPersisted: (getContent: () => [Tables, Values]) => Promise<void>,
593
+ * addPersisterListener: (listener: PersisterListener) => ListeningHandle,
594
+ * delPersisterListener: (listeningHandle: ListeningHandle) => void,
771
595
  * ): Persister;
772
596
  * ```
773
597
  *
@@ -780,15 +604,25 @@ export function createFilePersister<Schemas extends OptionalSchemas>(
780
604
  * covers. See those implementations for ideas on how to implement your own
781
605
  * Persister types.
782
606
  *
607
+ * This API changed in v4.0.0. Any custom persisters created on previous
608
+ * versions should be upgraded. Most notably, the `setPersisted` function
609
+ * parameter is provided with a `getContent` function to get the content from
610
+ * the Store itself, rather than being passed pre-serialized JSON.
611
+ * `addPersisterListener` has been renamed `addPersisterListener`, and
612
+ * `addPersisterListener` has been renamed `delPersisterListener`.
613
+ *
783
614
  * @param store The Store to persist.
784
- * @param getPersisted An asynchronous function which will fetch JSON from the
785
- * persistence layer (or `null` or `undefined` if not present).
786
- * @param setPersisted An asynchronous function which will send JSON to the
615
+ * @param getPersisted An asynchronous function which will fetch content from
616
+ * the persistence layer (or `null` or `undefined` if not present).
617
+ * @param setPersisted An asynchronous function which will send content to the
787
618
  * persistence layer.
788
- * @param startListeningToPersisted A function that will register a `didChange`
789
- * listener on underlying changes to the persistence layer.
790
- * @param stopListeningToPersisted A function that will unregister the listener
791
- * from the underlying changes to the persistence layer.
619
+ * @param addPersisterListener A function that will register a `listener`
620
+ * listener on underlying changes to the persistence layer. You can return a
621
+ * listening handle that will be provided again when `delPersisterListener`
622
+ * is called.
623
+ * @param delPersisterListener A function that will unregister the listener
624
+ * from the underlying changes to the persistence layer. It receives whatever
625
+ * was returned from your `addPersisterListener` implementation.
792
626
  * @returns A reference to the new Persister object.
793
627
  * @example
794
628
  * This example creates a custom Persister object and persists the Store to a
@@ -798,14 +632,13 @@ export function createFilePersister<Schemas extends OptionalSchemas>(
798
632
  * ```js
799
633
  * const store = createStore().setTables({pets: {fido: {species: 'dog'}}});
800
634
  * let storeJson;
801
- * let interval;
802
635
  *
803
636
  * const persister = createCustomPersister(
804
637
  * store,
805
638
  * async () => storeJson,
806
- * async (json) => (storeJson = json),
807
- * (didChange) => (interval = setInterval(didChange, 1000)),
808
- * () => clearInterval(interval),
639
+ * async (getContent) => (storeJson = JSON.stringify(getContent())),
640
+ * (listener) => setInterval(listener, 1000),
641
+ * (interval) => clearInterval(interval),
809
642
  * );
810
643
  *
811
644
  * await persister.save();
@@ -822,10 +655,17 @@ export function createFilePersister<Schemas extends OptionalSchemas>(
822
655
  * ```
823
656
  * @category Creation
824
657
  */
825
- export function createCustomPersister<Schemas extends OptionalSchemas>(
658
+ export function createCustomPersister<
659
+ Schemas extends OptionalSchemas,
660
+ ListeningHandle,
661
+ >(
826
662
  store: Store<Schemas>,
827
663
  getPersisted: () => Promise<string | null | undefined>,
828
- setPersisted: (json: string) => Promise<void>,
829
- startListeningToPersisted: (didChange: Callback) => void,
830
- stopListeningToPersisted: Callback,
664
+ setPersisted: (
665
+ getContent: () => [Tables<Schemas[0]>, Values<Schemas[1]>],
666
+ ) => Promise<void>,
667
+ addPersisterListener: (
668
+ listener: PersisterListener<Schemas>,
669
+ ) => ListeningHandle,
670
+ delPersisterListener: (listeningHandle: ListeningHandle) => void,
831
671
  ): Persister<Schemas>;