tinybase 4.3.0-beta.0 → 4.3.0

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 (172) hide show
  1. package/lib/cjs/persisters/persister-cr-sqlite-wasm.cjs +1 -1
  2. package/lib/cjs/persisters/persister-cr-sqlite-wasm.cjs.gz +0 -0
  3. package/lib/cjs/persisters/persister-expo-sqlite.cjs +1 -1
  4. package/lib/cjs/persisters/persister-expo-sqlite.cjs.gz +0 -0
  5. package/lib/cjs/persisters/persister-partykit-client.cjs +1 -0
  6. package/lib/cjs/persisters/persister-partykit-client.cjs.gz +0 -0
  7. package/lib/cjs/persisters/persister-partykit-server.cjs +1 -0
  8. package/lib/cjs/persisters/persister-partykit-server.cjs.gz +0 -0
  9. package/lib/cjs/persisters/persister-sqlite-wasm.cjs +1 -1
  10. package/lib/cjs/persisters/persister-sqlite-wasm.cjs.gz +0 -0
  11. package/lib/cjs/persisters/persister-sqlite3.cjs +1 -1
  12. package/lib/cjs/persisters/persister-sqlite3.cjs.gz +0 -0
  13. package/lib/cjs/store.cjs +1 -1
  14. package/lib/cjs/store.cjs.gz +0 -0
  15. package/lib/cjs/tinybase.cjs +1 -1
  16. package/lib/cjs/tinybase.cjs.gz +0 -0
  17. package/lib/cjs/tools.cjs +1 -1
  18. package/lib/cjs/tools.cjs.gz +0 -0
  19. package/lib/cjs/ui-react-dom-debug.cjs +1 -1
  20. package/lib/cjs/ui-react-dom-debug.cjs.gz +0 -0
  21. package/lib/cjs/ui-react-dom.cjs +1 -1
  22. package/lib/cjs/ui-react-dom.cjs.gz +0 -0
  23. package/lib/cjs/ui-react.cjs +1 -1
  24. package/lib/cjs/ui-react.cjs.gz +0 -0
  25. package/lib/cjs-es6/persisters/persister-cr-sqlite-wasm.cjs +1 -1
  26. package/lib/cjs-es6/persisters/persister-cr-sqlite-wasm.cjs.gz +0 -0
  27. package/lib/cjs-es6/persisters/persister-expo-sqlite.cjs +1 -1
  28. package/lib/cjs-es6/persisters/persister-expo-sqlite.cjs.gz +0 -0
  29. package/lib/cjs-es6/persisters/persister-partykit-client.cjs +1 -0
  30. package/lib/cjs-es6/persisters/persister-partykit-client.cjs.gz +0 -0
  31. package/lib/cjs-es6/persisters/persister-partykit-server.cjs +1 -0
  32. package/lib/cjs-es6/persisters/persister-partykit-server.cjs.gz +0 -0
  33. package/lib/cjs-es6/persisters/persister-sqlite-wasm.cjs +1 -1
  34. package/lib/cjs-es6/persisters/persister-sqlite-wasm.cjs.gz +0 -0
  35. package/lib/cjs-es6/persisters/persister-sqlite3.cjs +1 -1
  36. package/lib/cjs-es6/persisters/persister-sqlite3.cjs.gz +0 -0
  37. package/lib/cjs-es6/store.cjs +1 -1
  38. package/lib/cjs-es6/store.cjs.gz +0 -0
  39. package/lib/cjs-es6/tinybase.cjs +1 -1
  40. package/lib/cjs-es6/tinybase.cjs.gz +0 -0
  41. package/lib/cjs-es6/tools.cjs +1 -1
  42. package/lib/cjs-es6/tools.cjs.gz +0 -0
  43. package/lib/cjs-es6/ui-react-dom-debug.cjs +1 -1
  44. package/lib/cjs-es6/ui-react-dom-debug.cjs.gz +0 -0
  45. package/lib/cjs-es6/ui-react-dom.cjs +1 -1
  46. package/lib/cjs-es6/ui-react-dom.cjs.gz +0 -0
  47. package/lib/cjs-es6/ui-react.cjs +1 -1
  48. package/lib/cjs-es6/ui-react.cjs.gz +0 -0
  49. package/lib/debug/persisters/persister-cr-sqlite-wasm.js +10 -7
  50. package/lib/debug/persisters/persister-expo-sqlite.js +10 -7
  51. package/lib/debug/persisters/persister-partykit-client.js +249 -0
  52. package/lib/debug/persisters/persister-partykit-server.js +161 -0
  53. package/lib/debug/persisters/persister-sqlite-wasm.js +10 -7
  54. package/lib/debug/persisters/persister-sqlite3.js +10 -7
  55. package/lib/debug/queries.js +2 -2
  56. package/lib/debug/store.js +13 -13
  57. package/lib/debug/tinybase.js +14 -14
  58. package/lib/debug/tools.js +3 -4
  59. package/lib/debug/ui-react-dom.js +35 -24
  60. package/lib/debug/ui-react.js +38 -1
  61. package/lib/es6/persisters/persister-cr-sqlite-wasm.js +1 -1
  62. package/lib/es6/persisters/persister-cr-sqlite-wasm.js.gz +0 -0
  63. package/lib/es6/persisters/persister-expo-sqlite.js +1 -1
  64. package/lib/es6/persisters/persister-expo-sqlite.js.gz +0 -0
  65. package/lib/es6/persisters/persister-partykit-client.js +1 -0
  66. package/lib/es6/persisters/persister-partykit-client.js.gz +0 -0
  67. package/lib/es6/persisters/persister-partykit-server.js +1 -0
  68. package/lib/es6/persisters/persister-partykit-server.js.gz +0 -0
  69. package/lib/es6/persisters/persister-sqlite-wasm.js +1 -1
  70. package/lib/es6/persisters/persister-sqlite-wasm.js.gz +0 -0
  71. package/lib/es6/persisters/persister-sqlite3.js +1 -1
  72. package/lib/es6/persisters/persister-sqlite3.js.gz +0 -0
  73. package/lib/es6/store.js +1 -1
  74. package/lib/es6/store.js.gz +0 -0
  75. package/lib/es6/tinybase.js +1 -1
  76. package/lib/es6/tinybase.js.gz +0 -0
  77. package/lib/es6/tools.js +1 -1
  78. package/lib/es6/tools.js.gz +0 -0
  79. package/lib/es6/ui-react-dom-debug.js +1 -1
  80. package/lib/es6/ui-react-dom-debug.js.gz +0 -0
  81. package/lib/es6/ui-react-dom.js +1 -1
  82. package/lib/es6/ui-react-dom.js.gz +0 -0
  83. package/lib/es6/ui-react.js +1 -1
  84. package/lib/es6/ui-react.js.gz +0 -0
  85. package/lib/persisters/persister-cr-sqlite-wasm.js +1 -1
  86. package/lib/persisters/persister-cr-sqlite-wasm.js.gz +0 -0
  87. package/lib/persisters/persister-expo-sqlite.js +1 -1
  88. package/lib/persisters/persister-expo-sqlite.js.gz +0 -0
  89. package/lib/persisters/persister-partykit-client.js +1 -0
  90. package/lib/persisters/persister-partykit-client.js.gz +0 -0
  91. package/lib/persisters/persister-partykit-server.js +1 -0
  92. package/lib/persisters/persister-partykit-server.js.gz +0 -0
  93. package/lib/persisters/persister-sqlite-wasm.js +1 -1
  94. package/lib/persisters/persister-sqlite-wasm.js.gz +0 -0
  95. package/lib/persisters/persister-sqlite3.js +1 -1
  96. package/lib/persisters/persister-sqlite3.js.gz +0 -0
  97. package/lib/store.js +1 -1
  98. package/lib/store.js.gz +0 -0
  99. package/lib/tinybase.js +1 -1
  100. package/lib/tinybase.js.gz +0 -0
  101. package/lib/tools.js +1 -1
  102. package/lib/tools.js.gz +0 -0
  103. package/lib/types/persisters/persister-cr-sqlite-wasm.d.ts +2 -0
  104. package/lib/types/persisters/persister-expo-sqlite.d.ts +2 -0
  105. package/lib/types/persisters/persister-partykit-client.d.ts +88 -0
  106. package/lib/types/persisters/persister-partykit-server.d.ts +114 -0
  107. package/lib/types/persisters/persister-sqlite-wasm.d.ts +2 -0
  108. package/lib/types/persisters/persister-sqlite3.d.ts +2 -0
  109. package/lib/types/store.d.ts +6 -0
  110. package/lib/types/ui-react.d.ts +187 -4
  111. package/lib/types/with-schemas/persisters/persister-cr-sqlite-wasm.d.ts +2 -0
  112. package/lib/types/with-schemas/persisters/persister-expo-sqlite.d.ts +2 -0
  113. package/lib/types/with-schemas/persisters/persister-partykit-client.d.ts +99 -0
  114. package/lib/types/with-schemas/persisters/persister-partykit-server.d.ts +114 -0
  115. package/lib/types/with-schemas/persisters/persister-sqlite-wasm.d.ts +2 -0
  116. package/lib/types/with-schemas/persisters/persister-sqlite3.d.ts +2 -0
  117. package/lib/types/with-schemas/store.d.ts +6 -0
  118. package/lib/types/with-schemas/ui-react.d.ts +223 -8
  119. package/lib/ui-react-dom.js +1 -1
  120. package/lib/ui-react-dom.js.gz +0 -0
  121. package/lib/ui-react.js +1 -1
  122. package/lib/ui-react.js.gz +0 -0
  123. package/lib/umd/persisters/persister-cr-sqlite-wasm.js +1 -1
  124. package/lib/umd/persisters/persister-cr-sqlite-wasm.js.gz +0 -0
  125. package/lib/umd/persisters/persister-expo-sqlite.js +1 -1
  126. package/lib/umd/persisters/persister-expo-sqlite.js.gz +0 -0
  127. package/lib/umd/persisters/persister-partykit-client.js +1 -0
  128. package/lib/umd/persisters/persister-partykit-client.js.gz +0 -0
  129. package/lib/umd/persisters/persister-partykit-server.js +1 -0
  130. package/lib/umd/persisters/persister-partykit-server.js.gz +0 -0
  131. package/lib/umd/persisters/persister-sqlite-wasm.js +1 -1
  132. package/lib/umd/persisters/persister-sqlite-wasm.js.gz +0 -0
  133. package/lib/umd/persisters/persister-sqlite3.js +1 -1
  134. package/lib/umd/persisters/persister-sqlite3.js.gz +0 -0
  135. package/lib/umd/store.js +1 -1
  136. package/lib/umd/store.js.gz +0 -0
  137. package/lib/umd/tinybase.js +1 -1
  138. package/lib/umd/tinybase.js.gz +0 -0
  139. package/lib/umd/tools.js +1 -1
  140. package/lib/umd/tools.js.gz +0 -0
  141. package/lib/umd/ui-react-dom-debug.js +1 -1
  142. package/lib/umd/ui-react-dom-debug.js.gz +0 -0
  143. package/lib/umd/ui-react-dom.js +1 -1
  144. package/lib/umd/ui-react-dom.js.gz +0 -0
  145. package/lib/umd/ui-react.js +1 -1
  146. package/lib/umd/ui-react.js.gz +0 -0
  147. package/lib/umd-es6/persisters/persister-cr-sqlite-wasm.js +1 -1
  148. package/lib/umd-es6/persisters/persister-cr-sqlite-wasm.js.gz +0 -0
  149. package/lib/umd-es6/persisters/persister-expo-sqlite.js +1 -1
  150. package/lib/umd-es6/persisters/persister-expo-sqlite.js.gz +0 -0
  151. package/lib/umd-es6/persisters/persister-partykit-client.js +1 -0
  152. package/lib/umd-es6/persisters/persister-partykit-client.js.gz +0 -0
  153. package/lib/umd-es6/persisters/persister-partykit-server.js +1 -0
  154. package/lib/umd-es6/persisters/persister-partykit-server.js.gz +0 -0
  155. package/lib/umd-es6/persisters/persister-sqlite-wasm.js +1 -1
  156. package/lib/umd-es6/persisters/persister-sqlite-wasm.js.gz +0 -0
  157. package/lib/umd-es6/persisters/persister-sqlite3.js +1 -1
  158. package/lib/umd-es6/persisters/persister-sqlite3.js.gz +0 -0
  159. package/lib/umd-es6/store.js +1 -1
  160. package/lib/umd-es6/store.js.gz +0 -0
  161. package/lib/umd-es6/tinybase.js +1 -1
  162. package/lib/umd-es6/tinybase.js.gz +0 -0
  163. package/lib/umd-es6/tools.js +1 -1
  164. package/lib/umd-es6/tools.js.gz +0 -0
  165. package/lib/umd-es6/ui-react-dom-debug.js +1 -1
  166. package/lib/umd-es6/ui-react-dom-debug.js.gz +0 -0
  167. package/lib/umd-es6/ui-react-dom.js +1 -1
  168. package/lib/umd-es6/ui-react-dom.js.gz +0 -0
  169. package/lib/umd-es6/ui-react.js +1 -1
  170. package/lib/umd-es6/ui-react.js.gz +0 -0
  171. package/package.json +42 -31
  172. package/readme.md +13 -13
