mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-09 23:14:32 +00:00
37 lines
853 B
TypeScript
37 lines
853 B
TypeScript
import type { Client } from "@buape/carbon";
|
|
import { getDiscordGatewayEmitter } from "../monitor.gateway.js";
|
|
|
|
export type EarlyGatewayErrorGuard = {
|
|
pendingErrors: unknown[];
|
|
release: () => void;
|
|
};
|
|
|
|
export function attachEarlyGatewayErrorGuard(client: Client): EarlyGatewayErrorGuard {
|
|
const pendingErrors: unknown[] = [];
|
|
const gateway = client.getPlugin("gateway");
|
|
const emitter = getDiscordGatewayEmitter(gateway);
|
|
if (!emitter) {
|
|
return {
|
|
pendingErrors,
|
|
release: () => {},
|
|
};
|
|
}
|
|
|
|
let released = false;
|
|
const onGatewayError = (err: unknown) => {
|
|
pendingErrors.push(err);
|
|
};
|
|
emitter.on("error", onGatewayError);
|
|
|
|
return {
|
|
pendingErrors,
|
|
release: () => {
|
|
if (released) {
|
|
return;
|
|
}
|
|
released = true;
|
|
emitter.removeListener("error", onGatewayError);
|
|
},
|
|
};
|
|
}
|