UNPKG

@mastra/core

Version:
122 lines (121 loc) 3.35 kB
//#region src/hooks/mitt.ts /** * Mitt: Tiny (~200b) functional event emitter / pubsub. * @name mitt * @returns {Mitt} */ function mitt(all) { all = all || /* @__PURE__ */ new Map(); return { /** * A Map of event names to registered handler functions. */ all, /** * Register an event handler for the given type. * @param {string|symbol} type Type of event to listen for, or `'*'` for all events * @param {Function} handler Function to call in response to given event * @memberOf mitt */ on(type, handler) { const handlers = all.get(type); if (handlers) handlers.push(handler); else all.set(type, [handler]); }, /** * Remove an event handler for the given type. * If `handler` is omitted, all handlers of the given type are removed. * @param {string|symbol} type Type of event to unregister `handler` from (`'*'` to remove a wildcard handler) * @param {Function} [handler] Handler function to remove * @memberOf mitt */ off(type, handler) { const handlers = all.get(type); if (handlers) if (handler) handlers.splice(handlers.indexOf(handler) >>> 0, 1); else all.set(type, []); }, /** * Invoke all handlers for the given type. * If present, `'*'` handlers are invoked after type-matched handlers. * * Note: Manually firing '*' handlers is not supported. * * @param {string|symbol} type The event type to invoke * @param {Any} [evt] Any value (object is recommended and powerful), passed to each handler * @memberOf mitt */ emit(type, evt) { let handlers = all.get(type); if (handlers) handlers.slice().map((handler) => { handler(evt); }); handlers = all.get("*"); if (handlers) handlers.slice().map((handler) => { handler(type, evt); }); } }; } //#endregion //#region src/hooks/index.ts let AvailableHooks = /* @__PURE__ */ function(AvailableHooks) { AvailableHooks["ON_EVALUATION"] = "onEvaluation"; AvailableHooks["ON_GENERATION"] = "onGeneration"; AvailableHooks["ON_SCORER_RUN"] = "onScorerRun"; return AvailableHooks; }({}); const hooks = mitt(); function registerHook(hook, action) { hooks.on(hook, action); } function deregisterHook(hook, action) { hooks.off(hook, action); } function executeHook(hook, data) { setImmediate(() => { hooks.emit(hook, data); }); } /** * Number of handlers currently registered for a hook on the module-level * emitter. The emitter never drops handlers on its own, so leak regression * tests use this to assert that short-lived instances (e.g. a standalone * Agent's ephemeral Mastra) don't accumulate handlers (#19404). * * @internal test-only */ function __hookHandlerCount(hook) { return hooks.all.get(hook)?.length ?? 0; } //#endregion Object.defineProperty(exports, "AvailableHooks", { enumerable: true, get: function() { return AvailableHooks; } }); Object.defineProperty(exports, "__hookHandlerCount", { enumerable: true, get: function() { return __hookHandlerCount; } }); Object.defineProperty(exports, "deregisterHook", { enumerable: true, get: function() { return deregisterHook; } }); Object.defineProperty(exports, "executeHook", { enumerable: true, get: function() { return executeHook; } }); Object.defineProperty(exports, "registerHook", { enumerable: true, get: function() { return registerHook; } }); //# sourceMappingURL=hooks-JAto1d0E.cjs.map