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.
This commit is contained in:
Hornwitser committed 2023-12-11 20:21:43 +01:00
1 parent a2914bda33
commit 5748f834ff
1 file changed
+6 -3
+6 -3
View File
@@ -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<T>(Event: EventClass<T>, subscriptionUpdate?: SubscriptionRequestHandler<T>) {
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<unknown> | 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);