diff --git a/packages/lib/browser.js b/packages/lib/browser.js index 1f2d0428..ad4ccaf1 100644 --- a/packages/lib/browser.js +++ b/packages/lib/browser.js @@ -9,6 +9,7 @@ export * from "./dist/src/logging"; export * from "./dist/src/plugin"; export * from "./dist/src/schema"; export * from "./dist/src/users"; +export * from "./dist/src/subscriptions"; export { default as ExponentialBackoff } from "./dist/src/ExponentialBackoff"; export { default as PlayerStats } from "./dist/src/PlayerStats"; diff --git a/packages/lib/src/subscriptions.ts b/packages/lib/src/subscriptions.ts index 7e6ef1d0..082a5a67 100644 --- a/packages/lib/src/subscriptions.ts +++ b/packages/lib/src/subscriptions.ts @@ -118,7 +118,7 @@ export class SubscriptionRequest { export class SubscriptionController { _events = new Map, - subscriptions: Map }>, + subscriptions: Map, onceClose: () => void }>, }>(); constructor( @@ -186,14 +186,18 @@ export class SubscriptionController { const eventReplay = eventData.subscriptionUpdate ? await eventData.subscriptionUpdate(event, src, dst) : null; const link: Link = this.controller.wsServer.controlConnections.get(src.id); if (event.allChannels === false && event.channels.length === 0) { - eventData.subscriptions.delete(link); - } else { - if (!eventData.subscriptions.has(link)) { - link.connector.once("close", () => { - eventData.subscriptions.delete(link); - }) + let onceClose = eventData.subscriptions.get(link)?.onceClose; + if (onceClose) { + link.connector.off("close", onceClose); + eventData.subscriptions.delete(link); } - eventData.subscriptions.set(link, { all: event.allChannels, channels: event.channels }); + } else { + let onceClose = eventData.subscriptions.get(link)?.onceClose; + if (!onceClose) { + onceClose = () => eventData.subscriptions.delete(link); + link.connector.once("close", onceClose); + } + eventData.subscriptions.set(link, { all: event.allChannels, channels: event.channels, onceClose: onceClose }); } return new SubscriptionResponse(eventReplay); } @@ -249,51 +253,51 @@ export class EventSubscriber { /** * If a control link was not connected at creation, it can be connected here (normally during onControllerConnectionEvent) */ - connectControl(control: Link) { - if (this.control === control) return Promise.resolve(); + async connectControl(control: Link) { + if (this.control === control) return; this.control = control; this.control.handle(this.event, this._handle.bind(this)); - return this._updateSubscription(); + await this._updateSubscription(); } /** * Subscribe to receive all event notifications */ - subscribe(handler: EventSubscriberCallback) { + async subscribe(handler: EventSubscriberCallback) { this._callbacks.push(handler); - return this._updateSubscription(); + await this._updateSubscription(); } /** * Subscribe to receive event notifications for the given channel */ - subscribeToChannel(channel: string | number, handler: EventSubscriberCallback) { + async subscribeToChannel(channel: string | number, handler: EventSubscriberCallback) { if (!this._channelCallbacks.get(channel)) { this._channelCallbacks.set(channel, []); } this._channelCallbacks.get(channel)!.push(handler); - return this._updateSubscription(); + await this._updateSubscription(); } /** * Unsubscribe from receiving all event notifications, does not effect the subscriptions of other handlers * Will throw an error if your handler is not subscribed to all channels, does not accept anonymous functions */ - unsubscribe(handler: EventSubscriberCallback) { + async unsubscribe(handler: EventSubscriberCallback) { let index = this._callbacks.lastIndexOf(handler); if (index === -1) { throw new Error("handler is not registered"); } this._callbacks.splice(index, 1); - return this._updateSubscription(); + await this._updateSubscription(); } /** * Unsubscribe from receiving event notifications for a given channel, does not effect the subscriptions of other handlers * Will throw an error if your handler is not subscribed to the channel, does not accept anonymous functions */ - unsubscribeFromChannel(channel: string | number, handler: EventSubscriberCallback) { + async unsubscribeFromChannel(channel: string | number, handler: EventSubscriberCallback) { const channelCallbacks = this._channelCallbacks.get(channel); if (!channelCallbacks) { throw new Error("handler is not registered"); @@ -309,7 +313,7 @@ export class EventSubscriber { } else { channelCallbacks.splice(index, 1); } - return this._updateSubscription(); + await this._updateSubscription(); } /** diff --git a/packages/web_ui/src/util/websocket.ts b/packages/web_ui/src/util/websocket.ts index f503e6e0..31368b8b 100644 --- a/packages/web_ui/src/util/websocket.ts +++ b/packages/web_ui/src/util/websocket.ts @@ -59,9 +59,9 @@ export class Control extends lib.Link { hostUpdate = new lib.EventSubscriber(lib.HostUpdateEvent, event => event.update); instanceUpdate = new lib.EventSubscriber(lib.InstanceDetailsUpdateEvent, event => event.details); saveListUpdate = new lib.EventSubscriber(lib.InstanceSaveListUpdateEvent); - modPackUpdate = new lib.EventSubscriber(lib.InstanceSaveListUpdateEvent, event => event.modPack); - modUpdate = new lib.EventSubscriber(lib.InstanceSaveListUpdateEvent, event => event.mod); - userUpdate = new lib.EventSubscriber(lib.InstanceSaveListUpdateEvent, event => event.user); + modPackUpdate = new lib.EventSubscriber(lib.ModPackUpdateEvent, event => event.modPack); + modUpdate = new lib.EventSubscriber(lib.ModUpdateEvent, event => event.mod); + userUpdate = new lib.EventSubscriber(lib.UserUpdateEvent, event => event.user); declare connector: ControlConnector;