tinybase 6.1.0-beta.0 → 6.1.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.
Files changed (155) hide show
  1. package/@types/_internal/ui-react/with-schemas/index.d.ts +1 -1
  2. package/@types/common/index.d.ts +4 -4
  3. package/@types/common/with-schemas/index.d.ts +4 -4
  4. package/@types/index.d.ts +1 -1
  5. package/@types/persisters/persister-automerge/index.d.ts +1 -1
  6. package/@types/persisters/persister-automerge/with-schemas/index.d.ts +1 -1
  7. package/@types/persisters/persister-cr-sqlite-wasm/index.d.ts +1 -1
  8. package/@types/persisters/persister-cr-sqlite-wasm/with-schemas/index.d.ts +1 -1
  9. package/@types/persisters/persister-electric-sql/index.d.ts +4 -4
  10. package/@types/persisters/persister-electric-sql/with-schemas/index.d.ts +4 -4
  11. package/@types/persisters/persister-expo-sqlite/index.d.ts +1 -1
  12. package/@types/persisters/persister-expo-sqlite/with-schemas/index.d.ts +1 -1
  13. package/@types/persisters/persister-libsql/index.d.ts +1 -1
  14. package/@types/persisters/persister-libsql/with-schemas/index.d.ts +1 -1
  15. package/@types/persisters/persister-partykit-client/index.d.ts +1 -1
  16. package/@types/persisters/persister-partykit-client/with-schemas/index.d.ts +1 -1
  17. package/@types/persisters/persister-partykit-server/index.d.ts +7 -7
  18. package/@types/persisters/persister-partykit-server/with-schemas/index.d.ts +1 -1
  19. package/@types/persisters/persister-pglite/index.d.ts +1 -1
  20. package/@types/persisters/persister-pglite/with-schemas/index.d.ts +1 -1
  21. package/@types/persisters/persister-postgres/index.d.ts +1 -1
  22. package/@types/persisters/persister-postgres/with-schemas/index.d.ts +1 -1
  23. package/@types/persisters/persister-powersync/index.d.ts +1 -1
  24. package/@types/persisters/persister-powersync/with-schemas/index.d.ts +1 -1
  25. package/@types/persisters/persister-sqlite3/index.d.ts +1 -1
  26. package/@types/persisters/persister-sqlite3/with-schemas/index.d.ts +1 -1
  27. package/@types/persisters/persister-yjs/index.d.ts +1 -1
  28. package/@types/persisters/persister-yjs/with-schemas/index.d.ts +1 -1
  29. package/@types/synchronizers/synchronizer-ws-client/index.d.ts +1 -1
  30. package/@types/synchronizers/synchronizer-ws-client/with-schemas/index.d.ts +1 -1
  31. package/@types/synchronizers/synchronizer-ws-server/index.d.ts +1 -1
  32. package/@types/synchronizers/synchronizer-ws-server/with-schemas/index.d.ts +1 -1
  33. package/@types/synchronizers/synchronizer-ws-server-durable-object/index.d.ts +1 -1
  34. package/@types/synchronizers/synchronizer-ws-server-durable-object/with-schemas/index.d.ts +1 -1
  35. package/@types/ui-react/index.d.ts +3 -3
  36. package/@types/ui-react/with-schemas/index.d.ts +1 -1
  37. package/@types/ui-react-dom/index.d.ts +1 -1
  38. package/@types/ui-react-dom/with-schemas/index.d.ts +1 -1
  39. package/@types/ui-react-inspector/with-schemas/index.d.ts +1 -1
  40. package/@types/with-schemas/index.d.ts +1 -1
  41. package/index.js +1579 -1567
  42. package/indexes/index.js +15 -3
  43. package/indexes/with-schemas/index.js +15 -3
  44. package/min/index.js +1 -1
  45. package/min/index.js.gz +0 -0
  46. package/min/indexes/index.js +1 -1
  47. package/min/indexes/index.js.gz +0 -0
  48. package/min/indexes/with-schemas/index.js +1 -1
  49. package/min/indexes/with-schemas/index.js.gz +0 -0
  50. package/min/persisters/index.js +1 -1
  51. package/min/persisters/index.js.gz +0 -0
  52. package/min/persisters/with-schemas/index.js +1 -1
  53. package/min/persisters/with-schemas/index.js.gz +0 -0
  54. package/min/ui-react/index.js +1 -1
  55. package/min/ui-react/index.js.gz +0 -0
  56. package/min/ui-react/with-schemas/index.js +1 -1
  57. package/min/ui-react/with-schemas/index.js.gz +0 -0
  58. package/min/with-schemas/index.js +1 -1
  59. package/min/with-schemas/index.js.gz +0 -0
  60. package/package.json +7 -10
  61. package/persisters/index.js +95 -95
  62. package/persisters/with-schemas/index.js +95 -95
  63. package/readme.md +13 -13
  64. package/releases.md +25 -25
  65. package/synchronizers/synchronizer-ws-server-durable-object/index.js +1 -1
  66. package/synchronizers/synchronizer-ws-server-durable-object/with-schemas/index.js +1 -1
  67. package/ui-react/index.js +64 -53
  68. package/ui-react/with-schemas/index.js +64 -53
  69. package/ui-react-dom/index.js +3 -3
  70. package/ui-react-dom/with-schemas/index.js +3 -3
  71. package/ui-react-inspector/index.js +4 -4
  72. package/ui-react-inspector/with-schemas/index.js +4 -4
  73. package/with-schemas/index.js +1579 -1567
  74. package/@types/_internal/queries/index.d.cts +0 -0
  75. package/@types/_internal/queries/with-schemas/index.d.cts +0 -22
  76. package/@types/_internal/store/index.d.cts +0 -3
  77. package/@types/_internal/store/with-schemas/index.d.cts +0 -106
  78. package/@types/_internal/ui-react/index.d.cts +0 -0
  79. package/@types/_internal/ui-react/with-schemas/index.d.cts +0 -1130
  80. package/@types/checkpoints/index.d.cts +0 -1059
  81. package/@types/checkpoints/with-schemas/index.d.cts +0 -1151
  82. package/@types/common/index.d.cts +0 -158
  83. package/@types/common/with-schemas/index.d.cts +0 -158
  84. package/@types/index.d.cts +0 -17
  85. package/@types/indexes/index.d.cts +0 -1064
  86. package/@types/indexes/with-schemas/index.d.cts +0 -1210
  87. package/@types/mergeable-store/index.d.cts +0 -1139
  88. package/@types/mergeable-store/with-schemas/index.d.cts +0 -1628
  89. package/@types/metrics/index.d.cts +0 -917
  90. package/@types/metrics/with-schemas/index.d.cts +0 -1004
  91. package/@types/persisters/index.d.cts +0 -1877
  92. package/@types/persisters/persister-automerge/index.d.cts +0 -165
  93. package/@types/persisters/persister-automerge/with-schemas/index.d.cts +0 -180
  94. package/@types/persisters/persister-browser/index.d.cts +0 -185
  95. package/@types/persisters/persister-browser/with-schemas/index.d.cts +0 -208
  96. package/@types/persisters/persister-cr-sqlite-wasm/index.d.cts +0 -159
  97. package/@types/persisters/persister-cr-sqlite-wasm/with-schemas/index.d.cts +0 -178
  98. package/@types/persisters/persister-durable-object-storage/index.d.cts +0 -122
  99. package/@types/persisters/persister-durable-object-storage/with-schemas/index.d.cts +0 -136
  100. package/@types/persisters/persister-electric-sql/index.d.cts +0 -185
  101. package/@types/persisters/persister-electric-sql/with-schemas/index.d.cts +0 -204
  102. package/@types/persisters/persister-expo-sqlite/index.d.cts +0 -186
  103. package/@types/persisters/persister-expo-sqlite/with-schemas/index.d.cts +0 -205
  104. package/@types/persisters/persister-file/index.d.cts +0 -94
  105. package/@types/persisters/persister-file/with-schemas/index.d.cts +0 -107
  106. package/@types/persisters/persister-indexed-db/index.d.cts +0 -120
  107. package/@types/persisters/persister-indexed-db/with-schemas/index.d.cts +0 -135
  108. package/@types/persisters/persister-libsql/index.d.cts +0 -158
  109. package/@types/persisters/persister-libsql/with-schemas/index.d.cts +0 -177
  110. package/@types/persisters/persister-partykit-client/index.d.cts +0 -195
  111. package/@types/persisters/persister-partykit-client/with-schemas/index.d.cts +0 -210
  112. package/@types/persisters/persister-partykit-server/index.d.cts +0 -650
  113. package/@types/persisters/persister-partykit-server/with-schemas/index.d.cts +0 -695
  114. package/@types/persisters/persister-pglite/index.d.cts +0 -177
  115. package/@types/persisters/persister-pglite/with-schemas/index.d.cts +0 -196
  116. package/@types/persisters/persister-postgres/index.d.cts +0 -166
  117. package/@types/persisters/persister-postgres/with-schemas/index.d.cts +0 -185
  118. package/@types/persisters/persister-powersync/index.d.cts +0 -174
  119. package/@types/persisters/persister-powersync/with-schemas/index.d.cts +0 -193
  120. package/@types/persisters/persister-remote/index.d.cts +0 -117
  121. package/@types/persisters/persister-remote/with-schemas/index.d.cts +0 -133
  122. package/@types/persisters/persister-sqlite-wasm/index.d.cts +0 -175
  123. package/@types/persisters/persister-sqlite-wasm/with-schemas/index.d.cts +0 -195
  124. package/@types/persisters/persister-sqlite3/index.d.cts +0 -176
  125. package/@types/persisters/persister-sqlite3/with-schemas/index.d.cts +0 -195
  126. package/@types/persisters/persister-yjs/index.d.cts +0 -161
  127. package/@types/persisters/persister-yjs/with-schemas/index.d.cts +0 -176
  128. package/@types/persisters/with-schemas/index.d.cts +0 -2054
  129. package/@types/queries/index.d.cts +0 -3695
  130. package/@types/queries/with-schemas/index.d.cts +0 -4016
  131. package/@types/relationships/index.d.cts +0 -1320
  132. package/@types/relationships/with-schemas/index.d.cts +0 -1474
  133. package/@types/store/index.d.cts +0 -7598
  134. package/@types/store/with-schemas/index.d.cts +0 -9278
  135. package/@types/synchronizers/index.d.cts +0 -485
  136. package/@types/synchronizers/synchronizer-broadcast-channel/index.d.cts +0 -121
  137. package/@types/synchronizers/synchronizer-broadcast-channel/with-schemas/index.d.cts +0 -137
  138. package/@types/synchronizers/synchronizer-local/index.d.cts +0 -95
  139. package/@types/synchronizers/synchronizer-local/with-schemas/index.d.cts +0 -114
  140. package/@types/synchronizers/synchronizer-ws-client/index.d.cts +0 -160
  141. package/@types/synchronizers/synchronizer-ws-client/with-schemas/index.d.cts +0 -179
  142. package/@types/synchronizers/synchronizer-ws-server/index.d.cts +0 -736
  143. package/@types/synchronizers/synchronizer-ws-server/with-schemas/index.d.cts +0 -765
  144. package/@types/synchronizers/synchronizer-ws-server-durable-object/index.d.cts +0 -311
  145. package/@types/synchronizers/synchronizer-ws-server-durable-object/with-schemas/index.d.cts +0 -349
  146. package/@types/synchronizers/synchronizer-ws-server-simple/index.d.cts +0 -144
  147. package/@types/synchronizers/synchronizer-ws-server-simple/with-schemas/index.d.cts +0 -144
  148. package/@types/synchronizers/with-schemas/index.d.cts +0 -503
  149. package/@types/ui-react/index.d.cts +0 -16640
  150. package/@types/ui-react/with-schemas/index.d.cts +0 -17281
  151. package/@types/ui-react-dom/index.d.cts +0 -1862
  152. package/@types/ui-react-dom/with-schemas/index.d.cts +0 -1994
  153. package/@types/ui-react-inspector/index.d.cts +0 -79
  154. package/@types/ui-react-inspector/with-schemas/index.d.cts +0 -1985
  155. package/@types/with-schemas/index.d.cts +0 -17
