@flowlab/event
Version:
FlowLab event-driven system
190 lines (176 loc) • 4.91 kB
JavaScript
;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// src/index.ts
var index_exports = {};
__export(index_exports, {
EventBus: () => EventBus,
bindEvent: () => bindEvent,
emitEvent: () => emitEvent,
getCurrentEventBus: () => getCurrentEventBus,
isEventBound: () => isEventBound,
listBoundEvents: () => listBoundEvents,
onAnyEvent: () => onAnyEvent,
registerWorkflow: () => registerWorkflow,
startEventListener: () => startEventListener,
stopEventListener: () => stopEventListener,
unbindEvent: () => unbindEvent,
useEventBus: () => useEventBus
});
module.exports = __toCommonJS(index_exports);
// src/api/emitEvents.ts
async function emitEvent(event, payload, bus) {
await bus.emit(event, payload);
}
// src/bus/EventBus.ts
var EventBus = class {
handlers = /* @__PURE__ */ new Map();
/**
* 触发事件
* @param eventName - 事件名称
* @param payload - 事件数据
*/
async emit(eventName, payload) {
const handlers = this.handlers.get(eventName);
if (handlers) {
for (const handler of handlers) {
try {
await handler(payload);
} catch (err) {
console.error(`[FlowLab EventBus] Error handling event ${eventName}`, err);
}
}
}
}
/**
* 绑定事件处理器
* @param eventName - 事件名称
* @param handler - 事件处理函数
*/
on(eventName, handler) {
if (!this.handlers.has(eventName)) {
this.handlers.set(eventName, /* @__PURE__ */ new Set());
}
this.handlers.get(eventName)?.add(handler);
}
/**
* 解绑事件处理器
* @param eventName - 事件名称
* @param handler - 事件处理函数
*/
off(eventName, handler) {
this.handlers.get(eventName)?.delete(handler);
}
/**
* 启动事件监听(由子类实现)
*/
// abstract start(): Promise<void>;
start() {
return Promise.resolve();
}
/**
* 停止事件监听(由子类实现)
*/
// abstract stop(): Promise<void>;
stop() {
return Promise.resolve();
}
};
// src/bus/InMemoryBus.ts
var InMemoryBus = class extends EventBus {
running = false;
async start() {
this.running = true;
console.log("[FlowLab] \u2705 InMemory EventBus started successfully");
}
async stop() {
this.running = false;
console.log("[FlowLab] \u{1F6D1} InMemory EventBus stopped");
}
isRunning() {
return this.running;
}
};
// src/internal/context.ts
var currentEventBus = new InMemoryBus();
function getCurrentEventBus() {
return currentEventBus;
}
function useEventBus(bus) {
currentEventBus = bus;
}
// src/api/bindEvent.ts
function bindEvent(eventName, handler) {
getCurrentEventBus().on(eventName, handler);
}
// src/api/unbindEvent.ts
function unbindEvent(eventName, handler) {
if (handler) {
getCurrentEventBus().off(eventName, handler);
}
}
// src/api/registerWorkflow.ts
function registerWorkflow(eventName, workflow) {
bindEvent(eventName, async (payload) => {
await workflow.run(payload);
});
}
// src/api/startEventListener.ts
async function startEventListener() {
await getCurrentEventBus().start();
}
// src/api/stopEventListener.ts
async function stopEventListener() {
await getCurrentEventBus().stop();
}
// src/api/listBoundEvents.ts
function listBoundEvents() {
const bus = getCurrentEventBus();
return bus.handlers ? Array.from(bus.handlers.keys()) : [];
}
// src/api/isEventBound.ts
function isEventBound(eventName) {
const bus = getCurrentEventBus();
return bus.handlers?.has(eventName) ?? false;
}
// src/api/onAnyEvent.ts
function onAnyEvent(callback) {
const bus = getCurrentEventBus();
const originalEmit = bus.emit.bind(bus);
bus.emit = async (eventName, payload) => {
callback(eventName, payload);
return originalEmit(eventName, payload);
};
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
EventBus,
bindEvent,
emitEvent,
getCurrentEventBus,
isEventBound,
listBoundEvents,
onAnyEvent,
registerWorkflow,
startEventListener,
stopEventListener,
unbindEvent,
useEventBus
});
//# sourceMappingURL=index.js.map