Skip to content

Commit 0d8beeb

Browse files
Drickonsteipete
authored andcommitted
fix(hooks): use globalThis singleton for handler registry to survive bundle splitting
Without this fix, the bundler can emit multiple copies of internal-hooks into separate chunks. registerInternalHook writes to one Map instance while triggerInternalHook reads from another — resulting in hooks that silently fire with zero handlers regardless of how many were registered. Reproduce: load a hook via hooks.external.entries (loader reads one chunk), then send a message:transcribed event (get-reply imports a different chunk). The handler list is empty; the hook never runs. Fix: use globalThis.__openclaw_internal_hook_handlers__ as a shared singleton. All module copies check for and reuse the same Map, ensuring registrations are always visible to triggers.
1 parent 1e8afa1 commit 0d8beeb

1 file changed

Lines changed: 17 additions & 2 deletions

File tree

src/hooks/internal-hooks.ts

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -200,8 +200,23 @@ export interface InternalHookEvent {
200200

201201
export type InternalHookHandler = (event: InternalHookEvent) => Promise<void> | void;
202202

203-
/** Registry of hook handlers by event key */
204-
const handlers = new Map<string, InternalHookHandler[]>();
203+
/**
204+
* Registry of hook handlers by event key.
205+
*
206+
* Uses a globalThis singleton so that registerInternalHook and
207+
* triggerInternalHook always share the same Map even when the bundler
208+
* emits multiple copies of this module into separate chunks (bundle
209+
* splitting). Without the singleton, handlers registered in one chunk
210+
* are invisible to triggerInternalHook in another chunk, causing hooks
211+
* to silently fire with zero handlers.
212+
*/
213+
const _g = globalThis as typeof globalThis & {
214+
__openclaw_internal_hook_handlers__?: Map<string, InternalHookHandler[]>;
215+
};
216+
if (!_g.__openclaw_internal_hook_handlers__) {
217+
_g.__openclaw_internal_hook_handlers__ = new Map<string, InternalHookHandler[]>();
218+
}
219+
const handlers = _g.__openclaw_internal_hook_handlers__;
205220
const log = createSubsystemLogger("internal-hooks");
206221

207222
/**

0 commit comments

Comments
 (0)