From 5748f834ffcab9f4fd00fe2566b0f3b868a6cda5 Mon Sep 17 00:00:00 2001 From: Hornwitser Date: Mon, 11 Dec 2023 18:26:08 +0100 Subject: [PATCH] Only call subscription update function on subscribe Skip calling the subscription function that should provide an event replay if the client is unsubscribing from the event. This avoids having to check if the client is subscribing in every subscription function. --- packages/lib/src/subscriptions.ts | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/packages/lib/src/subscriptions.ts b/packages/lib/src/subscriptions.ts index 9dd0b33e..b2dbdb4c 100644 --- a/packages/lib/src/subscriptions.ts +++ b/packages/lib/src/subscriptions.ts @@ -119,10 +119,10 @@ export class SubscriptionController { /** * Allow clients to subscribe to an event by telling the subscription controller to accept them - * Has an optional subscription update handler which is called when any client updates their subscription + * Has an optional subscription update handler which is called when a client subscribes * @param Event - Event class which is sent out as updates. * @param subscriptionUpdate - - * Optional handler called when a client updates its subscriptions. + * Optional handler called when a client subscribes. */ handle(Event: EventClass, subscriptionUpdate?: SubscriptionRequestHandler) { const entry = Link._eventsByClass.get(Event); @@ -176,7 +176,7 @@ export class SubscriptionController { if (!eventData) { throw new Error(`Event ${event.eventName} is not a registered as subscribable`); } - const eventReplay = eventData.subscriptionUpdate ? await eventData.subscriptionUpdate(event, src, dst) : null; + let eventReplay: Event | null = null; const link: Link = this.controller.wsServer.controlConnections.get(src.id); if (event.subscribe === false) { let onceClose = eventData.subscriptions.get(link)?.onceClose; @@ -185,6 +185,9 @@ export class SubscriptionController { eventData.subscriptions.delete(link); } } else { + if (eventData.subscriptionUpdate) { + eventReplay = await eventData.subscriptionUpdate(event, src, dst); + } let onceClose = eventData.subscriptions.get(link)?.onceClose; if (!onceClose) { onceClose = () => eventData.subscriptions.delete(link);