@@ -404,14 +404,49 @@ const ASYNC_DIAGNOSTIC_EVENT_TYPES = new Set<DiagnosticEventPayload["type"]>([
|
404 | 404 | "log.record", |
405 | 405 | ]); |
406 | 406 | |
407 | | -const diagnosticEventsState: DiagnosticEventsGlobalState = { |
408 | | -enabled: true, |
409 | | -seq: 0, |
410 | | -listeners: new Set<DiagnosticEventListener>(), |
411 | | -dispatchDepth: 0, |
412 | | -asyncQueue: [], |
413 | | -asyncDrainScheduled: false, |
414 | | -}; |
| 407 | +const DIAGNOSTIC_EVENTS_STATE_KEY = Symbol.for("openclaw.diagnosticEvents.state.v1"); |
| 408 | + |
| 409 | +function createDiagnosticEventsState(): DiagnosticEventsGlobalState { |
| 410 | +return { |
| 411 | +enabled: true, |
| 412 | +seq: 0, |
| 413 | +listeners: new Set<DiagnosticEventListener>(), |
| 414 | +dispatchDepth: 0, |
| 415 | +asyncQueue: [], |
| 416 | +asyncDrainScheduled: false, |
| 417 | +}; |
| 418 | +} |
| 419 | + |
| 420 | +function isDiagnosticEventsState(value: unknown): value is DiagnosticEventsGlobalState { |
| 421 | +if (!value || typeof value !== "object") { |
| 422 | +return false; |
| 423 | +} |
| 424 | +const candidate = value as Partial<DiagnosticEventsGlobalState>; |
| 425 | +return ( |
| 426 | +typeof candidate.enabled === "boolean" && |
| 427 | +typeof candidate.seq === "number" && |
| 428 | +candidate.listeners instanceof Set && |
| 429 | +typeof candidate.dispatchDepth === "number" && |
| 430 | +Array.isArray(candidate.asyncQueue) && |
| 431 | +typeof candidate.asyncDrainScheduled === "boolean" |
| 432 | +); |
| 433 | +} |
| 434 | + |
| 435 | +const diagnosticEventsState: DiagnosticEventsGlobalState = (() => { |
| 436 | +const globalStore = globalThis as Record<PropertyKey, unknown>; |
| 437 | +const existing = globalStore[DIAGNOSTIC_EVENTS_STATE_KEY]; |
| 438 | +if (isDiagnosticEventsState(existing)) { |
| 439 | +return existing; |
| 440 | +} |
| 441 | +const created = createDiagnosticEventsState(); |
| 442 | +Object.defineProperty(globalStore, DIAGNOSTIC_EVENTS_STATE_KEY, { |
| 443 | +configurable: true, |
| 444 | +enumerable: false, |
| 445 | +value: created, |
| 446 | +writable: false, |
| 447 | +}); |
| 448 | +return created; |
| 449 | +})(); |
415 | 450 | |
416 | 451 | function getDiagnosticEventsState(): DiagnosticEventsGlobalState { |
417 | 452 | return diagnosticEventsState; |
|