Skip to content

Commit 79585c1

Browse files
committed
[sync] Ws onSend and onReceive
1 parent b05f156 commit 79585c1

File tree

4 files changed

+25
-3
lines changed

4 files changed

+25
-3
lines changed

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,13 +75,23 @@
7575
* As well as providing a reference to the MergeableStore to persist, you must
7676
* provide a configured WebSocket to send synchronization messages over.
7777
*
78+
* You can indicate how long the Synchronizer will wait for responses to message
79+
* requests before timing out. A final set of optional handlers can be provided
80+
* to help debug sends, receives, and errors respectively.
81+
*
7882
* This method is asynchronous because it will await the websocket's connection
7983
* to the server. You will need to `await` a call to this function or handle the
8084
* return type natively as a Promise.
8185
* @param store The MergeableStore to synchronize.
8286
* @param webSocket The WebSocket to send synchronization messages over.
8387
* @param requestTimeoutSeconds An optional time in seconds that the
8488
* Synchronizer will wait for responses to request messages, defaulting to 1.
89+
* @param onSend An optional handler for the messages that this Synchronizer
90+
* sends. This is suitable for debugging synchronization issues in a development
91+
* environment, since v5.1.
92+
* @param onReceive An optional handler for the messages that this Synchronizer
93+
* receives. This is suitable for debugging synchronization issues in a
94+
* development environment, since v5.1.
8595
* @param onIgnoredError An optional handler for the errors that the
8696
* Synchronizer would otherwise ignore when trying to synchronize data. This is
8797
* suitable for debugging synchronization issues in a development environment.

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/// synchronizer-ws-client
22

3+
import type {Receive, Send, Synchronizer} from '../index.d.ts';
34
import type {MergeableStore} from '../../mergeable-store/index.d.ts';
4-
import type {Synchronizer} from '../index.d.ts';
55
import type {WebSocket as WsWebSocket} from 'ws';
66

77
/// WebSocketTypes
@@ -19,5 +19,7 @@ export function createWsSynchronizer<WebSocketType extends WebSocketTypes>(
1919
store: MergeableStore,
2020
webSocket: WebSocketType,
2121
requestTimeoutSeconds?: number,
22+
onSend?: Send,
23+
onReceive?: Receive,
2224
onIgnoredError?: (error: any) => void,
2325
): Promise<WsSynchronizer<WebSocketType>>;

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
/// synchronizer-ws-client
22

3+
import type {Receive, Send, Synchronizer} from '../../with-schemas/index.d.ts';
34
import type {MergeableStore} from '../../../mergeable-store/with-schemas/index.d.ts';
45
import type {OptionalSchemas} from '../../../store/with-schemas/index.d.ts';
5-
import type {Synchronizer} from '../../with-schemas/index.d.ts';
66
import type {WebSocket as WsWebSocket} from 'ws';
77

88
/// WebSocketTypes
@@ -25,5 +25,7 @@ export function createWsSynchronizer<
2525
store: MergeableStore<Schemas>,
2626
webSocket: WebSocketType,
2727
requestTimeoutSeconds?: number,
28+
onSend?: Send,
29+
onReceive?: Receive,
2830
onIgnoredError?: (error: any) => void,
2931
): Promise<WsSynchronizer<Schemas, WebSocketType>>;

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
import type {Message, Receive} from '../../@types/synchronizers/index.d.ts';
1+
import type {
2+
Message,
3+
Receive,
4+
Send,
5+
} from '../../@types/synchronizers/index.d.ts';
26
import type {
37
WebSocketTypes,
48
createWsSynchronizer as createWsSynchronizerDecl,
@@ -16,6 +20,8 @@ export const createWsSynchronizer = (async <
1620
store: MergeableStore,
1721
webSocket: WebSocketType,
1822
requestTimeoutSeconds: number = 1,
23+
onSend?: Send,
24+
onReceive?: Receive,
1925
onIgnoredError?: (error: any) => void,
2026
) => {
2127
const addEventListener = (event: string, handler: (...args: any[]) => void) =>
@@ -41,6 +47,8 @@ export const createWsSynchronizer = (async <
4147
registerReceive,
4248
destroy,
4349
requestTimeoutSeconds,
50+
onSend,
51+
onReceive,
4452
onIgnoredError,
4553
{getWebSocket: () => webSocket},
4654
);

0 commit comments

Comments
 (0)