mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-13 15:14:52 +09:00
Keep the connector subscribers as a static on EventSubscriber
This commit is contained in:
1 file changed
+34
-31
@@ -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") {
|
||||
|
||||
Reference in new issue
Block a user