| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138 |
- class EventBus {
- constructor() {
- this.eventObject = {};
- this.callbackId = 0;
- this.state = {}
- // console.log("eventBus初始化了")
- }
- // 发布事件
- publish(eventName, ...args) {
- // console.log("发布事件", eventName, ...args);
- this.state[eventName] = args.length === 1 ? args[0] : args;
- // 取出当前事件所有的回调函数
- const callbackObject = this.eventObject[eventName];
- if (!callbackObject) return console.warn(eventName + " not found!");
- // 执行每一个回调函数
- for (let id in callbackObject) {
- // 执行时传入参数
- // console.log("准备执行回调", id); // 确认回调即将执行
- callbackObject[id](...args);
- // console.log("回调执行完毕", id); // 确认回调执行完毕
- // 只订阅一次的回调函数需要删除
- if (id[0] === "d") {
- delete callbackObject[id];
- }
- }
- }
- // 订阅事件
- subscribe(eventName, callback) {
- // console.log("订阅事件", eventName, callback);
- // 初始化这个事件
- if (!this.eventObject[eventName]) {
- // 使用对象存储,注销回调函数的时候提高删除的效率
- this.eventObject[eventName] = {};
- }
- const id = this.callbackId;
- this.callbackId++; // 确保每次调用后递增
- // 存储订阅者的回调函数
- // callbackId使用后需要自增,供下一个回调函数使用
- this.eventObject[eventName][id] = callback;
- // console.log("回调ID分配:", id); // 查看ID
- // 每一次订阅事件,都生成唯一一个取消订阅的函数
- const unSubscribe = () => {
- // 清除这个订阅者的回调函数
- delete this.eventObject[eventName][id];
- // 如果这个事件没有订阅者了,也把整个事件对象清除
- if (Object.keys(this.eventObject[eventName]).length === 0) {
- delete this.eventObject[eventName];
- }
- };
- return { unSubscribe };
- }
- // 只订阅一次
- subscribeOnce(eventName, callback) {
- // 初始化这个事件
- if (!this.eventObject[eventName]) {
- // 使用对象存储,注销回调函数的时候提高删除的效率
- this.eventObject[eventName] = {};
- }
- // 标示为只订阅一次的回调函数
- const id = "d" + this.callbackId++;
- // 存储订阅者的回调函数
- // callbackId使用后需要自增,供下一个回调函数使用
- this.eventObject[eventName][id] = callback;
- // 每一次订阅事件,都生成唯一一个取消订阅的函数
- const unSubscribe = () => {
- // 清除这个订阅者的回调函数
- delete this.eventObject[eventName][id];
- // 如果这个事件没有订阅者了,也把整个事件对象清除
- if (Object.keys(this.eventObject[eventName]).length === 0) {
- delete this.eventObject[eventName];
- }
- };
- return { unSubscribe };
- }
- // 获取状态的新方法
- getState(eventName) {
- return this.state[eventName];
- }
- // 清除事件
- clear(eventName) {
- // 未提供事件名称,默认清除所有事件
- if (!eventName) {
- this.eventObject = {};
- this.state = {};
- return;
- }
- // 清除指定事件
- delete this.eventObject[eventName];
- delete this.state[eventName];
- }
- }
- if (!window.sharedEventBus) {
- window.sharedEventBus = new EventBus();
- window.sharedEventBus.publish('folderPath', []);
- }
- export const eventBus = window.sharedEventBus;
- export const EVEN_VAR = {
- // 系统编辑模式变化
- systemEditModelChange: "systemEditModelChange",
- // 显示项目弹窗
- showProjectDialog: "showProjectDialog",
- showKnowledgeDialog: "KnowledgeDialog",
- showOrcDialog: "showOrcDialog",
- showOrcListDialog: "showOrcListDialog",
- showZhibiaoNodeDialog: "showZhibiaoNodeDialog",
- showVerifyDialog: "showVerifyDialog",
- showGlobalSearchDialog: "showGlobalSearchDialog",
- // 1.2.2录入页校验
- showAddUserInfoDialog: "showAddUserInfoDialog",
- // 1.5.2.新增工作节点
- showAddWorkNodeDialog: "showAddWorkNodeDialog",
- // 知识库变动01
- showKnowlegeDbViewerDialog: "showKnowlegeDbViewerDialog",
- showKnowlegeDbChangeDialog: "showKnowlegeDbChangeDialog",
- // 编辑器
- showEditorDialog: "showEditorDialog",
- // 添加预警窗口
- showAddWaringDialog: "showAddWaringDialog",
- // 空窗口
- showEmptyDialog: "showEmptyDialog",
- currentPage:"index.html",
- };
|