@@ -42,6 +42,7 @@ import {
42
42
  TableListener,
43
43
  Tables,
44
44
  TablesListener,
45
+ TransactionListener,
45
46
  Value,
46
47
  ValueIdsListener,
47
48
  ValueListener,
@@ -3621,6 +3622,180 @@ export function useValueListener(
3621
3622
  storeOrStoreId?: StoreOrStoreId,
3622
3623
  ): void;
3623
3624
 
3625
+ /**
3626
+ * The useStartTransactionListener hook registers a listener function with the
3627
+ * Store that will be called at the start of a transaction.
3628
+ *
3629
+ * Unlike the addStartTransactionListener method, which returns a listener Id
3630
+ * and requires you to remove it manually, the useStartTransactionListener hook
3631
+ * manages this lifecycle for you: when the listener changes (per its
3632
+ * `listenerDeps` dependencies) or the component unmounts, the listener on the
3633
+ * underlying Store will be deleted.
3634
+ * @param listener The function that will be called at the start of a
3635
+ * transaction.
3636
+ * @param listenerDeps An optional array of dependencies for the `listener`
3637
+ * function, which, if any change, result in the re-registration of the
3638
+ * listener. This parameter defaults to an empty array.
3639
+ * @param storeOrStoreId The Store to register the listener with: omit for the
3640
+ * default context Store, provide an Id for a named context Store, or provide an
3641
+ * explicit reference.
3642
+ * @example
3643
+ * This example uses the useStartTransactionListener hook to create a listener
3644
+ * that is scoped to a single component. When the component is unmounted, the
3645
+ * listener is removed from the Store.
3646
+ *
3647
+ * ```jsx
3648
+ * const App = ({store}) => (
3649
+ * <Provider store={store}>
3650
+ * <Pane />
3651
+ * </Provider>
3652
+ * );
3653
+ * const Pane = () => {
3654
+ * useStartTransactionListener(() => console.log('Start transaction'));
3655
+ * return <span>App</span>;
3656
+ * };
3657
+ *
3658
+ * const store = createStore();
3659
+ * const app = document.createElement('div');
3660
+ * const root = ReactDOMClient.createRoot(app);
3661
+ * root.render(<App store={store} />); // !act
3662
+ * console.log(store.getListenerStats().transaction);
3663
+ * // -> 1
3664
+ *
3665
+ * store.setValue('open', false); // !act
3666
+ * // -> 'Start transaction'
3667
+ *
3668
+ * root.unmount(); // !act
3669
+ * console.log(store.getListenerStats().transaction);
3670
+ * // -> 0
3671
+ * ```
3672
+ * @category Store hooks
3673
+ * @since v4.2.2
3674
+ */
3675
+ export function useStartTransactionListener(
3676
+ listener: TransactionListener,
3677
+ listenerDeps?: React.DependencyList,
3678
+ storeOrStoreId?: StoreOrStoreId,
3679
+ ): void;
3680
+
3681
+ /**
3682
+ * The useWillFinishTransactionListener hook registers a listener function with
3683
+ * a Store that will be called just before other non-mutating listeners are
3684
+ * called at the end of the transaction.
3685
+ *
3686
+ * Unlike the addWillFinisTransactionListener method, which returns a listener
3687
+ * Id and requires you to remove it manually, the
3688
+ * useWillFinishTransactionListener hook manages this lifecycle for you: when
3689
+ * the listener changes (per its `listenerDeps` dependencies) or the component
3690
+ * unmounts, the listener on the underlying Store will be deleted.
3691
+ * @param listener The function that will be called before the end of a
3692
+ * transaction.
3693
+ * @param listenerDeps An optional array of dependencies for the `listener`
3694
+ * function, which, if any change, result in the re-registration of the
3695
+ * listener. This parameter defaults to an empty array.
3696
+ * @param storeOrStoreId The Store to register the listener with: omit for the
3697
+ * default context Store, provide an Id for a named context Store, or provide an
3698
+ * explicit reference.
3699
+ * @example
3700
+ * This example uses the useWillFinishTransactionListener hook to create a
3701
+ * listener that is scoped to a single component. When the component is
3702
+ * unmounted, the listener is removed from the Store.
3703
+ *
3704
+ * ```jsx
3705
+ * const App = ({store}) => (
3706
+ * <Provider store={store}>
3707
+ * <Pane />
3708
+ * </Provider>
3709
+ * );
3710
+ * const Pane = () => {
3711
+ * useWillFinishTransactionListener(
3712
+ * () => console.log('Will finish transaction'),
3713
+ * );
3714
+ * return <span>App</span>;
3715
+ * };
3716
+ *
3717
+ * const store = createStore();
3718
+ * const app = document.createElement('div');
3719
+ * const root = ReactDOMClient.createRoot(app);
3720
+ * root.render(<App store={store} />); // !act
3721
+ * console.log(store.getListenerStats().transaction);
3722
+ * // -> 1
3723
+ *
3724
+ * store.setValue('open', false); // !act
3725
+ * // -> 'Will finish transaction'
3726
+ *
3727
+ * root.unmount(); // !act
3728
+ * console.log(store.getListenerStats().transaction);
3729
+ * // -> 0
3730
+ * ```
3731
+ * @category Store hooks
3732
+ * @since v4.2.2
3733
+ */
3734
+ export function useWillFinishTransactionListener(
3735
+ listener: TransactionListener,
3736
+ listenerDeps?: React.DependencyList,
3737
+ storeOrStoreId?: StoreOrStoreId,
3738
+ ): void;
3739
+
3740
+ /**
3741
+ * The useDidFinishTransactionListener hook registers a listener function with a
3742
+ * Store that will be called just after other non-mutating listeners are called
3743
+ * at the end of the transaction.
3744
+ *
3745
+ * Unlike the addDidFinishTransactionListener method, which returns a listener
3746
+ * Id and requires you to remove it manually, the
3747
+ * useDidFinishTransactionListener hook manages this lifecycle for you: when the
3748
+ * listener changes (per its `listenerDeps` dependencies) or the component
3749
+ * unmounts, the listener on the underlying Store will be deleted.
3750
+ * @param listener The function that will be called after the end of a
3751
+ * transaction.
3752
+ * @param listenerDeps An optional array of dependencies for the `listener`
3753
+ * function, which, if any change, result in the re-registration of the
3754
+ * listener. This parameter defaults to an empty array.
3755
+ * @param storeOrStoreId The Store to register the listener with: omit for the
3756
+ * default context Store, provide an Id for a named context Store, or provide an
3757
+ * explicit reference.
3758
+ * @example
3759
+ * This example uses the useDidFinishTransactionListener hook to create a
3760
+ * listener that is scoped to a single component. When the component is
3761
+ * unmounted, the listener is removed from the Store.
3762
+ *
3763
+ * ```jsx
3764
+ * const App = ({store}) => (
3765
+ * <Provider store={store}>
3766
+ * <Pane />
3767
+ * </Provider>
3768
+ * );
3769
+ * const Pane = () => {
3770
+ * useDidFinishTransactionListener(
3771
+ * () => console.log('Did finish transaction'),
3772
+ * );
3773
+ * return <span>App</span>;
3774
+ * };
3775
+ *
3776
+ * const store = createStore();
3777
+ * const app = document.createElement('div');
3778
+ * const root = ReactDOMClient.createRoot(app);
3779
+ * root.render(<App store={store} />); // !act
3780
+ * console.log(store.getListenerStats().transaction);
3781
+ * // -> 1
3782
+ *
3783
+ * store.setValue('open', false); // !act
3784
+ * // -> 'Did finish transaction'
3785
+ *
3786
+ * root.unmount(); // !act
3787
+ * console.log(store.getListenerStats().transaction);
3788
+ * // -> 0
3789
+ * ```
3790
+ * @category Store hooks
3791
+ * @since v4.2.2
3792
+ */
3793
+ export function useDidFinishTransactionListener(
3794
+ listener: TransactionListener,
3795
+ listenerDeps?: React.DependencyList,
3796
+ storeOrStoreId?: StoreOrStoreId,
3797
+ ): void;
3798
+
3624
3799
  /**
3625
3800
  * The useCreateMetrics hook is used to create a Metrics object within a React
3626
3801
  * application with convenient memoization.
@@ -8715,6 +8890,12 @@ export function useCheckpointListener(
8715
8890
  * an array in the fifth parameter. The Persister itself is used as a dependency
8716
8891
  * by default.
8717
8892
  *
8893
+ * Since v4.3.0, the `create` function can return undefined, meaning that you
8894
+ * can enable or disable persistence conditionally within this hook. This is
8895
+ * useful for applications which might turn on or off their cloud persistence or
8896
+ * collaboration features. As a result, the `then` callback may also get passed
8897
+ * undefined, which you should handle accordingly.
8898
+ *
8718
8899
  * This hook ensures the Persister object is destroyed whenever a new one is
8719
8900
  * created or the component is unmounted.
8720
8901
  * @param store A reference to the Store for which to create a new Persister
@@ -8825,13 +9006,15 @@ export function useCheckpointListener(
8825
9006
  * ```
8826
9007
  * @category Persister hooks
8827
9008
  */
8828
- export function useCreatePersister(
9009
+ export function useCreatePersister<
9010
+ PersisterOrUndefined extends Persister | undefined,
9011
+ >(
8829
9012
  store: Store,
8830
- create: (store: Store) => Persister,
9013
+ create: (store: Store) => PersisterOrUndefined,
8831
9014
  createDeps?: React.DependencyList,
8832
- then?: (persister: Persister) => Promise<void>,
9015
+ then?: (persister: PersisterOrUndefined) => Promise<void>,
8833
9016
  thenDeps?: React.DependencyList,
8834
- ): Persister;
9017
+ ): PersisterOrUndefined;
8835
9018
 
8836
9019
  /**
8837
9020
  * The ExtraProps type represents a set of arbitrary additional props.
@@ -5,6 +5,7 @@
5
5
  * @see Persisting Data guide
6
6
  * @packageDocumentation
7
7
  * @module persister-cr-sqlite-wasm
8
+ * @since v4.0.0
8
9
  */
9
10
 
10
11
  import {DatabasePersisterConfig, Persister} from '../persisters';
@@ -107,6 +108,7 @@ import {DB} from '@vlcn.io/crsqlite-wasm';
107
108
  * persister.destroy();
108
109
  * ```
109
110
  * @category Creation
111
+ * @since v4.0.0
110
112
  */
111
113
  export function createCrSqliteWasmPersister<Schemas extends OptionalSchemas>(
112
114
  store: Store<Schemas>,
@@ -8,6 +8,7 @@
8
8
  * @see Persisting Data guide
9
9
  * @packageDocumentation
10
10
  * @module persister-expo-sqlite
11
+ * @since v4.0.3
11
12
  */
12
13
 
13
14
  import {DatabasePersisterConfig, Persister} from '../persisters';
@@ -128,6 +129,7 @@ import {SQLiteDatabase} from 'expo-sqlite';
128
129
  * persister.destroy();
129
130
  * ```
130
131
  * @category Creation
132
+ * @since v4.0.3
131
133
  */
132
134
  export function createExpoSqlitePersister<Schemas extends OptionalSchemas>(
133
135
  store: Store<Schemas>,
@@ -0,0 +1,99 @@
1
+ /**
2
+ * The persister-partykit-client module of the TinyBase project contains the
3
+ * client portion of the PartyKit integration.
4
+ *
5
+ * It contains a Persister which, when run in a PartyKit client environment,
6
+ * lets you save and load Store data from the client to durable PartyKit cloud
7
+ * storage of a server (that is using the complementary
8
+ * persister-partykit-server module).
9
+ *
10
+ * This enables synchronization of the same Store across multiple clients in a
11
+ * PartyKit party room.
12
+ *
13
+ * Note that both the client and server parts of this Persister are currently
14
+ * experimental as PartyKit is new and still maturing.
15
+ * @see Persisting Data guide
16
+ * @see Todo App v6 (collaboration) demo
17
+ * @packageDocumentation
18
+ * @module persister-partykit-client
19
+ * @since 4.3.0
20
+ */
21
+
22
+ import {OptionalSchemas, Store} from '../store';
23
+ import PartySocket from 'partysocket';
24
+ import {Persister} from '../persisters';
25
+
26
+ /**
27
+ * The createPartyKitPersister function creates a Persister object that can
28
+ * persist the Store to durable PartyKit storage, enabling synchronization of
29
+ * the same Store across multiple clients.
30
+ *
31
+ * This has schema-based typing. The following is a simplified representation:
32
+ *
33
+ * ```ts override
34
+ * createPartyKitPersister(
35
+ * store: Store,
36
+ * connection: PartySocket,
37
+ * storeUrlProtocol?: 'http' | 'https',
38
+ * onIgnoredError?: (error: any) => void,
39
+ * ): Persister;
40
+ * ```
41
+ *
42
+ * As well as providing a reference to the Store to persist, you must provide a
43
+ * `connection` parameter which is a PartyKit PartySocket that you have already
44
+ * instantiated with details of the host and room.
45
+ *
46
+ * All suitably-equipped TinyBase clients connecting to that room will get to
47
+ * share synchronized Store state.
48
+ *
49
+ * The server room's Store is considered the source of truth. If it is a
50
+ * newly-created room, then calling the save method on this Persister will
51
+ * initiate it. If, however, there is already a Store present on the server, the
52
+ * save method will fail gracefully.
53
+ *
54
+ * In general, you are strongly recommended to use the auto-save and auto-load
55
+ * functionality to stay in sync incrementally with the server, as per the
56
+ * example below. This pattern will handle newly-created servers and
57
+ * newly-created clients - and the synchronization involved in joining rooms,
58
+ * leaving them, or temporarily going offline.
59
+ *
60
+ * See the [PartyKit client socket API
61
+ * documentation](https://docs.partykit.io/reference/partysocket-api/) for more
62
+ * details.
63
+ * @param store The Store to persist.
64
+ * @param connection The PartySocket to use for participating in the PartyKit
65
+ * room.
66
+ * @param storeUrlProtocol The HTTP protocol to use (in addition to the
67
+ * websocket channel). This defaults to 'https' but you may wish to use 'http'
68
+ * for local PartyKit development.
69
+ * @param onIgnoredError An optional handler for the errors that the Persister
70
+ * would otherwise ignore when trying to save or load data. This is suitable for
71
+ * debugging persistence issues in a development environment.
72
+ * @returns A reference to the new Persister object.
73
+ * @example
74
+ * This example creates a Persister object and persists the Store to the
75
+ * browser's IndexedDB storage.
76
+ *
77
+ * ```js yolo
78
+ * const store =
79
+ * createStore()
80
+ * .setTable('pets', {fido: {species: 'dog'}})
81
+ * .setTable('species', {dog: {price: 5}})
82
+ * .setValues({open: true});
83
+ * const partySocket = new PartySocket({host: PARTYKIT_HOST, room: 'my_room'});
84
+ * const persister = createPartyKitPersister(store, partySocket);
85
+ * await persister.startAutoLoad();
86
+ * await persister.startAutoSave();
87
+ * // Store will now be synchronized with the room.
88
+ *
89
+ * persister.destroy();
90
+ * ```
91
+ * @category Creation
92
+ * @since 4.3.0
93
+ */
94
+ export function createPartyKitPersister<Schemas extends OptionalSchemas>(
95
+ store: Store<Schemas>,
96
+ connection: PartySocket,
97
+ storeUrlProtocol?: 'http' | 'https',
98
+ onIgnoredError?: (error: any) => void,
99
+ ): Persister<Schemas>;
@@ -0,0 +1,114 @@
1
+ /**
2
+ * The persister-partykit-server module of the TinyBase project contains the
3
+ * server portion of the PartyKit integration.
4
+ *
5
+ * It contains a class which, when run in a PartyKit server environment, lets
6
+ * you save and load Store data from a client (that is using the complementary
7
+ * persister-partykit-client module) to durable PartyKit cloud storage.
8
+ *
9
+ * This enables synchronization of the same Store across multiple clients in a
10
+ * PartyKit party room.
11
+ *
12
+ * Note that both the client and server parts of this Persister are currently
13
+ * experimental as PartyKit is new and still maturing.
14
+ * @see Persisting Data guide
15
+ * @see Todo App v6 (collaboration) demo
16
+ * @packageDocumentation
17
+ * @module persister-partykit-server
18
+ * @since 4.3.0
19
+ */
20
+
21
+ import {Connection, Request, Server} from 'partykit/server';
22
+
23
+ /**
24
+ * A TinyBasePartyKitServer is the server component for persisting the Store to
25
+ * durable PartyKit storage, enabling synchronization of the same Store across
26
+ * multiple clients.
27
+ *
28
+ * This extends the PartyKit Server class, which provides a selection of methods
29
+ * you are expected to implement. The TinyBasePartyKitServer implements only two
30
+ * of them, the onMessage method and the onRequest method, as well as the
31
+ * constructor.
32
+ *
33
+ * If you wish to use TinyBasePartyKitServer as a general PartyKit server, you
34
+ * can implement other methods. But you must remember to call the super
35
+ * implementations of those methods to ensure the TinyBase synchronization stays
36
+ * supported in addition to your own custom functionality. The same applies to
37
+ * the constructor if you choose to implement that.
38
+ *
39
+ * ```js
40
+ * // This is your PartyKit server entry point.
41
+ *
42
+ * export default class extends TinyBasePartyServer {
43
+ * constructor(party) {
44
+ * super(party);
45
+ * // custom constructor code
46
+ * }
47
+ *
48
+ * async onStart() {
49
+ * // no need to call super.onStart()
50
+ * console.log('Server started');
51
+ * }
52
+ *
53
+ * async onMessage(message, client) {
54
+ * await super.onMessage(message, client);
55
+ * // custom onMessage code
56
+ * }
57
+ *
58
+ * async onRequest(request) {
59
+ * // custom onRequest code, else:
60
+ * return await super.onRequest(request);
61
+ * }
62
+ * }
63
+ * ```
64
+ *
65
+ * See the [PartyKit server API
66
+ * documentation](https://docs.partykit.io/reference/partyserver-api/) for
67
+ * more details.
68
+ */
69
+ export class TinyBasePartyKitServer implements Server {
70
+ /**
71
+ * The onRequest method is called when a HTTP request is made to the party
72
+ * URL.
73
+ *
74
+ * If you choose to implement additional functionality in this method, you
75
+ * must remember to call the super implementation to ensure the TinyBase
76
+ * synchronization stays supported:
77
+ *
78
+ * ```js
79
+ * export default class extends TinyBasePartyServer {
80
+ * async onRequest(request) {
81
+ * // custom onRequest code, else:
82
+ * return await super.onRequest(request);
83
+ * }
84
+ * }
85
+ * ```
86
+ *
87
+ * See the [PartyKit server API
88
+ * documentation](https://docs.partykit.io/reference/partyserver-api/) for
89
+ * more details.
90
+ */
91
+ onRequest(request: Request): Promise<Response>;
92
+ /**
93
+ * The onMessage method is called when the server receives a message from a
94
+ * client.
95
+ *
96
+ * If you choose to implement additional functionality in this method, you
97
+ * must remember to call the super implementation to ensure the TinyBase
98
+ * synchronization stays supported:
99
+ *
100
+ * ```js
101
+ * export default class extends TinyBasePartyServer {
102
+ * async onMessage(message, client) {
103
+ * await super.onMessage(message, client);
104
+ * // custom onMessage code
105
+ * }
106
+ * }
107
+ * ```
108
+ *
109
+ * See the [PartyKit server API
110
+ * documentation](https://docs.partykit.io/reference/partyserver-api/) for
111
+ * more details.
112
+ */
113
+ onMessage(message: string, client: Connection): void;
114
+ }
@@ -5,6 +5,7 @@
5
5
  * @see Persisting Data guide
6
6
  * @packageDocumentation
7
7
  * @module persister-sqlite-wasm
8
+ * @since v4.0.0
8
9
  */
9
10
 
10
11
  import {DatabasePersisterConfig, Persister} from '../persisters';
@@ -114,6 +115,7 @@ import {OptionalSchemas, Store} from '../store';
114
115
  * persister.destroy();
115
116
  * ```
116
117
  * @category Creation
118
+ * @since v4.0.0
117
119
  */
118
120
  export function createSqliteWasmPersister<Schemas extends OptionalSchemas>(
119
121
  store: Store<Schemas>,
@@ -5,6 +5,7 @@
5
5
  * @see Persisting Data guide
6
6
  * @packageDocumentation
7
7
  * @module persister-sqlite3
8
+ * @since v4.0.0
8
9
  */
9
10
 
10
11
  import {DatabasePersisterConfig, Persister} from '../persisters';
@@ -121,6 +122,7 @@ import {Database} from 'sqlite3';
121
122
  * persister.destroy();
122
123
  * ```
123
124
  * @category Creation
125
+ * @since v4.0.0
124
126
  */
125
127
  export function createSqlite3Persister<Schemas extends OptionalSchemas>(
126
128
  store: Store<Schemas>,
@@ -6949,6 +6949,8 @@ export interface Store<in out Schemas extends OptionalSchemas> {
6949
6949
  * Note that a TransactionListener added to the Store with this method can
6950
6950
  * mutate the Store, and its changes will be treated as part of the
6951
6951
  * transaction that is starting.
6952
+ * @param listener The function that will be called at the start of a
6953
+ * transaction.
6952
6954
  * @returns A unique Id for the listener that can later be used to remove it.
6953
6955
  * @example
6954
6956
  * This example registers a listener that is called at start end of the
@@ -7011,6 +7013,8 @@ export interface Store<in out Schemas extends OptionalSchemas> {
7011
7013
  * mutate the Store itself, and its changes will be treated as part of the
7012
7014
  * transaction that is starting (and may fire non-mutating listeners after
7013
7015
  * this).
7016
+ * @param listener The function that will be called before the end of a
7017
+ * transaction.
7014
7018
  * @returns A unique Id for the listener that can later be used to remove it.
7015
7019
  * @example
7016
7020
  * This example registers a listener that is called at the end of the
@@ -7113,6 +7117,8 @@ export interface Store<in out Schemas extends OptionalSchemas> {
7113
7117
  *
7114
7118
  * Note that a TransactionListener added to the Store with this method
7115
7119
  * _cannot_ mutate the Store itself, and attempts to do so will fail silently.
7120
+ * @param listener The function that will be called after the end of a
7121
+ * transaction.
7116
7122
  * @returns A unique Id for the listener that can later be used to remove it.
7117
7123
  * @example
7118
7124
  * This example registers a listener that is called at the end of the