UNPKG

@pisell/pisellos

Version:

一个可扩展的前端模块化SDK框架,支持插件系统

218 lines (216 loc) 5.9 kB
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/utils/watch.ts var watch_exports = {}; __export(watch_exports, { default: () => watch_default }); module.exports = __toCommonJS(watch_exports); var Watch = class { constructor() { // 监听列表 this.events = /* @__PURE__ */ new Map(); // 启用列表 - 使用数组保持顺序 this.enableListMap = /* @__PURE__ */ new Map(); } /** * 订阅事件 * @param event 事件名称 * @param callback 回调函数 * @returns 订阅对象,可用于取消订阅 */ subscribe(watchKey, event) { if (!this.events.has(watchKey)) { this.events.set(watchKey, /* @__PURE__ */ new Map()); } const callbacks = this.events.get(watchKey); callbacks.set(event.key, event.callback); this.enableEvent(watchKey, event.key); return { // 取消订阅 unsubscribe: () => { this.unsubscribe(watchKey, event); } }; } /** * 启用事件 * @param watchKey 监听的key * @param event 事件名称 */ enableEvent(watchKey, eventKey) { const enableList = this.enableListMap.get(watchKey); if (enableList) { if (enableList.includes(eventKey)) { return; } enableList.push(eventKey); } else { this.enableListMap.set(watchKey, [eventKey]); } } /** * 禁用某个事件 * @param watchKey 监听的key * @param eventKey 事件名称 */ disableEvent(watchKey, eventKey) { const enableList = this.enableListMap.get(watchKey); if (enableList) { const index = enableList.indexOf(eventKey); if (index !== -1) { enableList.splice(index); } } } /** * 冻结某个监听下的所有事件 * @param watchKey 监听的key */ disabledAllEventByWatchKey(watchKey) { this.enableListMap.set(watchKey, []); } /** * 启用某个监听下的所有事件 * @param watchKey 监听的key */ enableAllEventByWatchKey(watchKey) { var _a; this.enableListMap.set( watchKey, Array.from(((_a = this.events.get(watchKey)) == null ? void 0 : _a.keys()) || []) ); } /** * 取消订阅事件 * @param event 事件名称 * @param callback 回调函数 * @returns 是否成功取消订阅 */ unsubscribe(watchKey, event) { const callbacks = this.events.get(watchKey); if (!callbacks) { return; } this.disableEvent(watchKey, event.key); callbacks.delete(event.key); return; } /** * 发布事件 * @param event 事件名称 * @param args 事件参数 */ publish(watchKey, ...args) { var _a; const callbacks = this.events.get(watchKey); if (!callbacks) { return; } const enableList = this.enableListMap.get(watchKey); if (!enableList || enableList.length === 0) { return; } const lastCallbackKey = enableList.at(-1); if (lastCallbackKey) { try { (_a = callbacks.get(lastCallbackKey)) == null ? void 0 : _a(...args); } catch (error) { console.error(`执行事件"${watchKey}"的回调时出错:`, error); } } } /** * 根据事件名称发布事件 * @param watchKey 监听的key * @param eventKey 事件名称 * @param args 事件参数 */ publishByEventKey(watchKey, eventKey, ...args) { var _a; const callbacks = this.events.get(watchKey); if (!callbacks) { return; } (_a = callbacks.get(eventKey)) == null ? void 0 : _a(...args); } /** * 获取最后一个启用的事件 * @param watchKey 监听的key * @returns 最后一个启用的事件 */ getLastEnableEventKey(watchKey) { const enableList = this.enableListMap.get(watchKey); console.log("enableList", enableList); if (!enableList) { return void 0; } return enableList.at(-1) || void 0; } /** * 移除某个事件的所有订阅 * @param event 事件名称 * @returns 是否成功移除 */ clearEvent(watchKey) { this.disabledAllEventByWatchKey(watchKey); this.events.delete(watchKey); } /** * 移除所有事件订阅 */ clearAllEvents() { this.events.clear(); this.enableListMap.clear(); } /** * 获取事件的订阅数量 * @param event 事件名称 * @returns 订阅数量 */ getSubscriberCount(event) { const callbacks = this.events.get(event); return callbacks ? callbacks.size : 0; } /** * 检查事件是否有订阅者 * @param event 事件名称 * @returns 是否有订阅者 */ hasSubscribers(event) { return this.getSubscriberCount(event) > 0; } /** * 一次性订阅事件,事件触发后自动取消订阅 * @param event 事件名称 * @param callback 回调函数 * @returns 订阅对象,可用于取消订阅 */ once(watchKey, event) { const wrappedCallback = (...args) => { this.unsubscribe(watchKey, { key: event.key, callback: wrappedCallback }); event.callback(...args); }; return this.subscribe(watchKey, { key: event.key, callback: wrappedCallback }); } }; var watch = new Watch(); var watch_default = watch;