epic-designer
Version:
基于vue3的设计器,可视化开发页面表单
86 lines (85 loc) • 2.28 kB
JavaScript
import { provide as A, onUnmounted as E } from "vue";
import { EVENT_BUS_KEY as R } from "../logic/useEventBus.js";
const c = /* @__PURE__ */ new Map();
function y(h = "root") {
const p = (o) => {
if (!c.has(o)) {
const t = /* @__PURE__ */ new Map(), e = /* @__PURE__ */ new Map(), n = 10, l = {
// 清空通道
clear() {
t.clear(), e.clear();
},
// 触发事件
emit(s, ...r) {
e.has(s) || e.set(s, []);
const a = e.get(s);
a.push({ args: r, timestamp: Date.now() }), a.length > n && a.shift(), t.has(s) && t.get(s).forEach((m) => {
try {
m(...r);
} catch (M) {
console.error(`事件 "${s}" 处理错误:`, M);
}
});
},
// 取消监听
off(s, r) {
t.has(s) && t.get(s).delete(r);
},
// 监听事件
on(s, r) {
if (t.has(s) || t.set(s, /* @__PURE__ */ new Set()), t.get(s).add(r), e.has(s) && e.get(s).length > 0) {
const a = e.get(s)[e.get(s).length - 1];
try {
r(...a.args);
} catch (m) {
console.error(`缓存事件 "${s}" 处理错误:`, m);
}
}
return () => {
this.off(s, r);
};
}
};
c.set(o, l);
}
return c.get(o);
}, i = p(h), f = p("root"), C = (o, ...t) => {
i.emit(o, ...t);
}, L = (o, ...t) => {
f.emit(o, ...t);
}, w = (o, t) => {
i.off(o, t);
}, B = (o, t) => {
f.off(o, t);
}, u = () => {
i.clear(), c.delete(h);
}, g = {
clear: u,
emit: C,
emitRoot: L,
off: w,
offRoot: B,
on: (o, t) => {
const e = i.on(o, t);
return E(() => {
var n;
e(), c.has(h) && ([...((n = i.listeners) == null ? void 0 : n.values()) || []].some(
(s) => s.size > 0
) || u());
}), e;
},
onRoot: (o, t) => {
const e = f.on(o, t);
return E(() => {
var n;
e(), c.has("root") && ([...((n = f.listeners) == null ? void 0 : n.values()) || []].some(
(s) => s.size > 0
) || u());
}), e;
}
};
return A(R, g), g;
}
export {
y as createEventBus
};