@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 +23 -29
- package/package.json +1 -1
- package/types/mitt/src/index.d.ts +8 -22
package/mitt/src/index.js
CHANGED
|
@@ -1,37 +1,31 @@
|
|
|
1
|
-
function
|
|
2
|
-
function
|
|
3
|
-
|
|
4
|
-
|
|
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
|
|
7
|
-
|
|
7
|
+
function c(e, t) {
|
|
8
|
+
const n = (...r) => {
|
|
9
|
+
t(...r), s(e, n);
|
|
10
|
+
};
|
|
11
|
+
f(e, n);
|
|
8
12
|
}
|
|
9
|
-
function
|
|
10
|
-
const n =
|
|
11
|
-
n && (
|
|
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
|
|
18
|
-
e ?
|
|
17
|
+
function i(e) {
|
|
18
|
+
e ? o.delete(e) : o.clear();
|
|
19
19
|
}
|
|
20
|
-
function
|
|
21
|
-
const n =
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
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
|
-
|
|
30
|
+
l as default
|
|
37
31
|
};
|
package/package.json
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"name":"@unicom-cloud/utils","version":"0.1.
|
|
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
|
|
3
|
-
export
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
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
|
|
15
|
+
export default function mitt(all?: Map<EventType, Set<Handler>>): Emitter;
|