@@ -1,503 +0,0 @@
1
- /**
2
- * The synchronizers module of the TinyBase project lets you synchronize
3
- * MergeableStore data to and from other MergeableStore instances.
4
- * @see Synchronization guide
5
- * @packageDocumentation
6
- * @module synchronizers
7
- * @since v5.0.0
8
- */
9
- import type {Id, IdOrNull} from '../../common/with-schemas/index.d.cts';
10
- import type {MergeableStore} from '../../mergeable-store/with-schemas/index.d.cts';
11
- import type {
12
- Persister,
13
- Persists,
14
- } from '../../persisters/with-schemas/index.d.cts';
15
- import type {
16
- Content,
17
- OptionalSchemas,
18
- } from '../../store/with-schemas/index.d.cts';
19
-
20
- /**
21
- * The Message enum is used to indicate the type of the message being passed
22
- * between Synchronizer instances.
23
- *
24
- * These message comprise the basic synchronization protocol for merging
25
- * MergeableStore instances across multiple systems.
26
- *
27
- * The enum is generally intended to be used internally within TinyBase itself
28
- * and opaque to applications that use synchronization.
29
- * @category Synchronization
30
- * @since v5.0.0
31
- */
32
- export const enum Message {
33
- /**
34
- * A message that is a response to a previous request.
35
- * @category Enum
36
- * @since v5.0.0
37
- */
38
- Response = 0,
39
- /**
40
- * A message that is a request to get ContentHashes from another
41
- * MergeableStore.
42
- * @category Enum
43
- * @since v5.0.0
44
- */
45
- GetContentHashes = 1,
46
- /**
47
- * A message that contains ContentHashes.
48
- * @category Enum
49
- * @since v5.0.0
50
- */
51
- ContentHashes = 2,
52
- /**
53
- * A message that contains a ContentDiff.
54
- * @category Enum
55
- * @since v5.0.0
56
- */
57
- ContentDiff = 3,
58
- /**
59
- * A message that is a request to get a TableDiff from another MergeableStore.
60
- * @category Enum
61
- * @since v5.0.0
62
- */
63
- GetTableDiff = 4,
64
- /**
65
- * A message that is a request to get a RowDiff from another MergeableStore.
66
- * @category Enum
67
- * @since v5.0.0
68
- */
69
- GetRowDiff = 5,
70
- /**
71
- * A message that is a request to get a CellDiff from another MergeableStore.
72
- * @category Enum
73
- * @since v5.0.0
74
- */
75
- GetCellDiff = 6,
76
- /**
77
- * A message that is a request to get a ValueDiff from another MergeableStore.
78
- * @category Enum
79
- * @since v5.0.0
80
- */
81
- GetValueDiff = 7,
82
- }
83
-
84
- /**
85
- * The Send type describes a function that knows how to dispatch a message as
86
- * part of the synchronization protocol.
87
- * @param toClientId The optional Id of the other client (in other words, the
88
- * other system) to which the message should be sent. If omitted, this is to be
89
- * a broadcast.
90
- * @param requestId The optional Id of the message, which should be awaited in
91
- * the response (if requested) to constitute a matched request/response
92
- * transaction.
93
- * @param message A number that indicates the type of the message, according to
94
- * the Message enum.
95
- * @param body A message-specific payload.
96
- * @category Synchronization
97
- * @since v5.0.0
98
- */
99
- export type Send = (
100
- toClientId: IdOrNull,
101
- requestId: Id,
102
- message: Message,
103
- body: any,
104
- ) => void;
105
-
106
- /**
107
- * The Receive type describes a function that knows how to handle the arrival of
108
- * a message as part of the synchronization protocol.
109
- *
110
- * When a message arrives (most likely from another system), the function will
111
- * be called with parameters that indicate where the message came from, and its
112
- * meaning and content.
113
- * @param fromClientId The Id of the other client (in other words, the other
114
- * system) that sent the message.
115
- * @param requestId The optional Id of the message, which should be returned in
116
- * the response (if requested) to constitute a matched request/response
117
- * transaction.
118
- * @param message A number that indicates the type of the message, according to
119
- * the Message enum.
120
- * @param body A message-specific payload.
121
- * @category Synchronization
122
- * @since v5.0.0
123
- */
124
- export type Receive = (
125
- fromClientId: Id,
126
- requestId: Id,
127
- message: Message,
128
- body: any,
129
- ) => void;
130
-
131
- /**
132
- * The SynchronizerStats type describes the number of times a Synchronizer
133
- * object has sent or received data.
134
- *
135
- * A SynchronizerStats object is returned from the getSynchronizerStats method.
136
- * @category Development
137
- * @since v5.0.0
138
- */
139
- export type SynchronizerStats = {
140
- sends: number;
141
- receives: number;
142
- };
143
-
144
- /**
145
- * A Synchronizer object lets you synchronize MergeableStore data with another
146
- * TinyBase client or system.
147
- *
148
- * This is useful for sharing data between users, or between devices of a single
149
- * user. This is especially valuable when there is the possibility that there
150
- * has been a lack of immediate connectivity between clients and the
151
- * synchronization requires some negotiation to orchestrate merging the
152
- * MergeableStore objects together.
153
- *
154
- * Creating a Synchronizer depends on the choice of underlying medium over which
155
- * the synchronization will take place. Options include the createWsSynchronizer
156
- * function (for a Synchronizer that will sync over web-sockets), and the
157
- * createLocalSynchronizer function (for a Synchronizer that will sync two
158
- * MergeableStore objects in memory on one system). The createCustomSynchronizer
159
- * function can also be used to easily create a fully customized way to send and
160
- * receive the messages of the synchronization protocol.
161
- *
162
- * Note that, as an interface, it is an extension to the Persister interface,
163
- * since they share underlying implementations. Think of a Synchronizer as
164
- * 'persisting' your MergeableStore to another client (and vice-versa).
165
- * @example
166
- * This example creates two empty MergeableStore objects, creates a
167
- * LocalSynchronizer for each, and starts synchronizing them. A change in one
168
- * becomes evident in the other.
169
- *
170
- * ```js
171
- * import {createMergeableStore} from 'tinybase';
172
- * import {createLocalSynchronizer} from 'tinybase/synchronizers/synchronizer-local';
173
- *
174
- * const store1 = createMergeableStore();
175
- * const store2 = createMergeableStore();
176
- *
177
- * const synchronizer1 = createLocalSynchronizer(store1);
178
- * const synchronizer2 = createLocalSynchronizer(store2);
179
- *
180
- * await synchronizer1.startSync();
181
- * await synchronizer2.startSync();
182
- *
183
- * store1.setTables({pets: {fido: {species: 'dog'}}});
184
- * // ...
185
- * console.log(store2.getTables());
186
- * // -> {pets: {fido: {species: 'dog'}}}
187
- *
188
- * store2.setRow('pets', 'felix', {species: 'cat'});
189
- * // ...
190
- * console.log(store1.getTables());
191
- * // -> {pets: {fido: {species: 'dog'}, felix: {species: 'cat'}}}
192
- *
193
- * synchronizer1.destroy();
194
- * synchronizer2.destroy();
195
- * ```
196
- * @category Synchronizer
197
- * @since v5.0.0
198
- */
199
- export interface Synchronizer<Schemas extends OptionalSchemas>
200
- extends Persister<Schemas, Persists.MergeableStoreOnly> {
201
- /**
202
- * The startSync method is used to start the process of synchronization
203
- * between this instance and another matching Synchronizer.
204
- *
205
- * This has schema-based typing. The following is a simplified representation:
206
- *
207
- * ```ts override
208
- * startSync(initialContent?: Content): Promise<this>;
209
- * ```
210
- *
211
- * The underlying implementation of a Synchronizer is shared with the
212
- * Persister framework, and so this startSync method is equivalent to starting
213
- * both auto-loading (listening to sync messages from other active
214
- * Synchronizer instances) and auto-saving (sending sync messages to it).
215
- *
216
- * This method is asynchronous so you should you `await` calls to this method
217
- * or handle the return type natively as a Promise.
218
- * @param initialContent An optional Content object used when no content is
219
- * available from another other peer Synchronizer instances.
220
- * @returns A Promise containing a reference to the Synchronizer object.
221
- * @example
222
- * This example creates two empty MergeableStore objects, creates a
223
- * LocalSynchronizer for each, and starts synchronizing them. A change in one
224
- * becomes evident in the other.
225
- *
226
- * ```js
227
- * import {createMergeableStore} from 'tinybase';
228
- * import {createLocalSynchronizer} from 'tinybase/synchronizers/synchronizer-local';
229
- *
230
- * const store1 = createMergeableStore();
231
- * const store2 = createMergeableStore();
232
- *
233
- * const synchronizer1 = createLocalSynchronizer(store1);
234
- * const synchronizer2 = createLocalSynchronizer(store2);
235
- *
236
- * await synchronizer1.startSync();
237
- * await synchronizer2.startSync();
238
- *
239
- * store1.setTables({pets: {fido: {species: 'dog'}}});
240
- * // ...
241
- * console.log(store2.getTables());
242
- * // -> {pets: {fido: {species: 'dog'}}}
243
- *
244
- * store2.setRow('pets', 'felix', {species: 'cat'});
245
- * // ...
246
- * console.log(store1.getTables());
247
- * // -> {pets: {fido: {species: 'dog'}, felix: {species: 'cat'}}}
248
- *
249
- * synchronizer1.destroy();
250
- * synchronizer2.destroy();
251
- * ```
252
- * @example
253
- * This example creates two empty MergeableStore objects, creates a
254
- * LocalSynchronizer for each, and starts synchronizing them with default
255
- * content. The default content from the first Synchronizer's startSync method
256
- * ends up populated in both MergeableStore instances: by the time the second
257
- * started, the first was available to synchronize with and its default was
258
- * not needed.
259
- *
260
- * ```js
261
- * import {createMergeableStore} from 'tinybase';
262
- * import {createLocalSynchronizer} from 'tinybase/synchronizers/synchronizer-local';
263
- *
264
- * const store1 = createMergeableStore();
265
- * const store2 = createMergeableStore();
266
- *
267
- * const synchronizer1 = createLocalSynchronizer(store1);
268
- * const synchronizer2 = createLocalSynchronizer(store2);
269
- *
270
- * await synchronizer1.startSync([{pets: {fido: {species: 'dog'}}}, {}]);
271
- * await synchronizer2.startSync([{pets: {felix: {species: 'cat'}}}, {}]);
272
- *
273
- * // ...
274
- *
275
- * console.log(store1.getTables());
276
- * // -> {pets: {fido: {species: 'dog'}}}
277
- * console.log(store2.getTables());
278
- * // -> {pets: {fido: {species: 'dog'}}}
279
- *
280
- * synchronizer1.destroy();
281
- * synchronizer2.destroy();
282
- * ```
283
- * @category Synchronization
284
- * @since v5.0.0
285
- */
286
- startSync(initialContent?: Content<Schemas, true>): Promise<this>;
287
- /**
288
- * The stopSync method is used to stop the process of synchronization between
289
- * this instance and another matching Synchronizer.
290
- *
291
- * The underlying implementation of a Synchronizer is shared with the
292
- * Persister framework, and so this startSync method is equivalent to stopping
293
- * both auto-loading (listening to sync messages from other active
294
- * Synchronizer instances) and auto-saving (sending sync messages to them).
295
- * @returns A reference to the Synchronizer object.
296
- * @example
297
- * This example creates two empty MergeableStore objects, creates a
298
- * LocalSynchronizer for each, and starts - then stops - synchronizing them.
299
- * Subsequent changes are not merged.
300
- *
301
- * ```js
302
- * import {createMergeableStore} from 'tinybase';
303
- * import {createLocalSynchronizer} from 'tinybase/synchronizers/synchronizer-local';
304
- *
305
- * const store1 = createMergeableStore();
306
- * const synchronizer1 = createLocalSynchronizer(store1);
307
- * await synchronizer1.startSync();
308
- *
309
- * const store2 = createMergeableStore();
310
- * const synchronizer2 = createLocalSynchronizer(store2);
311
- * await synchronizer2.startSync();
312
- *
313
- * store1.setTables({pets: {fido: {species: 'dog'}}});
314
- * // ...
315
- * console.log(store1.getTables());
316
- * // -> {pets: {fido: {species: 'dog'}}}
317
- * console.log(store2.getTables());
318
- * // -> {pets: {fido: {species: 'dog'}}}
319
- *
320
- * synchronizer1.stopSync();
321
- * synchronizer2.stopSync();
322
- *
323
- * store1.setCell('pets', 'fido', 'color', 'brown');
324
- * // ...
325
- * console.log(store1.getTables());
326
- * // -> {pets: {fido: {species: 'dog', color: 'brown'}}}
327
- * console.log(store2.getTables());
328
- * // -> {pets: {fido: {species: 'dog'}}}
329
- *
330
- * synchronizer1.destroy();
331
- * synchronizer2.destroy();
332
- * ```
333
- * @category Synchronization
334
- * @since v5.0.0
335
- */
336
- stopSync(): this;
337
- /**
338
- * The getSynchronizerStats method provides a set of statistics about the
339
- * Synchronizer, and is used for debugging purposes.
340
- *
341
- * The SynchronizerStats object contains a count of the number of times the
342
- * Synchronizer has sent and received messages.
343
- *
344
- * The method is intended to be used during development to ensure your
345
- * synchronization layer is acting as expected, for example.
346
- * @returns A SynchronizerStats object containing Synchronizer send and
347
- * receive statistics.
348
- * @example
349
- * This example gets the send and receive statistics of two active
350
- * Synchronizer instances.
351
- *
352
- * ```js
353
- * import {createMergeableStore} from 'tinybase';
354
- * import {createLocalSynchronizer} from 'tinybase/synchronizers/synchronizer-local';
355
- *
356
- * const store1 = createMergeableStore();
357
- * const store2 = createMergeableStore();
358
- *
359
- * const synchronizer1 = createLocalSynchronizer(store1);
360
- * const synchronizer2 = createLocalSynchronizer(store2);
361
- *
362
- * await synchronizer1.startSync();
363
- * await synchronizer2.startSync();
364
- *
365
- * store1.setTables({pets: {fido: {species: 'dog'}}});
366
- * // ...
367
- * store2.setRow('pets', 'felix', {species: 'cat'});
368
- * // ...
369
- *
370
- * console.log(synchronizer1.getSynchronizerStats());
371
- * // -> {receives: 4, sends: 5}
372
- * console.log(synchronizer2.getSynchronizerStats());
373
- * // -> {receives: 5, sends: 4}
374
- *
375
- * synchronizer1.destroy();
376
- * synchronizer2.destroy();
377
- * ```
378
- * @category Synchronization
379
- * @since v5.0.0
380
- */
381
- getSynchronizerStats(): SynchronizerStats;
382
- }
383
-
384
- /**
385
- * The createCustomSynchronizer function creates a Synchronizer object that can
386
- * persist one MergeableStore to another.
387
- *
388
- * This has schema-based typing. The following is a simplified representation:
389
- *
390
- * ```ts override
391
- * createCustomSynchronizer(
392
- * store: MergeableStore,
393
- * send: Send,
394
- * registerReceive: (receive: Receive) => void,
395
- * destroy: () => void,
396
- * requestTimeoutSeconds: number,
397
- * onSend?: Send,
398
- * onReceive?: Receive,
399
- * onIgnoredError?: (error: any) => void,
400
- * ): Synchronizer;
401
- * ```
402
- *
403
- * As well as providing a reference to the MergeableStore to synchronize, you
404
- * must provide parameters which identify how to send and receive changes to and
405
- * from this MergeableStore and its peers. This is entirely dependent upon the
406
- * medium of communication used.
407
- *
408
- * You must also provide a callback for when the Synchronizer is destroyed
409
- * (which is a good place to clean up resources and stop communication
410
- * listeners), and indicate how long the Synchronizer will wait for responses to
411
- * message requests before timing out.
412
- *
413
- * A final set of optional handlers can be provided to help debug sends,
414
- * receives, and errors respectively.
415
- * @param store The MergeableStore to synchronize.
416
- * @param send A Send function for sending a message.
417
- * @param registerReceive A callback (called once when the Synchronizer is
418
- * created) that is passed a Receive function that you need to ensure will
419
- * receive messages addressed or broadcast to this client.
420
- * @param destroy A function called when destroying the Synchronizer which can
421
- * be used to clean up underlying resources.
422
- * @param requestTimeoutSeconds An number of seconds before a request sent from
423
- * this Synchronizer to another peer times out.
424
- * @param onSend An optional handler for the messages that this Synchronizer
425
- * sends. This is suitable for debugging synchronization issues in a development
426
- * environment, since v5.1.
427
- * @param onReceive An optional handler for the messages that this Synchronizer
428
- * receives. This is suitable for debugging synchronization issues in a
429
- * development environment, since v5.1.
430
- * @param onIgnoredError An optional handler for the errors that the
431
- * Synchronizer would otherwise ignore when trying to synchronize data. This is
432
- * suitable for debugging synchronization issues in a development environment.
433
- * @returns A reference to the new Synchronizer object.
434
- * @example
435
- * This example creates a function for creating custom Synchronizer objects via
436
- * a very naive pair of message buses (which are first-in, first-out). Each
437
- * Synchronizer can write to the other's bus, and they each poll to read from
438
- * their own. The example then uses these Synchronizer instances to sync two
439
- * MergeableStore objects together
440
- *
441
- * ```js
442
- * import {createMergeableStore, getUniqueId} from 'tinybase';
443
- * import {createCustomSynchronizer} from 'tinybase/synchronizers';
444
- *
445
- * const bus1 = [];
446
- * const bus2 = [];
447
- *
448
- * const createBusSynchronizer = (store, localBus, remoteBus) => {
449
- * let timer;
450
- * const clientId = getUniqueId();
451
- * return createCustomSynchronizer(
452
- * store,
453
- * (toClientId, requestId, message, body) => {
454
- * // send
455
- * remoteBus.push([clientId, requestId, message, body]);
456
- * },
457
- * (receive) => {
458
- * // registerReceive
459
- * timer = setInterval(() => {
460
- * if (localBus.length > 0) {
461
- * receive(...localBus.shift());
462
- * }
463
- * }, 1);
464
- * },
465
- * () => clearInterval(timer), // destroy
466
- * 1,
467
- * );
468
- * };
469
- *
470
- * const store1 = createMergeableStore();
471
- * const store2 = createMergeableStore();
472
- *
473
- * const synchronizer1 = createBusSynchronizer(store1, bus1, bus2);
474
- * const synchronizer2 = createBusSynchronizer(store2, bus2, bus1);
475
- *
476
- * await synchronizer1.startSync();
477
- * await synchronizer2.startSync();
478
- *
479
- * store1.setTables({pets: {fido: {species: 'dog'}}});
480
- * store2.setTables({pets: {felix: {species: 'cat'}}});
481
- *
482
- * // ...
483
- * console.log(store1.getTables());
484
- * // -> {pets: {fido: {species: 'dog'}, felix: {species: 'cat'}}}
485
- * console.log(store2.getTables());
486
- * // -> {pets: {fido: {species: 'dog'}, felix: {species: 'cat'}}}
487
- *
488
- * synchronizer1.destroy();
489
- * synchronizer2.destroy();
490
- * ```
491
- * @category Creation
492
- * @since v5.0.0
493
- */
494
- export function createCustomSynchronizer<Schemas extends OptionalSchemas>(
495
- store: MergeableStore<Schemas>,
496
- send: Send,
497
- registerReceive: (receive: Receive) => void,
498
- destroy: () => void,
499
- requestTimeoutSeconds: number,
500
- onSend?: Send,
501
- onReceive?: Receive,
502
- onIgnoredError?: (error: any) => void,
503
- ): Synchronizer<Schemas>;