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,765 +0,0 @@
1
- /**
2
- * The synchronizer-ws-server module of the TinyBase project lets you create
3
- * a server that facilitates synchronization between clients.
4
- * @see Synchronization guide
5
- * @see Todo App v6 (collaboration) demo
6
- * @packageDocumentation
7
- * @module synchronizer-ws-server
8
- * @since v5.0.0
9
- */
10
- import type {WebSocketServer} from 'ws';
11
- import type {Id, IdOrNull, Ids} from '../../../common/with-schemas/index.d.cts';
12
- import type {MergeableStore} from '../../../mergeable-store/with-schemas/index.d.cts';
13
- import type {
14
- Persister,
15
- Persists,
16
- } from '../../../persisters/with-schemas/index.d.cts';
17
- import type {
18
- IdAddedOrRemoved,
19
- OptionalSchemas,
20
- } from '../../../store/with-schemas/index.d.cts';
21
-
22
- /**
23
- * The PathIdsListener type describes a function that is used to listen to
24
- * changes of active paths that a WsServer is handling.
25
- *
26
- * A WsServer listens to any path, allowing an app to have the concept of
27
- * distinct 'rooms' that only certain clients are participating in. As soon as a
28
- * single client connects to a new path, this listener will be called with the
29
- * Id of the new path and an `addedOrRemoved` value of `1`.
30
- *
31
- * When the final client disconnects from a path, it will be called again with
32
- * the Id of the deactivated path and an `addedOrRemoved` value of `-1`.
33
- *
34
- * A PathIdsListener is provided when using the addPathIdsListener method. See
35
- * that method for specific examples.
36
- * @param wsServer A reference to the WsServer.
37
- * @param pathId The Id of the path being added or removed.
38
- * @param addedOrRemoved Whether the path was added (`1`) or removed (`-1`).
39
- * @category Listener
40
- * @since v5.0.3
41
- */
42
- export type PathIdsListener = (
43
- wsServer: WsServer,
44
- pathId: Id,
45
- addedOrRemoved: IdAddedOrRemoved,
46
- ) => void;
47
-
48
- /**
49
- * The ClientIdsListener type describes a function that is used to listen to
50
- * clients joining and leaving the active paths that a WsServer is handling.
51
- *
52
- * A WsServer listens to any path, allowing an app to have the concept of
53
- * distinct 'rooms' that only certain clients are participating in. As soon as a
54
- * new client connects to a path, this listener will be called with the Id of
55
- * the path, the Id of the new client, and an `addedOrRemoved` value of `1`.
56
- *
57
- * When the client disconnects from a path, it will be called again with the Id
58
- * of the path, the Id of the leaving client, and an `addedOrRemoved` value of
59
- * `-1`.
60
- *
61
- * A ClientIdsListener is provided when using the addClientIdsListener method.
62
- * See that method for specific examples.
63
- * @param wsServer A reference to the WsServer.
64
- * @param pathId The path that the client joined or left.
65
- * @param clientId The Id of the client being added or removed.
66
- * @param addedOrRemoved Whether the client was added (`1`) or removed (`-1`).
67
- * @category Listener
68
- * @since v5.0.3
69
- */
70
- export type ClientIdsListener = (
71
- wsServer: WsServer,
72
- pathId: Id,
73
- clientId: Id,
74
- addedOrRemoved: IdAddedOrRemoved,
75
- ) => void;
76
-
77
- /**
78
- * The WsServerStats type describes the number of paths and clients that are
79
- * active on the WsServer.
80
- *
81
- * A WsServerStats object is returned from the getStats method.
82
- * @category Development
83
- * @since v5.0.0
84
- */
85
- export type WsServerStats = {
86
- /**
87
- * The number of paths currently being served by the WsServer.
88
- * @category Stat
89
- * @since v5.0.0
90
- */
91
- paths: number;
92
- /**
93
- * The number of clients currently being served by the WsServer.
94
- * @category Stat
95
- * @since v5.0.0
96
- */
97
- clients: number;
98
- };
99
-
100
- /**
101
- * The WsServer interface represents an object that facilitates synchronization
102
- * between clients that are using WsSynchronizer instances.
103
- *
104
- * You should use the createWsServer function to create a WsServer object.
105
- * @category Server
106
- * @since v5.0.0
107
- */
108
- export interface WsServer {
109
- /**
110
- * The getWebSocketServer method returns a reference to the WebSocketServer
111
- * being used for this WsServer.
112
- * @returns The WebSocketServer reference.
113
- * @example
114
- * This example creates a WsServer and then gets the WebSocketServer
115
- * reference back out again.
116
- *
117
- * ```js
118
- * import {createWsServer} from 'tinybase/synchronizers/synchronizer-ws-server';
119
- * import {WebSocketServer} from 'ws';
120
- *
121
- * const webSocketServer = new WebSocketServer({port: 8047});
122
- * const server = createWsServer(webSocketServer);
123
- *
124
- * console.log(server.getWebSocketServer() == webSocketServer);
125
- * // -> true
126
- *
127
- * server.destroy();
128
- * ```
129
- * @category Getter
130
- * @since v5.0.0
131
- */
132
- getWebSocketServer(): WebSocketServer;
133
- /**
134
- * The getPathIds method returns the active paths that the WsServer is
135
- * handling.
136
- *
137
- * These will be all the paths that have at least one active client connected
138
- * to them.
139
- * @returns An array of the paths that have clients connected to them.
140
- * @example
141
- * This example creates a WsServer, sets some clients up to connect
142
- * to it, and then enumerates the paths being used.
143
- *
144
- * ```js
145
- * import {createMergeableStore} from 'tinybase';
146
- * import {createWsSynchronizer} from 'tinybase/synchronizers/synchronizer-ws-client';
147
- * import {createWsServer} from 'tinybase/synchronizers/synchronizer-ws-server';
148
- * import {WebSocket, WebSocketServer} from 'ws';
149
- *
150
- * const server = createWsServer(new WebSocketServer({port: 8047}));
151
- * console.log(server.getPathIds());
152
- * // -> []
153
- *
154
- * const synchronizer1 = await createWsSynchronizer(
155
- * createMergeableStore(),
156
- * new WebSocket('ws://localhost:8047/roomA'),
157
- * );
158
- * const synchronizer2 = await createWsSynchronizer(
159
- * createMergeableStore(),
160
- * new WebSocket('ws://localhost:8047/roomA'),
161
- * );
162
- * const synchronizer3 = await createWsSynchronizer(
163
- * createMergeableStore(),
164
- * new WebSocket('ws://localhost:8047/roomB'),
165
- * );
166
- *
167
- * console.log(server.getPathIds());
168
- * // -> ['roomA', 'roomB']
169
- *
170
- * synchronizer3.destroy();
171
- * // ...
172
- * console.log(server.getPathIds());
173
- * // -> ['roomA']
174
- *
175
- * synchronizer1.destroy();
176
- * synchronizer2.destroy();
177
- * server.destroy();
178
- * ```
179
- * @category Getter
180
- * @since v5.0.0
181
- */
182
- getPathIds(): Ids;
183
- /**
184
- * The getClientIds method method returns the active clients that the WsServer
185
- * is handling for a given path.
186
- * @param pathId The path for which to return the list of active clients.
187
- * @returns An array of the clients connected to the given path.
188
- * @example
189
- * This example creates a WsServer, sets some clients up to connect
190
- * to it, and then gets the number of clients on the given paths. (The client
191
- * Ids themselves are unique, based on the `sec-websocket-key` header.)
192
- *
193
- * ```js
194
- * import {createMergeableStore} from 'tinybase';
195
- * import {createWsSynchronizer} from 'tinybase/synchronizers/synchronizer-ws-client';
196
- * import {createWsServer} from 'tinybase/synchronizers/synchronizer-ws-server';
197
- * import {WebSocket, WebSocketServer} from 'ws';
198
- *
199
- * const server = createWsServer(new WebSocketServer({port: 8047}));
200
- *
201
- * const synchronizer1 = await createWsSynchronizer(
202
- * createMergeableStore(),
203
- * new WebSocket('ws://localhost:8047/roomA'),
204
- * );
205
- * const synchronizer2 = await createWsSynchronizer(
206
- * createMergeableStore(),
207
- * new WebSocket('ws://localhost:8047/roomA'),
208
- * );
209
- * const synchronizer3 = await createWsSynchronizer(
210
- * createMergeableStore(),
211
- * new WebSocket('ws://localhost:8047/roomB'),
212
- * );
213
- *
214
- * console.log(server.getClientIds('roomA').length);
215
- * // -> 2
216
- * console.log(server.getClientIds('roomB').length);
217
- * // -> 1
218
- *
219
- * synchronizer3.destroy();
220
- * // ...
221
- * console.log(server.getClientIds('roomB').length);
222
- * // -> 0
223
- *
224
- * synchronizer1.destroy();
225
- * synchronizer2.destroy();
226
- * server.destroy();
227
- * ```
228
- * @category Getter
229
- * @since v5.0.0
230
- */
231
- getClientIds(pathId: Id): Ids;
232
- /**
233
- * The addPathIdsListener method registers a listener function with the
234
- * WsServer that will be called whenever there is a change in the active paths
235
- * that a WsServer is handling.
236
- *
237
- * The provided listener is a PathIdsListener function, and will be called
238
- * with a reference to the WsServer and a callback you can use to get
239
- * information about the change.
240
- * @param listener The function that will be called whenever the path Ids
241
- * handled by the WsServer change.
242
- * @returns A unique Id for the listener that can later be used to remove it.
243
- * @example
244
- * This example creates a WsServer, and listens to changes to the active paths
245
- * when clients connect to and disconnect from it.
246
- *
247
- * ```js
248
- * import {createMergeableStore} from 'tinybase';
249
- * import {createWsSynchronizer} from 'tinybase/synchronizers/synchronizer-ws-client';
250
- * import {createWsServer} from 'tinybase/synchronizers/synchronizer-ws-server';
251
- * import {WebSocket, WebSocketServer} from 'ws';
252
- *
253
- * const server = createWsServer(new WebSocketServer({port: 8047}));
254
- * const listenerId = server.addPathIdsListener(
255
- * (server, pathId, addedOrRemoved) => {
256
- * console.log(pathId + (addedOrRemoved == 1 ? ' added' : ' removed'));
257
- * console.log(server.getPathIds());
258
- * },
259
- * );
260
- *
261
- * const synchronizer1 = await createWsSynchronizer(
262
- * createMergeableStore(),
263
- * new WebSocket('ws://localhost:8047/roomA'),
264
- * );
265
- * // -> 'roomA added'
266
- * // -> ['roomA']
267
- *
268
- * const synchronizer2 = await createWsSynchronizer(
269
- * createMergeableStore(),
270
- * new WebSocket('ws://localhost:8047/roomB'),
271
- * );
272
- * // -> 'roomB added'
273
- * // -> ['roomA', 'roomB']
274
- *
275
- * synchronizer1.destroy();
276
- * // ...
277
- * // -> 'roomA removed'
278
- * // -> ['roomB']
279
- *
280
- * synchronizer2.destroy();
281
- * // ...
282
- * // -> 'roomB removed'
283
- * // -> []
284
- *
285
- * server.delListener(listenerId);
286
- * server.destroy();
287
- * ```
288
- * @category Listener
289
- * @since v5.0.0
290
- */
291
- addPathIdsListener(listener: PathIdsListener): Id;
292
- /**
293
- * The addClientIdsListener method registers a listener function with the
294
- * WsServer that will be called whenever there is a change in the clients
295
- * connected to a path that a WsServer is handling.
296
- *
297
- * The provided listener is a ClientIdsListener function, and will be called
298
- * with a reference to the WsServer, the Id of the path that the client joined
299
- * or left, and a callback you can use to get information about the change.
300
- *
301
- * You can either listen to a single path (by specifying its Id as the
302
- * method's first parameter) or changes to any path (by providing a `null`
303
- * wildcard).
304
- * @param pathId The path to listen to, or `null` as a wildcard.
305
- * @param listener The function that will be called whenever the client Ids on
306
- * a path handled by the WsServer change.
307
- * @returns A unique Id for the listener that can later be used to remove it.
308
- * @example
309
- * This example creates a WsServer, and listens to changes to the clients
310
- * connecting to and disconnecting from a specific path.
311
- *
312
- * ```js
313
- * import {createMergeableStore} from 'tinybase';
314
- * import {createWsSynchronizer} from 'tinybase/synchronizers/synchronizer-ws-client';
315
- * import {createWsServer} from 'tinybase/synchronizers/synchronizer-ws-server';
316
- * import {WebSocket, WebSocketServer} from 'ws';
317
- *
318
- * const server = createWsServer(new WebSocketServer({port: 8047}));
319
- * const listenerId = server.addClientIdsListener(
320
- * 'roomA',
321
- * (server, pathId) => {
322
- * console.log(
323
- * `${server.getClientIds(pathId).length} client(s) in roomA`,
324
- * );
325
- * },
326
- * );
327
- *
328
- * const synchronizer1 = await createWsSynchronizer(
329
- * createMergeableStore(),
330
- * new WebSocket('ws://localhost:8047/roomA'),
331
- * );
332
- * // -> '1 client(s) in roomA'
333
- *
334
- * const synchronizer2 = await createWsSynchronizer(
335
- * createMergeableStore(),
336
- * new WebSocket('ws://localhost:8047/roomB'),
337
- * );
338
- * // The listener is not called.
339
- *
340
- * synchronizer1.destroy();
341
- * // ...
342
- * // -> '0 client(s) in roomA'
343
- *
344
- * synchronizer2.destroy();
345
- *
346
- * server.delListener(listenerId);
347
- * server.destroy();
348
- * ```
349
- * @example
350
- * This example creates a WsServer, and listens to changes to the clients
351
- * connecting to and disconnecting from any path.
352
- *
353
- * ```js
354
- * import {createMergeableStore} from 'tinybase';
355
- * import {createWsSynchronizer} from 'tinybase/synchronizers/synchronizer-ws-client';
356
- * import {createWsServer} from 'tinybase/synchronizers/synchronizer-ws-server';
357
- * import {WebSocket, WebSocketServer} from 'ws';
358
- *
359
- * const server = createWsServer(new WebSocketServer({port: 8047}));
360
- * const listenerId = server.addClientIdsListener(null, (server, pathId) => {
361
- * console.log(
362
- * `${server.getClientIds(pathId).length} client(s) in ${pathId}`,
363
- * );
364
- * });
365
- *
366
- * const synchronizer1 = await createWsSynchronizer(
367
- * createMergeableStore(),
368
- * new WebSocket('ws://localhost:8047/roomA'),
369
- * );
370
- * // -> '1 client(s) in roomA'
371
- *
372
- * const synchronizer2 = await createWsSynchronizer(
373
- * createMergeableStore(),
374
- * new WebSocket('ws://localhost:8047/roomB'),
375
- * );
376
- * // -> '1 client(s) in roomB'
377
- *
378
- * synchronizer1.destroy();
379
- * // ...
380
- * // -> '0 client(s) in roomA'
381
- *
382
- * synchronizer2.destroy();
383
- * // ...
384
- * // -> '0 client(s) in roomB'
385
- *
386
- * server.delListener(listenerId);
387
- * server.destroy();
388
- * ```
389
- * @category Listener
390
- * @since v5.0.0
391
- */
392
- addClientIdsListener(pathId: IdOrNull, listener: ClientIdsListener): Id;
393
- /**
394
- * The delListener method removes a listener that was previously added to the
395
- * WsServer.
396
- *
397
- * Use the Id returned by whichever method was used to add the listener. Note
398
- * that the WsServer may re-use this Id for future listeners added to it.
399
- * @param listenerId The Id of the listener to remove.
400
- * @returns A reference to the WsServer.
401
- * @example
402
- * This example registers a listener to a WsServer and then removes it.
403
- *
404
- * ```js
405
- * import {createMergeableStore} from 'tinybase';
406
- * import {createWsSynchronizer} from 'tinybase/synchronizers/synchronizer-ws-client';
407
- * import {createWsServer} from 'tinybase/synchronizers/synchronizer-ws-server';
408
- * import {WebSocket, WebSocketServer} from 'ws';
409
- *
410
- * const server = createWsServer(new WebSocketServer({port: 8047}));
411
- * const listenerId = server.addPathIdsListener(() => {
412
- * console.log('Paths changed');
413
- * });
414
- *
415
- * const synchronizer = await createWsSynchronizer(
416
- * createMergeableStore(),
417
- * new WebSocket('ws://localhost:8047/roomA'),
418
- * );
419
- * // -> 'Paths changed'
420
- *
421
- * server.delListener(listenerId);
422
- *
423
- * synchronizer.destroy();
424
- * // -> undefined
425
- * // The listener is not called.
426
- *
427
- * server.destroy();
428
- * ```
429
- * @category Listener
430
- * @since v5.0.0
431
- */
432
- delListener(listenerId: Id): WsServer;
433
- /**
434
- * The getStats method provides a set of statistics about the WsServer, and is
435
- * used for debugging purposes.
436
- *
437
- * The WsServerStats object contains the number of paths and clients that are
438
- * active on the WsServer and is intended to be used during development.
439
- * @returns A WsServerStats object containing statistics.
440
- * @example
441
- * This example creates a WsServer that facilitates some synchronization,
442
- * demonstrating the statistics of the paths and clients handled as a result.
443
- *
444
- * ```js
445
- * import {createMergeableStore} from 'tinybase';
446
- * import {createWsSynchronizer} from 'tinybase/synchronizers/synchronizer-ws-client';
447
- * import {createWsServer} from 'tinybase/synchronizers/synchronizer-ws-server';
448
- * import {WebSocket, WebSocketServer} from 'ws';
449
- *
450
- * const server = createWsServer(new WebSocketServer({port: 8047}));
451
- *
452
- * const synchronizer1 = await createWsSynchronizer(
453
- * createMergeableStore(),
454
- * new WebSocket('ws://localhost:8047'),
455
- * );
456
- * const synchronizer2 = await createWsSynchronizer(
457
- * createMergeableStore(),
458
- * new WebSocket('ws://localhost:8047'),
459
- * );
460
- *
461
- * console.log(server.getStats());
462
- * // -> {paths: 1, clients: 2}
463
- *
464
- * synchronizer1.destroy();
465
- * synchronizer2.destroy();
466
- * server.destroy();
467
- * ```
468
- * @category Development
469
- * @since v5.0.0
470
- */
471
- getStats(): WsServerStats;
472
- /**
473
- * The destroy method provides a way to clean up the server at the end of its
474
- * use.
475
- *
476
- * This closes the underlying WebSocketServer that was provided when the
477
- * WsServer was created.
478
- * @example
479
- * This example creates a WsServer and then destroys it again, closing the
480
- * underlying WebSocketServer.
481
- *
482
- * ```js
483
- * import {createWsServer} from 'tinybase/synchronizers/synchronizer-ws-server';
484
- * import {WebSocketServer} from 'ws';
485
- *
486
- * const webSocketServer = new WebSocketServer({port: 8047});
487
- * webSocketServer.on('close', () => {
488
- * console.log('WebSocketServer closed');
489
- * });
490
- * const server = createWsServer(webSocketServer);
491
- *
492
- * server.destroy();
493
- * // ...
494
- * // -> 'WebSocketServer closed'
495
- * ```
496
- * @category Getter
497
- * @since v5.0.0
498
- */
499
- destroy(): void;
500
- }
501
-
502
- /**
503
- * The createWsServer function creates a WsServer that facilitates
504
- * synchronization between clients that are using WsSynchronizer instances.
505
- *
506
- * This has schema-based typing. The following is a simplified representation:
507
- *
508
- * ```ts override
509
- * createWsServer<
510
- * PathPersister extends Persister<
511
- * Persists.MergeableStoreOnly | Persists.StoreOrMergeableStore
512
- * >,
513
- * >(
514
- * webSocketServer: WebSocketServer,
515
- * createPersisterForPath?: (
516
- * pathId: Id,
517
- * ) =>
518
- * | PathPersister
519
- * | [PathPersister, (store: MergeableStore) => void]
520
- * | Promise<PathPersister>
521
- * | Promise<[PathPersister, (store: MergeableStore) => void]>
522
- * | undefined,
523
- * onIgnoredError?: (error: any) => void,
524
- * ): WsServer;
525
- * ```
526
- *
527
- * This should be run in a server environment, and you must pass in a configured
528
- * WebSocketServer object in order to create it.
529
- *
530
- * If you want your server to persist data itself, you can use the optional
531
- * second parameter of this function, which allows you to create a Persister for
532
- * a new path - whenever a new path is accessed by a client. This Persister will
533
- * only exist when there are active clients on that particular path. The
534
- * creation callback can be asynchronous.
535
- *
536
- * You are responsible for creating a MergeableStore to pass to this Persister,
537
- * but starting and stopping its automatic saving and loading is taken care of
538
- * by the WsServer. As a result, the server MergeableStore will be kept in sync
539
- * with the clients on that path, and in turn with whatever persistence layer
540
- * you have configured. See the example below.
541
- *
542
- * It is not safe to add or manipulate data in the MergeableStore during the
543
- * `createPersisterForPath` function, since changes will probably be overwritten
544
- * when the Persister starts. If you wish to modify data - or upgrade a schema,
545
- * for example - you can have that function instead return an array containing
546
- * the Persister _and_ a callback that takes the MergeableStore. That callback
547
- * will get called after the Persister has started, and is an appropriate place
548
- * to manipulate data in a way that will be transmitted to clients. Again, see
549
- * the example below.
550
- * @param webSocketServer A WebSocketServer object from your server environment.
551
- * @param createPersisterForPath An optional function that will create a
552
- * Persister to synchronize with the clients on a given path (or a two-item
553
- * array of Persister and callback that lets you handle data after persistence
554
- * has started).
555
- * @param onIgnoredError An optional handler for the errors that the server
556
- * would otherwise ignore when trying to sync data. This is suitable for
557
- * debugging issues in a development environment.
558
- * @returns A reference to the new WsServer object.
559
- * @example
560
- * This example creates a WsServer that synchronizes two clients on a shared
561
- * path.
562
- *
563
- * ```js
564
- * import {createMergeableStore} from 'tinybase';
565
- * import {createWsSynchronizer} from 'tinybase/synchronizers/synchronizer-ws-client';
566
- * import {createWsServer} from 'tinybase/synchronizers/synchronizer-ws-server';
567
- * import {WebSocketServer} from 'ws';
568
- *
569
- * // Server
570
- * const server = createWsServer(new WebSocketServer({port: 8047}));
571
- *
572
- * // Client 1
573
- * const clientStore1 = createMergeableStore();
574
- * clientStore1.setCell('pets', 'fido', 'species', 'dog');
575
- * const synchronizer1 = await createWsSynchronizer(
576
- * clientStore1,
577
- * new WebSocket('ws://localhost:8047/petShop'),
578
- * );
579
- * await synchronizer1.startSync();
580
- * // ...
581
- *
582
- * // Client 2
583
- * const clientStore2 = createMergeableStore();
584
- * clientStore2.setCell('pets', 'felix', 'species', 'cat');
585
- * const synchronizer2 = await createWsSynchronizer(
586
- * clientStore2,
587
- * new WebSocket('ws://localhost:8047/petShop'),
588
- * );
589
- * await synchronizer2.startSync();
590
- * // ...
591
- *
592
- * console.log(clientStore1.getTables());
593
- * // -> {pets: {fido: {species: 'dog'}, felix: {species: 'cat'}}}
594
- *
595
- * console.log(clientStore2.getTables());
596
- * // -> {pets: {fido: {species: 'dog'}, felix: {species: 'cat'}}}
597
- *
598
- * synchronizer1.destroy();
599
- * synchronizer2.destroy();
600
- * server.destroy();
601
- * ```
602
- * @example
603
- * This longer example creates a WsServer that persists a MergeableStore to file
604
- * that is synchronized with two clients on a shared path. Later, when a third
605
- * client connects, it picks up the data the previous two were using.
606
- *
607
- * ```js
608
- * import {rmSync} from 'fs';
609
- * import {createMergeableStore} from 'tinybase';
610
- * import {createFilePersister} from 'tinybase/persisters/persister-file';
611
- * import {createWsSynchronizer} from 'tinybase/synchronizers/synchronizer-ws-client';
612
- * import {createWsServer} from 'tinybase/synchronizers/synchronizer-ws-server';
613
- * import {WebSocketServer} from 'ws';
614
- *
615
- * // Server
616
- * const server = createWsServer(
617
- * new WebSocketServer({port: 8047}),
618
- * (pathId) =>
619
- * createFilePersister(
620
- * createMergeableStore(),
621
- * pathId.replace(/[^a-zA-Z0-9]/g, '-') + '.json',
622
- * ),
623
- * );
624
- *
625
- * // Client 1
626
- * const clientStore1 = createMergeableStore();
627
- * clientStore1.setCell('pets', 'fido', 'species', 'dog');
628
- * const synchronizer1 = await createWsSynchronizer(
629
- * clientStore1,
630
- * new WebSocket('ws://localhost:8047/petShop'),
631
- * );
632
- * await synchronizer1.startSync();
633
- * // ...
634
- *
635
- * // Client 2
636
- * const clientStore2 = createMergeableStore();
637
- * clientStore2.setCell('pets', 'felix', 'species', 'cat');
638
- * const synchronizer2 = await createWsSynchronizer(
639
- * clientStore2,
640
- * new WebSocket('ws://localhost:8047/petShop'),
641
- * );
642
- * await synchronizer2.startSync();
643
- * // ...
644
- *
645
- * console.log(clientStore1.getTables());
646
- * // -> {pets: {fido: {species: 'dog'}, felix: {species: 'cat'}}}
647
- *
648
- * console.log(clientStore2.getTables());
649
- * // -> {pets: {fido: {species: 'dog'}, felix: {species: 'cat'}}}
650
- *
651
- * synchronizer1.destroy();
652
- * synchronizer2.destroy();
653
- *
654
- * // ...
655
- * // Client 3 connects later
656
- * const clientStore3 = createMergeableStore();
657
- * const synchronizer3 = await createWsSynchronizer(
658
- * clientStore3,
659
- * new WebSocket('ws://localhost:8047/petShop'),
660
- * );
661
- * await synchronizer3.startSync();
662
- * // ...
663
- *
664
- * console.log(clientStore3.getTables());
665
- * // -> {pets: {fido: {species: 'dog'}, felix: {species: 'cat'}}}
666
- *
667
- * synchronizer3.destroy();
668
- * server.destroy();
669
- *
670
- * // Remove file for the purposes of this demo.
671
- * rmSync('petShop.json');
672
- * ```
673
- * @example
674
- * This example creates a WsServer that persists a MergeableStore to file that
675
- * is synchronized with two clients on a shared path, but also which updates its
676
- * data once synchronization has started.
677
- *
678
- * ```js
679
- * import {rmSync} from 'fs';
680
- * import {createMergeableStore} from 'tinybase';
681
- * import {createFilePersister} from 'tinybase/persisters/persister-file';
682
- * import {createWsSynchronizer} from 'tinybase/synchronizers/synchronizer-ws-client';
683
- * import {createWsServer} from 'tinybase/synchronizers/synchronizer-ws-server';
684
- * import {WebSocketServer} from 'ws';
685
- *
686
- * // Server
687
- * const server = createWsServer(
688
- * new WebSocketServer({port: 8047}),
689
- * (pathId) => [
690
- * createFilePersister(
691
- * createMergeableStore(),
692
- * pathId.replace(/[^a-zA-Z0-9]/g, '-') + '.json',
693
- * ),
694
- * (store) => store.setValue('pathId', pathId),
695
- * ],
696
- * );
697
- *
698
- * const clientStore = createMergeableStore();
699
- * clientStore.setCell('pets', 'fido', 'species', 'dog');
700
- * const synchronizer = await createWsSynchronizer(
701
- * clientStore,
702
- * new WebSocket('ws://localhost:8047/petShop'),
703
- * );
704
- * await synchronizer.startSync();
705
- * // ...
706
- *
707
- * console.log(clientStore.getContent());
708
- * // -> [{pets: {fido: {species: 'dog'}}}, {"pathId": "petShop"}]
709
- *
710
- * synchronizer.destroy();
711
- * server.destroy();
712
- *
713
- * // Remove file for the purposes of this demo.
714
- * rmSync('petShop.json');
715
- * ```
716
- * @example
717
- * This example creates a WsServer with a custom listener that displays
718
- * information about the address of the client that connects to it.
719
- *
720
- * ```js
721
- * import {createMergeableStore} from 'tinybase';
722
- * import {createWsSynchronizer} from 'tinybase/synchronizers/synchronizer-ws-client';
723
- * import {createWsServer} from 'tinybase/synchronizers/synchronizer-ws-server';
724
- * import {WebSocket, WebSocketServer} from 'ws';
725
- *
726
- * // On the server:
727
- * const webSocketServer = new WebSocketServer({port: 8047});
728
- * webSocketServer.on('connection', (_, request) => {
729
- * if (request.headers.connection == 'Upgrade') {
730
- * console.log('Local client connected');
731
- * }
732
- * });
733
- * const server = createWsServer(webSocketServer);
734
- *
735
- * // On a client:
736
- * const synchronizer = await createWsSynchronizer(
737
- * createMergeableStore(),
738
- * new WebSocket('ws://localhost:8047'),
739
- * );
740
- * // -> 'Local client connected'
741
- *
742
- * synchronizer.destroy();
743
- * server.destroy();
744
- * ```
745
- * @category Creation
746
- * @since v5.0.0
747
- */
748
- export function createWsServer<
749
- Schemas extends OptionalSchemas,
750
- PathPersister extends Persister<
751
- Schemas,
752
- Persists.MergeableStoreOnly | Persists.StoreOrMergeableStore
753
- >,
754
- >(
755
- webSocketServer: WebSocketServer,
756
- createPersisterForPath?: (
757
- pathId: Id,
758
- ) =>
759
- | PathPersister
760
- | [PathPersister, (store: MergeableStore<Schemas>) => void]
761
- | Promise<PathPersister>
762
- | Promise<[PathPersister, (store: MergeableStore<Schemas>) => void]>
763
- | undefined,
764
- onIgnoredError?: (error: any) => void,
765
- ): WsServer;