tinybase 6.1.0-beta.1 → 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 (98) hide show
  1. package/@types/common/index.d.ts +4 -4
  2. package/@types/common/with-schemas/index.d.ts +4 -4
  3. package/index.js +15 -3
  4. package/indexes/index.js +15 -3
  5. package/indexes/with-schemas/index.js +15 -3
  6. package/min/index.js +1 -1
  7. package/min/index.js.gz +0 -0
  8. package/min/indexes/index.js +1 -1
  9. package/min/indexes/index.js.gz +0 -0
  10. package/min/indexes/with-schemas/index.js +1 -1
  11. package/min/indexes/with-schemas/index.js.gz +0 -0
  12. package/min/with-schemas/index.js +1 -1
  13. package/min/with-schemas/index.js.gz +0 -0
  14. package/package.json +7 -7
  15. package/readme.md +1 -1
  16. package/with-schemas/index.js +15 -3
  17. package/@types/_internal/queries/index.d.cts +0 -0
  18. package/@types/_internal/queries/with-schemas/index.d.cts +0 -22
  19. package/@types/_internal/store/index.d.cts +0 -3
  20. package/@types/_internal/store/with-schemas/index.d.cts +0 -106
  21. package/@types/_internal/ui-react/index.d.cts +0 -0
  22. package/@types/_internal/ui-react/with-schemas/index.d.cts +0 -1130
  23. package/@types/checkpoints/index.d.cts +0 -1059
  24. package/@types/checkpoints/with-schemas/index.d.cts +0 -1151
  25. package/@types/common/index.d.cts +0 -158
  26. package/@types/common/with-schemas/index.d.cts +0 -158
  27. package/@types/index.d.cts +0 -17
  28. package/@types/indexes/index.d.cts +0 -1064
  29. package/@types/indexes/with-schemas/index.d.cts +0 -1210
  30. package/@types/mergeable-store/index.d.cts +0 -1139
  31. package/@types/mergeable-store/with-schemas/index.d.cts +0 -1628
  32. package/@types/metrics/index.d.cts +0 -917
  33. package/@types/metrics/with-schemas/index.d.cts +0 -1004
  34. package/@types/persisters/index.d.cts +0 -1877
  35. package/@types/persisters/persister-automerge/index.d.cts +0 -165
  36. package/@types/persisters/persister-automerge/with-schemas/index.d.cts +0 -180
  37. package/@types/persisters/persister-browser/index.d.cts +0 -185
  38. package/@types/persisters/persister-browser/with-schemas/index.d.cts +0 -208
  39. package/@types/persisters/persister-cr-sqlite-wasm/index.d.cts +0 -159
  40. package/@types/persisters/persister-cr-sqlite-wasm/with-schemas/index.d.cts +0 -178
  41. package/@types/persisters/persister-durable-object-storage/index.d.cts +0 -122
  42. package/@types/persisters/persister-durable-object-storage/with-schemas/index.d.cts +0 -136
  43. package/@types/persisters/persister-electric-sql/index.d.cts +0 -185
  44. package/@types/persisters/persister-electric-sql/with-schemas/index.d.cts +0 -204
  45. package/@types/persisters/persister-expo-sqlite/index.d.cts +0 -186
  46. package/@types/persisters/persister-expo-sqlite/with-schemas/index.d.cts +0 -205
  47. package/@types/persisters/persister-file/index.d.cts +0 -94
  48. package/@types/persisters/persister-file/with-schemas/index.d.cts +0 -107
  49. package/@types/persisters/persister-indexed-db/index.d.cts +0 -120
  50. package/@types/persisters/persister-indexed-db/with-schemas/index.d.cts +0 -135
  51. package/@types/persisters/persister-libsql/index.d.cts +0 -158
  52. package/@types/persisters/persister-libsql/with-schemas/index.d.cts +0 -177
  53. package/@types/persisters/persister-partykit-client/index.d.cts +0 -195
  54. package/@types/persisters/persister-partykit-client/with-schemas/index.d.cts +0 -210
  55. package/@types/persisters/persister-partykit-server/index.d.cts +0 -650
  56. package/@types/persisters/persister-partykit-server/with-schemas/index.d.cts +0 -695
  57. package/@types/persisters/persister-pglite/index.d.cts +0 -177
  58. package/@types/persisters/persister-pglite/with-schemas/index.d.cts +0 -196
  59. package/@types/persisters/persister-postgres/index.d.cts +0 -166
  60. package/@types/persisters/persister-postgres/with-schemas/index.d.cts +0 -185
  61. package/@types/persisters/persister-powersync/index.d.cts +0 -174
  62. package/@types/persisters/persister-powersync/with-schemas/index.d.cts +0 -193
  63. package/@types/persisters/persister-remote/index.d.cts +0 -117
  64. package/@types/persisters/persister-remote/with-schemas/index.d.cts +0 -133
  65. package/@types/persisters/persister-sqlite-wasm/index.d.cts +0 -175
  66. package/@types/persisters/persister-sqlite-wasm/with-schemas/index.d.cts +0 -195
  67. package/@types/persisters/persister-sqlite3/index.d.cts +0 -176
  68. package/@types/persisters/persister-sqlite3/with-schemas/index.d.cts +0 -195
  69. package/@types/persisters/persister-yjs/index.d.cts +0 -161
  70. package/@types/persisters/persister-yjs/with-schemas/index.d.cts +0 -176
  71. package/@types/persisters/with-schemas/index.d.cts +0 -2054
  72. package/@types/queries/index.d.cts +0 -3695
  73. package/@types/queries/with-schemas/index.d.cts +0 -4016
  74. package/@types/relationships/index.d.cts +0 -1320
  75. package/@types/relationships/with-schemas/index.d.cts +0 -1474
  76. package/@types/store/index.d.cts +0 -7598
  77. package/@types/store/with-schemas/index.d.cts +0 -9278
  78. package/@types/synchronizers/index.d.cts +0 -485
  79. package/@types/synchronizers/synchronizer-broadcast-channel/index.d.cts +0 -121
  80. package/@types/synchronizers/synchronizer-broadcast-channel/with-schemas/index.d.cts +0 -137
  81. package/@types/synchronizers/synchronizer-local/index.d.cts +0 -95
  82. package/@types/synchronizers/synchronizer-local/with-schemas/index.d.cts +0 -114
  83. package/@types/synchronizers/synchronizer-ws-client/index.d.cts +0 -160
  84. package/@types/synchronizers/synchronizer-ws-client/with-schemas/index.d.cts +0 -179
  85. package/@types/synchronizers/synchronizer-ws-server/index.d.cts +0 -736
  86. package/@types/synchronizers/synchronizer-ws-server/with-schemas/index.d.cts +0 -765
  87. package/@types/synchronizers/synchronizer-ws-server-durable-object/index.d.cts +0 -311
  88. package/@types/synchronizers/synchronizer-ws-server-durable-object/with-schemas/index.d.cts +0 -349
  89. package/@types/synchronizers/synchronizer-ws-server-simple/index.d.cts +0 -144
  90. package/@types/synchronizers/synchronizer-ws-server-simple/with-schemas/index.d.cts +0 -144
  91. package/@types/synchronizers/with-schemas/index.d.cts +0 -503
  92. package/@types/ui-react/index.d.cts +0 -16640
  93. package/@types/ui-react/with-schemas/index.d.cts +0 -17281
  94. package/@types/ui-react-dom/index.d.cts +0 -1862
  95. package/@types/ui-react-dom/with-schemas/index.d.cts +0 -1994
  96. package/@types/ui-react-inspector/index.d.cts +0 -79
  97. package/@types/ui-react-inspector/with-schemas/index.d.cts +0 -1985
  98. 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>;