Keep the connector subscribers as a static on EventSubscriber

This commit is contained in:
Danielv123 committed 2026-11-11 11:22:08 +01:00
1 parent 4d6b9b52e3
commit f86ebb6e66
1 file changed
+16 -13
+16 -13
View File
@@ -455,21 +455,29 @@ export interface SubscribableValue {
isDeleted: boolean, isDeleted: boolean,
} }
/** Subscribers created on each connector, they share one connect and one close listener on it */
const connectorSubscribers = new WeakMap<object, Set<EventSubscriber<any, any>>>();
/** /**
* Component for subscribing to and tracking updates of a remote resource
* Multiple handlers can be subscribed at the same time
*/
export class EventSubscriber<E, S = null> {
/** Subscribers created on each connector, they share one connect and one close listener on it */
private static connectorSubscribers = new WeakMap<object, Set<EventSubscriber<any, any>>>();
/**
* Pass connect and close events of a connector on to a subscriber * Pass connect and close events of a connector on to a subscriber
* Uses one listener for all subscribers, so that plugins creating many subscribers do not go past the * Uses one listener for all subscribers, so that plugins creating many subscribers do not go past the
* listener limit set on the connector. * listener limit set on the connector.
* @param connector - connector of the link the subscriber was created on * @param connector - connector of the link the subscriber was created on
* @param subscriber - subscriber to pass the events to * @param subscriber - subscriber to pass the events to
*/ */
function addConnectorSubscriber(connector: WebSocketBaseConnector, subscriber: EventSubscriber<any, any>) { private static addConnectorSubscriber(
let subscribers = connectorSubscribers.get(connector); connector: WebSocketBaseConnector,
subscriber: EventSubscriber<any, any>,
) {
let subscribers = EventSubscriber.connectorSubscribers.get(connector);
if (!subscribers) { if (!subscribers) {
const created = new Set<EventSubscriber<any, any>>(); const created = new Set<EventSubscriber<any, any>>();
connectorSubscribers.set(connector, created); EventSubscriber.connectorSubscribers.set(connector, created);
connector.on("connect", () => { connector.on("connect", () => {
for (const entry of created) { for (const entry of created) {
entry.handleConnectionEvent("connect"); entry.handleConnectionEvent("connect");
@@ -483,13 +491,8 @@ function addConnectorSubscriber(connector: WebSocketBaseConnector, subscriber: E
subscribers = created; subscribers = created;
} }
subscribers.add(subscriber); subscribers.add(subscriber);
} }
/**
* Component for subscribing to and tracking updates of a remote resource
* Multiple handlers can be subscribed at the same time
*/
export class EventSubscriber<E, S = null> {
/** The time at which an event was last received, is less than 0 when there have been no events */ /** The time at which an event was last received, is less than 0 when there have been no events */
lastUpdatedMs = -1; lastUpdatedMs = -1;
/** True if this subscriber is currently synced with the source */ /** True if this subscriber is currently synced with the source */
@@ -513,7 +516,7 @@ export class EventSubscriber<E, S = null> {
) { ) {
control.handle(Event, this._handleEvent.bind(this)); control.handle(Event, this._handleEvent.bind(this));
// The events below exist only on websocket connectors, we can't early return because of test mocking // The events below exist only on websocket connectors, we can't early return because of test mocking
addConnectorSubscriber(control.connector as WebSocketBaseConnector, this); EventSubscriber.addConnectorSubscriber(control.connector as WebSocketBaseConnector, this);
// Only the web Control reports account changes // Only the web Control reports account changes
const accountControl = control as Link & { onAccountUpdate?: (handler: () => void) => void }; const accountControl = control as Link & { onAccountUpdate?: (handler: () => void) => void };
if (typeof accountControl.onAccountUpdate === "function") { if (typeof accountControl.onAccountUpdate === "function") {