@unicom-cloud/utils 0.1.18 → 0.1.19

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/mitt/src/index.js CHANGED
@@ -1,37 +1,31 @@
1
- function S(i = /* @__PURE__ */ new Map()) {
2
- function d(e, o, t, n = !1, f = !1) {
3
- const c = { handler: o, context: t, once: n }, s = i.get(e);
4
- s ? s.add(c) : i.set(e, /* @__PURE__ */ new Set([c])), f && a(e);
1
+ function l(o = /* @__PURE__ */ new Map()) {
2
+ function f(e, t) {
3
+ if (typeof t != "function") throw new Error(`${String(e)}: handler 必须为函数`);
4
+ const n = o.get(e) || /* @__PURE__ */ new Set();
5
+ n.add(t), o.set(e, n);
5
6
  }
6
- function u(e, o, t) {
7
- d(e, o, t, !0);
7
+ function c(e, t) {
8
+ const n = (...r) => {
9
+ t(...r), s(e, n);
10
+ };
11
+ f(e, n);
8
12
  }
9
- function l(e, o, t) {
10
- const n = i.get(e);
11
- n && (o ? new Set(n).forEach((f) => {
12
- f.handler === o && (t === void 0 || f.context === t) && n.delete(f);
13
- }) : t ? new Set(n).forEach((f) => {
14
- f.context === t && n.delete(f);
15
- }) : n.clear(), n.size || r(e));
13
+ function s(e, t) {
14
+ const n = o.get(e);
15
+ n && (t ? n.delete(t) : n.clear(), n.size || i(e));
16
16
  }
17
- function r(e) {
18
- e ? i.delete(e) : i.clear();
17
+ function i(e) {
18
+ e ? o.delete(e) : o.clear();
19
19
  }
20
- function a(e, o, ...t) {
21
- const n = i.get(e);
22
- n && new Set(n).forEach((f) => {
23
- const { handler: c, context: s, once: h } = f;
24
- h && l(e, c, s), c.call(s, o, ...t);
25
- });
20
+ function u(e, ...t) {
21
+ const n = o.get(e);
22
+ if (!n) return;
23
+ const r = Array.from(n);
24
+ for (const d of r)
25
+ d(...t);
26
26
  }
27
- return {
28
- on: d,
29
- once: u,
30
- off: l,
31
- offAll: r,
32
- emit: a
33
- };
27
+ return { on: f, once: c, off: s, offAll: i, emit: u };
34
28
  }
35
29
  export {
36
- S as default
30
+ l as default
37
31
  };
package/package.json CHANGED
@@ -1 +1 @@
1
- {"name":"@unicom-cloud/utils","version":"0.1.18","dependencies":{},"peerDependencies":{"lodash":"^4.17.21"},"main":"./index.js","type":"module","types":"types/index.d.ts","publishConfig":{"registry":"https://registry.npmjs.org/","access":"public"}}
1
+ {"name":"@unicom-cloud/utils","version":"0.1.19","dependencies":{},"peerDependencies":{"lodash":"^4.17.21"},"main":"./index.js","type":"module","types":"types/index.d.ts","publishConfig":{"registry":"https://registry.npmjs.org/","access":"public"}}
@@ -1,29 +1,15 @@
1
1
  export type EventType = string | symbol;
2
- export type EventsBase = Record<EventType, unknown>;
3
- export type EventKey<Events extends EventsBase> = keyof Events;
4
- export type EventValue<Events extends EventsBase, Key extends EventKey<Events>> = Events[Key];
5
- export type Handler<T = unknown> = (event: T, ...args: unknown[]) => void;
6
- export type WildcardHandler<Events extends EventsBase> = (event: EventValue<Events, EventKey<Events>>, ...args: unknown[]) => void;
7
- export interface HandlerWrapper<Events extends EventsBase> {
8
- handler: GenericEventHandler<Events>;
9
- context?: unknown;
10
- once?: boolean;
11
- }
12
- export type GenericEventHandler<Events extends EventsBase> = Handler<EventValue<Events, EventKey<Events>>> | WildcardHandler<Events>;
13
- export type EventHandlerSet<Events extends EventsBase, Key extends EventKey<Events>> = Set<HandlerWrapper<Events>>;
14
- export type WildCardEventHandlerSet<Events extends EventsBase> = Set<HandlerWrapper<Events>>;
15
- export type EventHandlerMap<Events extends EventsBase> = Map<EventKey<Events>, EventHandlerSet<Events, EventKey<Events>> | WildCardEventHandlerSet<Events>>;
16
- export interface Emitter<Events extends EventsBase> {
17
- on<Key extends EventKey<Events>>(type: Key, handler: Handler<EventValue<Events, Key>>, context?: unknown): void;
18
- once<Key extends EventKey<Events>>(type: Key, handler: Handler<EventValue<Events, Key>>, context?: unknown): void;
19
- off<Key extends EventKey<Events>>(type: Key, handler?: Handler<EventValue<Events, Key>>, context?: unknown): void;
20
- offAll<Key extends EventKey<Events>>(type?: Key): void;
21
- emit<Key extends EventKey<Events>>(type: Key, event?: EventValue<Events, Key>, ...args: unknown[]): void;
2
+ export type Handler = (...args: unknown[]) => void;
3
+ export interface Emitter {
4
+ on(type: EventType, handler: Handler): void;
5
+ once(type: EventType, handler: Handler): void;
6
+ off(type: EventType, handler?: Handler): void;
7
+ offAll(type?: EventType): void;
8
+ emit(type: EventType, ...args: unknown[]): void;
22
9
  }
23
10
  /**
24
11
  * Mitt: 函数式事件发射器 / 发布-订阅模式实现
25
12
  * @name mitt
26
- * @param {EventHandlerMap<Events>} all 事件名称到已注册处理函数的映射表
27
13
  * @returns {Emitter}
28
14
  */
29
- export default function mitt<Events extends EventsBase>(all?: EventHandlerMap<Events>): Emitter<Events>;
15
+ export default function mitt(all?: Map<EventType, Set<Handler>>): Emitter;