Files
factorio-clusterio/packages/web_ui/src/util/host_pairing.ts
T
Danielv123 49a0b27d29 Address review of host pairing
Revoking a host now unassigns its instances instead of being refused
while it has any, and removes its system metrics.

Approving and revoking no longer force a save. Hosts are saved by
autosave and on exit like everything else, which also removes the
approving state from pairing requests.

The approval code and link are logged as warnings so they show up in
the systemd service, which only prints warnings and above. The pairing
limits are now the controller.host_pairing_timeout and
controller.host_pairing_max_pending config values, 0 removes the limit.

Subscribers take an optional permitted function, used for the pairing
requests instead of overrides in a subclass.
2026-10-31 16:31:45 +01:00

43 lines
1.2 KiB
TypeScript

import * as lib from "@clusterio/lib";
/**
* Subscriber for the requests from hosts waiting to be approved
*
* The requests only live in the controller's memory and it does not tell
* subscribers about requests removed while nothing was subscribed, so they
* are cleared when subscribing again after the last handler unsubscribed.
*/
export class HostPairingSubscriber extends lib.MapSubscriber<lib.HostPairingUpdatesEvent> {
private _handlers = 0;
/**
* @param control - Link to the controller.
* @param permitted - Returns true if the account may approve hosts.
*/
constructor(control: lib.Link, permitted: () => boolean) {
super(lib.HostPairingUpdatesEvent, control, undefined, permitted);
}
override subscribe(handler: lib.EventSubscriberCallback<lib.HostPairingUpdatesEvent>) {
if (this._handlers === 0) {
this.values.clear();
this.lastUpdatedMs = -1;
}
this._handlers += 1;
const unsubscribe = super.subscribe(handler);
let unsubscribed = false;
return () => {
if (unsubscribed) {
return;
}
unsubscribed = true;
unsubscribe();
// Deferred like the unsubscribe in EventSubscriber, so moving between pages
// that both subscribe keeps the requests.
setTimeout(() => {
this._handlers -= 1;
});
};
}
}