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
+34 -31
+34 -31
View File
@@ -455,41 +455,44 @@ export interface SubscribableValue {
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>>>();
/**
* 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
* listener limit set on the connector.
* @param connector - connector of the link the subscriber was created on
* @param subscriber - subscriber to pass the events to
*/
function addConnectorSubscriber(connector: WebSocketBaseConnector, subscriber: EventSubscriber<any, any>) {
let subscribers = connectorSubscribers.get(connector);
if (!subscribers) {
const created = new Set<EventSubscriber<any, any>>();
connectorSubscribers.set(connector, created);
connector.on("connect", () => {
for (const entry of created) {
entry.handleConnectionEvent("connect");
}
});
connector.on("close", () => {
for (const entry of created) {
entry.handleConnectionEvent("close");
}
});
subscribers = created;
}
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> {
/** 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
* Uses one listener for all subscribers, so that plugins creating many subscribers do not go past the
* listener limit set on the connector.
* @param connector - connector of the link the subscriber was created on
* @param subscriber - subscriber to pass the events to
*/
private static addConnectorSubscriber(
connector: WebSocketBaseConnector,
subscriber: EventSubscriber<any, any>,
) {
let subscribers = EventSubscriber.connectorSubscribers.get(connector);
if (!subscribers) {
const created = new Set<EventSubscriber<any, any>>();
EventSubscriber.connectorSubscribers.set(connector, created);
connector.on("connect", () => {
for (const entry of created) {
entry.handleConnectionEvent("connect");
}
});
connector.on("close", () => {
for (const entry of created) {
entry.handleConnectionEvent("close");
}
});
subscribers = created;
}
subscribers.add(subscriber);
}
/** The time at which an event was last received, is less than 0 when there have been no events */
lastUpdatedMs = -1;
/** 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));
// 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
const accountControl = control as Link & { onAccountUpdate?: (handler: () => void) => void };
if (typeof accountControl.onAccountUpdate === "function") {