mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-18 07:33:12 +09:00
Resend subscriptions when the account permissions change
EventSubscriber registers with the control's onAccountUpdate when it has one and resends its subscription through handleAccountEvent. A lost permission makes the request fail, a granted one syncs it again. The web Control also reports the account on connect, so resends are queued and sent once per tick. A failed subscription request now sets synced to false and notifies callbacks, before the error never reached the UI. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
8a170eb2ac
commit
2fc5a67dce
2 files changed
+117
-1
No files matched your search
@@ -473,6 +473,8 @@ export class EventSubscriber<E, S = null> {
|
||||
private _callbacks = new Array<EventSubscriberCallback<E>>();
|
||||
/** Filters applied to the filtered handler, if empty the filtered handler will not be called */
|
||||
private _filters = SubscriptionFilters.empty();
|
||||
/** True if a resend of the subscription is queued for the current tick */
|
||||
private _resendQueued = false;
|
||||
|
||||
constructor(
|
||||
protected Event: EventClass<E>,
|
||||
@@ -488,6 +490,13 @@ export class EventSubscriber<E, S = null> {
|
||||
webSocketConnector.on("close", () => {
|
||||
this.handleConnectionEvent("close");
|
||||
});
|
||||
// Only the web Control reports account changes
|
||||
const accountControl = control as Link & { onAccountUpdate?: (handler: () => void) => void };
|
||||
if (typeof accountControl.onAccountUpdate === "function") {
|
||||
accountControl.onAccountUpdate(() => {
|
||||
this.handleAccountEvent();
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -497,13 +506,38 @@ export class EventSubscriber<E, S = null> {
|
||||
*/
|
||||
handleConnectionEvent(event: "connect" | "drop" | "resume" | "close") {
|
||||
if (event === "connect" || event === "resume") {
|
||||
this._sendRequest("replace", this._callbacks.length > 0 ? SubscriptionFilters.all() : this._filters);
|
||||
this._queueResend();
|
||||
} else if (this.synced) {
|
||||
this.synced = false;
|
||||
this._notify(null);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle the permissions of the account changing
|
||||
* Resends the subscription so it fails if permission was lost, or syncs if it was granted.
|
||||
*/
|
||||
handleAccountEvent() {
|
||||
if (this._hasSubscriptions()) {
|
||||
this._queueResend();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Resend the current subscription at the end of the tick, the web Control
|
||||
* reports the account on connect so both would otherwise send it.
|
||||
*/
|
||||
private _queueResend() {
|
||||
if (this._resendQueued) {
|
||||
return;
|
||||
}
|
||||
this._resendQueued = true;
|
||||
queueMicrotask(() => {
|
||||
this._resendQueued = false;
|
||||
this._sendRequest("replace", this._callbacks.length > 0 ? SubscriptionFilters.all() : this._filters);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle incoming events and distribute it to the correct callbacks
|
||||
* @param event - event from subscribed resource
|
||||
@@ -657,6 +691,8 @@ export class EventSubscriber<E, S = null> {
|
||||
if (err instanceof Error) {
|
||||
this.error = err;
|
||||
}
|
||||
this.synced = false;
|
||||
this._notify(null);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user