mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-25 00:23:12 +09:00
Keep the connector subscribers as a static on EventSubscriber
This commit is contained in:
1 file changed
+13
-10
@@ -455,8 +455,13 @@ export interface SubscribableValue {
|
|||||||
isDeleted: boolean,
|
isDeleted: boolean,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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 */
|
/** Subscribers created on each connector, they share one connect and one close listener on it */
|
||||||
const connectorSubscribers = new WeakMap<object, Set<EventSubscriber<any, any>>>();
|
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
|
||||||
@@ -465,11 +470,14 @@ const connectorSubscribers = new WeakMap<object, Set<EventSubscriber<any, any>>>
|
|||||||
* @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");
|
||||||
@@ -485,11 +493,6 @@ function addConnectorSubscriber(connector: WebSocketBaseConnector, subscriber: E
|
|||||||
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") {
|
||||||
|
|||||||
Reference in new issue
Block a user