refactor(onebot): lift the network adapter reload state machine into the base class
The four NetworkAdapters each carried a byte-identical reload() state machine (sigChanged/wasEnabled -> Reopened/Closed/Opened/Normal). Lift it into IOneBotNetworkAdapter as a template method; per-adapter variation is now expressed via three hooks: bindingSignature(), willEnable() (default enabled!==false; clients add && !!url), and onConfigReplaced() (post-swap side effects). Also delete four redundant 'override get isActive()' (identical to the base getter), lift the duplicated uin-logger setup into the base ctor, and drop http-post's now-redundant signature_ cache. NetworkReloadType is a test-observability seam -- the sole production caller (instance.applyConfigDiff) discards it -- so a new base-level test exercises all transitions directly. Behaviour-preserving: +94/-157 in src, full workspace typecheck clean, 332 onebot tests pass.
This commit is contained in:
7 files changed
+324
-158
No files matched your search
@@ -1,3 +1,4 @@
|
||||
import { createLogger, type Logger } from '@snowluma/common/logger';
|
||||
import type { ApiHandler } from '../api-handler';
|
||||
import type { DispatchPayload } from '../event-filter';
|
||||
import type { JsonObject, NetworkBase } from '../types';
|
||||
@@ -33,12 +34,16 @@ export abstract class IOneBotNetworkAdapter<C extends NetworkBase> {
|
||||
readonly name: string;
|
||||
protected config: C;
|
||||
protected readonly ctx: NetworkAdapterContext;
|
||||
protected readonly log: Logger;
|
||||
protected isEnabled = false;
|
||||
|
||||
constructor(name: string, config: C, ctx: NetworkAdapterContext) {
|
||||
constructor(name: string, config: C, ctx: NetworkAdapterContext, moduleLog?: Logger) {
|
||||
this.name = name;
|
||||
this.config = structuredClone(config);
|
||||
this.ctx = ctx;
|
||||
const base = moduleLog ?? createLogger('OneBot.Network');
|
||||
const uinNum = Number.parseInt(ctx.uin, 10);
|
||||
this.log = Number.isFinite(uinNum) && uinNum > 0 ? base.child({ uin: uinNum }) : base;
|
||||
}
|
||||
|
||||
get isActive(): boolean { return this.isEnabled; }
|
||||
@@ -47,10 +52,63 @@ export abstract class IOneBotNetworkAdapter<C extends NetworkBase> {
|
||||
|
||||
abstract open(): void | Promise<void>;
|
||||
abstract close(): void | Promise<void>;
|
||||
abstract reload(config: C): NetworkReloadType | Promise<NetworkReloadType>;
|
||||
|
||||
abstract onEvent(event: JsonObject, payload: DispatchPayload): void | Promise<void>;
|
||||
|
||||
/** Report live connection health for the WebUI dashboard. */
|
||||
abstract describeStatus(): AdapterStatus;
|
||||
|
||||
// ── reload: shared hot-reload state machine (template method) ──────────
|
||||
//
|
||||
// All four adapters reconcile a live config swap identically: decide
|
||||
// whether the binding changed, whether the adapter should be enabled, and
|
||||
// then open/close to reach the target state. The skeleton lives here; the
|
||||
// three things that genuinely differ per adapter are the hooks below.
|
||||
//
|
||||
// NOTE: the returned `NetworkReloadType` is a *test-observability* seam.
|
||||
// The sole production caller (`instance.applyConfigDiff`) discards it —
|
||||
// it exists so tests can assert "which transition did this config cause"
|
||||
// without poking private state. Behaviour, not the label, is the contract.
|
||||
|
||||
/** Stable string identity of the bound resource (host:port / url / token …).
|
||||
* A change here means the live binding must be torn down and re-opened. */
|
||||
protected abstract bindingSignature(config: C): string;
|
||||
|
||||
/** Whether `config` should result in an enabled adapter. Defaults to the
|
||||
* `enabled` flag; client adapters also require a target `url`. */
|
||||
protected willEnable(config: C): boolean {
|
||||
return config.enabled !== false;
|
||||
}
|
||||
|
||||
/** Refresh derived state after `this.config` has been replaced (report
|
||||
* options, role, propagation to live connections). Default: nothing. */
|
||||
protected onConfigReplaced(_next: C): void { /* no-op */ }
|
||||
|
||||
async reload(next: C): Promise<NetworkReloadType> {
|
||||
const prevSig = this.bindingSignature(this.config);
|
||||
const wasEnabled = this.isEnabled;
|
||||
const willEnable = this.willEnable(next);
|
||||
|
||||
this.config = structuredClone(next);
|
||||
this.onConfigReplaced(next);
|
||||
|
||||
const sigChanged = prevSig !== this.bindingSignature(next);
|
||||
if (sigChanged && wasEnabled) {
|
||||
await this.close();
|
||||
if (willEnable) {
|
||||
await this.open();
|
||||
return NetworkReloadType.Reopened;
|
||||
}
|
||||
return NetworkReloadType.Closed;
|
||||
}
|
||||
if (!wasEnabled && willEnable) {
|
||||
await this.open();
|
||||
return NetworkReloadType.Opened;
|
||||
}
|
||||
if (wasEnabled && !willEnable) {
|
||||
await this.close();
|
||||
return NetworkReloadType.Closed;
|
||||
}
|
||||
return NetworkReloadType.Normal;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user