Skip to content

Commit b05f156

Browse files
committed
[sync] Custom onSend and onReceive
1 parent 3723ab9 commit b05f156

File tree

4 files changed

+26
-4
lines changed

4 files changed

+26
-4
lines changed

src/@types/synchronizers/docs.js

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,7 @@
321321
* Synchronizer, and is used for debugging purposes.
322322
*
323323
* The SynchronizerStats object contains a count of the number of times the
324-
* Persister has sent and received messages.
324+
* Synchronizer has sent and received messages.
325325
*
326326
* The method is intended to be used during development to ensure your
327327
* synchronization layer is acting as expected, for example.
@@ -370,15 +370,29 @@
370370
* must provide parameters which identify how to send and receive changes to and
371371
* from this MergeableStore and its peers. This is entirely dependent upon the
372372
* medium of communication used.
373+
*
374+
* You must also provide a callback for when the Synchronizer is destroyed
375+
* (which is a good place to clean up resources and stop communication
376+
* listeners), and indicate how long the Synchronizer will wait for responses to
377+
* message requests before timing out.
378+
*
379+
* A final set of optional handlers can be provided to help debug sends,
380+
* receives, and errors respectively.
373381
* @param store The MergeableStore to synchronize.
374382
* @param send A Send function for sending a message.
375383
* @param registerReceive A callback (called once when the Synchronizer is
376384
* created) that is passed a Receive function that you need to ensure will
377385
* receive messages addressed or broadcast to this client.
378-
* @param destroy A function called when destroying the Persister which can be
379-
* used to clean up underlying resources.
386+
* @param destroy A function called when destroying the Synchronizer which can
387+
* be used to clean up underlying resources.
380388
* @param requestTimeoutSeconds An number of seconds before a request sent from
381-
* this Persister to another peer times out.
389+
* this Synchronizer to another peer times out.
390+
* @param onSend An optional handler for the messages that this Synchronizer
391+
* sends. This is suitable for debugging synchronization issues in a development
392+
* environment, since v5.1.
393+
* @param onReceive An optional handler for the messages that this Synchronizer
394+
* receives. This is suitable for debugging synchronization issues in a
395+
* development environment, since v5.1.
382396
* @param onIgnoredError An optional handler for the errors that the
383397
* Synchronizer would otherwise ignore when trying to synchronize data. This is
384398
* suitable for debugging synchronization issues in a development environment.

src/@types/synchronizers/index.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,5 +66,7 @@ export function createCustomSynchronizer(
6666
registerReceive: (receive: Receive) => void,
6767
destroy: () => void,
6868
requestTimeoutSeconds: number,
69+
onSend?: Send,
70+
onReceive?: Receive,
6971
onIgnoredError?: (error: any) => void,
7072
): Synchronizer;

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,5 +71,7 @@ export function createCustomSynchronizer<Schemas extends OptionalSchemas>(
7171
registerReceive: (receive: Receive) => void,
7272
destroy: () => void,
7373
requestTimeoutSeconds: number,
74+
onSend?: Send,
75+
onReceive?: Receive,
7476
onIgnoredError?: (error: any) => void,
7577
): Synchronizer<Schemas>;

src/synchronizers/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ export const createCustomSynchronizer = (
4848
registerReceive: (receive: Receive) => void,
4949
destroyImpl: () => void,
5050
requestTimeoutSeconds: number,
51+
onSend?: Send,
52+
onReceive?: Receive,
5153
onIgnoredError?: (error: any) => void,
5254
// undocumented:
5355
extra: {[methodName: string]: (...args: any[]) => any} = {},
@@ -72,6 +74,7 @@ export const createCustomSynchronizer = (
7274
body: any,
7375
) => {
7476
sends++;
77+
onSend?.(toClientId, requestId, message, body);
7578
send(toClientId, requestId, message, body);
7679
};
7780

@@ -244,6 +247,7 @@ export const createCustomSynchronizer = (
244247
body: any,
245248
) => {
246249
receives++;
250+
onReceive?.(fromClientId, requestId, message, body);
247251
if (message == Message.Response) {
248252
ifNotUndefined(
249253
mapGet(pendingRequests, requestId),

0 commit comments

Comments
 (0)