tinybase 6.1.0-beta.0 → 6.1.0-beta.2

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