Skip to content

Commit c96bf26

Browse files
committed
[ws] Adjust server listener API
1 parent a8848ce commit c96bf26

File tree

5 files changed

+61
-48
lines changed

5 files changed

+61
-48
lines changed

src/@types/synchronizers/synchronizer-ws-server/docs.js

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,19 @@
1414
*
1515
* A WsServer listens to any path, allowing an app to have the concept of
1616
* distinct 'rooms' that only certain clients are participating in. As soon as a
17-
* single client connects to a new path, this listener will be called with a
18-
* GetIdChanges callback that you can use to determine its Id.
17+
* single client connects to a new path, this listener will be called with the
18+
* Id of the new path and an `addedOrRemoved` value of `1`.
1919
*
20-
* When the final client disconnects from a path, it will be called again with a
21-
* GetIdChanges callback that you can again use to determine its Id.
20+
* When the final client disconnects from a path, it will be called again with
21+
* the Id of the deactivated path and an `addedOrRemoved` value of `-1`.
2222
*
2323
* A PathIdsListener is provided when using the addPathIdsListener method. See
2424
* that method for specific examples.
2525
* @param wsServer A reference to the WsServer.
26-
* @param getIdChanges A function that returns information about the path Id
27-
* change.
26+
* @param pathId The Id of the path being added or removed.
27+
* @param addedOrRemoved Whether the path was added (`1`) or removed (`-1`).
2828
* @category Listener
29-
* @since v5.0.0
29+
* @since v5.0.3
3030
*/
3131
/// PathIdsListener
3232
/**
@@ -35,20 +35,21 @@
3535
*
3636
* A WsServer listens to any path, allowing an app to have the concept of
3737
* distinct 'rooms' that only certain clients are participating in. As soon as a
38-
* new client connects to a path, this listener will be called with a
39-
* GetIdChanges callback that you can use to determine its Id.
38+
* new client connects to a path, this listener will be called with the Id of
39+
* the path, the Id of the new client, and an `addedOrRemoved` value of `1`.
4040
*
41-
* When the client disconnects from a path, it will be called again with a
42-
* GetIdChanges callback that you can again use to determine its Id.
41+
* When the client disconnects from a path, it will be called again with the Id
42+
* of the path, the Id of the leaving client, and an `addedOrRemoved` value of
43+
* `-1`.
4344
*
4445
* A ClientIdsListener is provided when using the addClientIdsListener method.
4546
* See that method for specific examples.
4647
* @param wsServer A reference to the WsServer.
4748
* @param pathId The path that the client joined or left.
48-
* @param getIdChanges A function that returns information about the client Id
49-
* change.
49+
* @param clientId The Id of the client being added or removed.
50+
* @param addedOrRemoved Whether the client was added (`1`) or removed (`-1`).
5051
* @category Listener
51-
* @since v5.0.0
52+
* @since v5.0.3
5253
*/
5354
/// ClientIdsListener
5455
/**
@@ -228,33 +229,35 @@
228229
* import {createWsSynchronizer} from 'tinybase/synchronizers/synchronizer-ws-client';
229230
*
230231
* const server = createWsServer(new WebSocketServer({port: 8047}));
231-
* const listenerId = server.addPathIdsListener((server, getIdChanges) => {
232-
* console.log(getIdChanges());
233-
* console.log(server.getPathIds());
234-
* });
232+
* const listenerId = server.addPathIdsListener(
233+
* (server, pathId, addedOrRemoved) => {
234+
* console.log(pathId + (addedOrRemoved == 1 ? ' added' : ' removed'));
235+
* console.log(server.getPathIds());
236+
* },
237+
* );
235238
*
236239
* const synchronizer1 = await createWsSynchronizer(
237240
* createMergeableStore(),
238241
* new WebSocket('ws://localhost:8047/roomA'),
239242
* );
240-
* // -> {'roomA': 1}
243+
* // -> 'roomA added'
241244
* // -> ['roomA']
242245
*
243246
* const synchronizer2 = await createWsSynchronizer(
244247
* createMergeableStore(),
245248
* new WebSocket('ws://localhost:8047/roomB'),
246249
* );
247-
* // -> {'roomB': 1}
250+
* // -> 'roomB added'
248251
* // -> ['roomA', 'roomB']
249252
*
250253
* synchronizer1.destroy();
251254
* // ...
252-
* // -> {'roomA': -1}
255+
* // -> 'roomA removed'
253256
* // -> ['roomB']
254257
*
255258
* synchronizer2.destroy();
256259
* // ...
257-
* // -> {'roomB': -1}
260+
* // -> 'roomB removed'
258261
* // -> []
259262
*
260263
* server.delListener(listenerId);

src/@types/synchronizers/synchronizer-ws-server/index.d.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,22 @@
11
/// synchronizer-ws-server
22

33
import type {Id, IdOrNull, Ids} from '../../common/index.d.ts';
4-
import type {GetIdChanges} from '../../store/index.d.ts';
4+
import type {IdAddedOrRemoved} from '../../store/index.d.ts';
55
import type {WebSocketServer} from 'ws';
66

77
/// PathIdsListener
8-
export type PathIdsListener<> = (
8+
export type PathIdsListener = (
99
wsServer: WsServer,
10-
getIdChanges: GetIdChanges,
10+
pathId: Id,
11+
addedOrRemoved: IdAddedOrRemoved,
1112
) => void;
1213

1314
/// ClientIdsListener
14-
export type ClientIdsListener<> = (
15+
export type ClientIdsListener = (
1516
wsServer: WsServer,
1617
pathId: Id,
17-
getIdChanges: GetIdChanges,
18+
clientId: Id,
19+
addedOrRemoved: IdAddedOrRemoved,
1820
) => void;
1921

2022
/// WsServerStats

src/@types/synchronizers/synchronizer-ws-server/with-schemas/index.d.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,22 @@
11
/// synchronizer-ws-server
22

33
import type {Id, IdOrNull, Ids} from '../../../common/with-schemas/index.d.ts';
4-
import type {GetIdChanges} from '../../../store/with-schemas/index.d.ts';
4+
import type {IdAddedOrRemoved} from '../../../store/with-schemas/index.d.ts';
55
import type {WebSocketServer} from 'ws';
66

77
/// PathIdsListener
8-
export type PathIdsListener<> = (
8+
export type PathIdsListener = (
99
wsServer: WsServer,
10-
getIdChanges: GetIdChanges<Id>,
10+
pathId: Id,
11+
addedOrRemoved: IdAddedOrRemoved,
1112
) => void;
1213

1314
/// ClientIdsListener
14-
export type ClientIdsListener<> = (
15+
export type ClientIdsListener = (
1516
wsServer: WsServer,
1617
pathId: Id,
17-
getIdChanges: GetIdChanges<Id>,
18+
clientId: Id,
19+
addedOrRemoved: IdAddedOrRemoved,
1820
) => void;
1921

2022
/// WsServerStats

src/synchronizers/synchronizer-ws-server/index.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,9 @@ export const createWsServer = ((webSocketServer: WebSocketServer) => {
4848
mapSet(clients, clientId, webSocket);
4949

5050
if (clients.size == 1) {
51-
callListeners(pathIdListeners, undefined, () => ({[pathId]: 1}));
51+
callListeners(pathIdListeners, undefined, pathId, 1);
5252
}
53-
callListeners(clientIdListeners, [pathId], () => ({[clientId]: 1}));
53+
callListeners(clientIdListeners, [pathId], clientId, 1);
5454

5555
webSocket.on('message', (data) => {
5656
const payload = data.toString(UTF8);
@@ -74,10 +74,10 @@ export const createWsServer = ((webSocketServer: WebSocketServer) => {
7474

7575
webSocket.on('close', () => {
7676
collDel(clients, clientId);
77-
callListeners(clientIdListeners, [pathId], () => ({[clientId]: -1}));
77+
callListeners(clientIdListeners, [pathId], clientId, -1);
7878
if (collIsEmpty(clients)) {
7979
collDel(clientsByPath, pathId);
80-
callListeners(pathIdListeners, undefined, () => ({[pathId]: -1}));
80+
callListeners(pathIdListeners, undefined, pathId, -1);
8181
}
8282
});
8383
}),

test/unit/other/synchronizer-ws-server.test.ts

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -89,32 +89,38 @@ describe('Multiple connections', () => {
8989

9090
test('Listeners', async () => {
9191
const pathIdsLog: {[pathId: string]: -1 | 1}[] = [];
92-
wsServer.addPathIdsListener((server, getIdChanges) => {
92+
wsServer.addPathIdsListener((server, pathId, addedOrRemoved) => {
9393
expect(server).toEqual(wsServer);
94-
pathIdsLog.push(getIdChanges());
94+
pathIdsLog.push({[pathId]: addedOrRemoved});
9595
});
9696

9797
const allClientIdsLog: {[pathId: string]: {[clientId: string]: -1 | 1}}[] =
9898
[];
99-
wsServer.addClientIdsListener(null, (server, pathId, getIdChanges) => {
100-
expect(server).toEqual(wsServer);
101-
allClientIdsLog.push({[pathId]: getIdChanges()});
102-
});
99+
wsServer.addClientIdsListener(
100+
null,
101+
(server, pathId, clientId, addedOrRemoved) => {
102+
expect(server).toEqual(wsServer);
103+
allClientIdsLog.push({[pathId]: {[clientId]: addedOrRemoved}});
104+
},
105+
);
103106

104107
const clientIdsLog1: {[pathId: string]: {[clientId: string]: -1 | 1}}[] =
105108
[];
106-
wsServer.addClientIdsListener('p1', (server, pathId, getIdChanges) => {
107-
expect(server).toEqual(wsServer);
108-
clientIdsLog1.push({[pathId]: getIdChanges()});
109-
});
109+
wsServer.addClientIdsListener(
110+
'p1',
111+
(server, pathId, clientId, addedOrRemoved) => {
112+
expect(server).toEqual(wsServer);
113+
clientIdsLog1.push({[pathId]: {[clientId]: addedOrRemoved}});
114+
},
115+
);
110116

111117
const clientIdsLog2: {[pathId: string]: {[clientId: string]: -1 | 1}}[] =
112118
[];
113119
const listenerId = wsServer.addClientIdsListener(
114120
'p2',
115-
(server, pathId, getIdChanges) => {
121+
(server, pathId, clientId, addedOrRemoved) => {
116122
expect(server).toEqual(wsServer);
117-
clientIdsLog2.push({[pathId]: getIdChanges()});
123+
clientIdsLog2.push({[pathId]: {[clientId]: addedOrRemoved}});
118124
},
119125
);
120126

0 commit comments

Comments
 (0)