UNPKG

vue-hooks-plus

Version:
109 lines (108 loc) 3.42 kB
var __defProp = Object.defineProperty; var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value; var __publicField = (obj, key, value) => { __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value); return value; }; import cloneDeep from "lodash-es/cloneDeep"; import { ref, watchEffect } from "vue"; const _EventEmitter = class { constructor(options) { __publicField(this, "subscriptions", /* @__PURE__ */ new Map()); __publicField(this, "emitEffectCache", /* @__PURE__ */ new Map()); __publicField(this, "isGlobal"); this.isGlobal = !!(options == null ? void 0 : options.global); this.clear(); } static getInstance() { if (!_EventEmitter.instance) { _EventEmitter.instance = new _EventEmitter({ global: true }); } return _EventEmitter.instance; } subscribe(event, listener) { if (!this.subscriptions.has(event)) { this.subscriptions.set(event, /* @__PURE__ */ new Set()); } const listeners = this.subscriptions.get(event); listeners.add(listener); if (!this.isGlobal) { this.emitEffect(event); } return () => { listeners.delete(listener); if (listeners.size === 0) { this.subscriptions.delete(event); } }; } emit(event, ...args) { if (typeof event === "string" || typeof event === "number") { const listeners = this.subscriptions.get(event); listeners == null ? void 0 : listeners.forEach((callback) => { callback == null ? void 0 : callback({ params: cloneDeep(args.length === 1 ? args[0] : args), event }); }); if (!this.isGlobal) { this.emitEffectCache.set(event, { params: cloneDeep(args.length === 1 ? args[0] : args), event }); } } else { throw new TypeError("event must be string or number!"); } } emitEffect(event) { if (this.isGlobal) return; const emitEffectCache = this.emitEffectCache.get(event); const listeners = this.subscriptions.get(event); if (emitEffectCache) { listeners == null ? void 0 : listeners.forEach((listener) => { listener == null ? void 0 : listener({ ...emitEffectCache }); }); } } removeListener(event, listener) { if (!listener) { this.subscriptions.delete(event); } else { const listeners = this.subscriptions.get(event); listeners == null ? void 0 : listeners.delete(listener); if (listeners && listeners.size === 0) { this.subscriptions.delete(event); } } } clear() { this.subscriptions.clear(); this.emitEffectCache.clear(); } }; let EventEmitter = _EventEmitter; __publicField(EventEmitter, "instance", null); function useEventEmitterSubscription(event, listener, emitter) { const _emitter = emitter || EventEmitter.getInstance(); const callbackRef = ref(listener); let unsubscribe = null; watchEffect((onInvalidate) => { if (unsubscribe) unsubscribe(); unsubscribe = _emitter.subscribe(event, (params) => { callbackRef.value(params); }); onInvalidate(() => { if (unsubscribe) unsubscribe(); }); }); } const eventEmitterOverall = EventEmitter.getInstance(); export { EventEmitter, eventEmitterOverall, useEventEmitterSubscription };