tinybase 6.1.0-beta.1 → 6.1.0-beta.3

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