fastevent 1.0.3 → 1.1.0
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/.prettierrc.js +20 -0
- package/.vscode/settings.json +18 -0
- package/CHANGELOG.md +17 -5
- package/dist/index.d.mts +30 -10
- package/dist/index.d.ts +30 -10
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +1 -1
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
- package/readme.md +191 -66
- package/readme_cn.md +192 -67
- package/src/__tests__/emit.test.ts +73 -61
- package/src/__tests__/emitAsync.test.ts +41 -42
- package/src/__tests__/many.test.ts +15 -16
- package/src/__tests__/meta.test.ts +20 -21
- package/src/__tests__/off.test.ts +162 -162
- package/src/__tests__/onany.test.ts +97 -98
- package/src/__tests__/once.test.ts +42 -43
- package/src/__tests__/retain.test.ts +36 -36
- package/src/__tests__/scope.test.ts +38 -39
- package/src/__tests__/types.test.ts +134 -0
- package/src/__tests__/waitFor.test.ts +35 -29
- package/src/__tests__/wildcard.test.ts +114 -115
- package/src/event.ts +250 -213
- package/src/scope.ts +56 -55
- package/src/types.ts +33 -28
- package/src/typestest.ts +0 -102
|
@@ -1,65 +1,64 @@
|
|
|
1
1
|
import { describe, test, expect } from "vitest"
|
|
2
|
-
import { FastEvent } from "../event"
|
|
2
|
+
import { FastEvent } from "../event"
|
|
3
3
|
|
|
4
|
-
describe("异步发布与订阅",async ()=>{
|
|
5
|
-
test("异步发布订阅事件",async ()=>{
|
|
6
|
-
const emitter = new FastEvent()
|
|
4
|
+
describe("异步发布与订阅", async () => {
|
|
5
|
+
test("异步发布订阅事件", async () => {
|
|
6
|
+
const emitter = new FastEvent()
|
|
7
7
|
const events: any[] = []
|
|
8
|
-
emitter.on("x",async (payload,
|
|
9
|
-
events.push({type, payload})
|
|
8
|
+
emitter.on("x", async ({ payload, type }) => {
|
|
9
|
+
events.push({ type, payload })
|
|
10
10
|
})
|
|
11
|
-
await emitter.emitAsync("x",1)
|
|
11
|
+
await emitter.emitAsync("x", 1)
|
|
12
12
|
expect(events[0].type).toBe("x")
|
|
13
13
|
expect(events[0].payload).toBe(1)
|
|
14
14
|
})
|
|
15
|
-
test("异步发布订阅事件后取消",async ()=>{
|
|
16
|
-
const emitter = new FastEvent()
|
|
17
|
-
const events:string[]=[]
|
|
18
|
-
const subscriber = emitter.on("x",async (
|
|
19
|
-
events.push(type)
|
|
15
|
+
test("异步发布订阅事件后取消", async () => {
|
|
16
|
+
const emitter = new FastEvent()
|
|
17
|
+
const events: string[] = []
|
|
18
|
+
const subscriber = emitter.on("x", async ({ type }) => {
|
|
19
|
+
events.push(type)
|
|
20
20
|
})
|
|
21
|
-
await emitter.emitAsync("x",1)
|
|
21
|
+
await emitter.emitAsync("x", 1)
|
|
22
22
|
expect(events).toEqual(["x"])
|
|
23
23
|
subscriber.off()
|
|
24
|
-
await emitter.emitAsync("x",1)
|
|
25
|
-
await emitter.emitAsync("x",1)
|
|
26
|
-
await emitter.emitAsync("x",1)
|
|
24
|
+
await emitter.emitAsync("x", 1)
|
|
25
|
+
await emitter.emitAsync("x", 1)
|
|
26
|
+
await emitter.emitAsync("x", 1)
|
|
27
27
|
expect(events).toEqual(["x"])
|
|
28
|
-
})
|
|
29
|
-
test("发布订阅层级事件",async ()=>{
|
|
30
|
-
const emitter = new FastEvent()
|
|
31
|
-
const events:string[]=[]
|
|
32
|
-
emitter.on("a.b.c",async (
|
|
33
|
-
events.push(type)
|
|
28
|
+
})
|
|
29
|
+
test("发布订阅层级事件", async () => {
|
|
30
|
+
const emitter = new FastEvent()
|
|
31
|
+
const events: string[] = []
|
|
32
|
+
emitter.on("a.b.c", async ({ type }) => {
|
|
33
|
+
events.push(type)
|
|
34
34
|
})
|
|
35
|
-
await emitter.emitAsync("a",1)
|
|
36
|
-
await emitter.emitAsync("a.b",1)
|
|
37
|
-
await emitter.emitAsync("a.b.c",1)
|
|
35
|
+
await emitter.emitAsync("a", 1)
|
|
36
|
+
await emitter.emitAsync("a.b", 1)
|
|
37
|
+
await emitter.emitAsync("a.b.c", 1)
|
|
38
38
|
expect(events).toEqual(["a.b.c"])
|
|
39
39
|
})
|
|
40
|
-
test("返回事件执行结果",async ()=>{
|
|
41
|
-
const emitter = new FastEvent()
|
|
42
|
-
for(let i=1;i<=10;i++){
|
|
43
|
-
emitter.on("x",async ()=>{
|
|
40
|
+
test("返回事件执行结果", async () => {
|
|
41
|
+
const emitter = new FastEvent()
|
|
42
|
+
for (let i = 1; i <= 10; i++) {
|
|
43
|
+
emitter.on("x", async () => {
|
|
44
44
|
return i
|
|
45
45
|
})
|
|
46
46
|
}
|
|
47
|
-
const results = await emitter.emitAsync("x",1)
|
|
48
|
-
expect(results).toEqual([1,2,3,4,5,6,7,8,9,10])
|
|
47
|
+
const results = await emitter.emitAsync("x", 1)
|
|
48
|
+
expect(results).toEqual([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
|
|
49
49
|
})
|
|
50
|
-
test("异步侦听器执行出错返回事件执行结果",async ()=>{
|
|
51
|
-
const emitter = new FastEvent()
|
|
52
|
-
for(let i=1;i<=10;i++){
|
|
53
|
-
emitter.on("x",async ()=>{
|
|
54
|
-
if(i % 2 ==0) throw new Error("custom")
|
|
50
|
+
test("异步侦听器执行出错返回事件执行结果", async () => {
|
|
51
|
+
const emitter = new FastEvent()
|
|
52
|
+
for (let i = 1; i <= 10; i++) {
|
|
53
|
+
emitter.on("x", async () => {
|
|
54
|
+
if (i % 2 == 0) throw new Error("custom")
|
|
55
55
|
return i
|
|
56
56
|
})
|
|
57
57
|
}
|
|
58
|
-
const results = await emitter.emitAsync("x",1)
|
|
59
|
-
for(let i=1;i<=10;i++){
|
|
60
|
-
if(i % 2 ==0) expect(results[i-1]).toBeInstanceOf(Error)
|
|
61
|
-
else expect(results[i-1]).toBe(i)
|
|
58
|
+
const results = await emitter.emitAsync("x", 1)
|
|
59
|
+
for (let i = 1; i <= 10; i++) {
|
|
60
|
+
if (i % 2 == 0) expect(results[i - 1]).toBeInstanceOf(Error)
|
|
61
|
+
else expect(results[i - 1]).toBe(i)
|
|
62
62
|
}
|
|
63
63
|
})
|
|
64
|
-
})
|
|
65
|
-
|
|
64
|
+
})
|
|
@@ -1,23 +1,22 @@
|
|
|
1
1
|
import { describe, test, expect } from "vitest"
|
|
2
|
-
import { FastEvent } from "../event"
|
|
2
|
+
import { FastEvent } from "../event"
|
|
3
3
|
|
|
4
|
-
describe("指定次数事件的发布与订阅",async ()=>{
|
|
5
|
-
test("简单发布只订阅一次事件",()=>{
|
|
6
|
-
const emitter = new FastEvent()
|
|
7
|
-
const events:string[] =[]
|
|
8
|
-
emitter.on("x",(payload,
|
|
4
|
+
describe("指定次数事件的发布与订阅", async () => {
|
|
5
|
+
test("简单发布只订阅一次事件", () => {
|
|
6
|
+
const emitter = new FastEvent()
|
|
7
|
+
const events: string[] = []
|
|
8
|
+
emitter.on("x", ({ payload, type }) => {
|
|
9
9
|
expect(type).toBe("x")
|
|
10
|
-
expect(payload).toBe(1)
|
|
10
|
+
expect(payload).toBe(1)
|
|
11
11
|
events.push(type)
|
|
12
|
-
},{count:2})
|
|
13
|
-
emitter.emit("x",1)
|
|
14
|
-
emitter.emit("x",1)
|
|
15
|
-
emitter.emit("x",1)
|
|
16
|
-
emitter.emit("x",1)
|
|
17
|
-
emitter.emit("x",1)
|
|
18
|
-
expect(events).toEqual(["x","x"])
|
|
19
|
-
|
|
12
|
+
}, { count: 2 })
|
|
13
|
+
emitter.emit("x", 1)
|
|
14
|
+
emitter.emit("x", 1)
|
|
15
|
+
emitter.emit("x", 1)
|
|
16
|
+
emitter.emit("x", 1)
|
|
17
|
+
emitter.emit("x", 1)
|
|
18
|
+
expect(events).toEqual(["x", "x"])
|
|
19
|
+
|
|
20
20
|
})
|
|
21
21
|
|
|
22
22
|
})
|
|
23
|
-
|
|
@@ -1,29 +1,28 @@
|
|
|
1
1
|
import { describe, test, expect } from "vitest"
|
|
2
|
-
import { FastEvent } from "../event"
|
|
2
|
+
import { FastEvent } from "../event"
|
|
3
3
|
|
|
4
|
-
describe("传递Meta数据",async ()=>{
|
|
5
|
-
test("全局meta数据",()=>{
|
|
4
|
+
describe("传递Meta数据", async () => {
|
|
5
|
+
test("全局meta数据", () => {
|
|
6
6
|
const emitter = new FastEvent({
|
|
7
|
-
meta:{ a:1 }
|
|
8
|
-
})
|
|
9
|
-
emitter.on("x",(payload,
|
|
7
|
+
meta: { a: 1 }
|
|
8
|
+
})
|
|
9
|
+
emitter.on("x", ({ payload, type, meta }) => {
|
|
10
10
|
expect(type).toBe("x")
|
|
11
|
-
expect(payload).toBe(1)
|
|
12
|
-
expect(a).toBe(1)
|
|
11
|
+
expect(payload).toBe(1)
|
|
12
|
+
expect(meta.a).toBe(1)
|
|
13
13
|
})
|
|
14
|
-
emitter.emit("x",1)
|
|
15
|
-
})
|
|
16
|
-
test("emit时传递meta数据",()=>{
|
|
14
|
+
emitter.emit("x", 1)
|
|
15
|
+
})
|
|
16
|
+
test("emit时传递meta数据", () => {
|
|
17
17
|
const emitter = new FastEvent({
|
|
18
|
-
meta:{ a:1,b: 2 }
|
|
19
|
-
})
|
|
20
|
-
emitter.on("x",(payload,
|
|
18
|
+
meta: { a: 1, b: 2 }
|
|
19
|
+
})
|
|
20
|
+
emitter.on("x", ({ payload, type, meta }) => {
|
|
21
21
|
expect(type).toBe("x")
|
|
22
|
-
expect(payload).toBe(1)
|
|
23
|
-
expect(a).toBe(10)
|
|
24
|
-
expect(b).toBe(20)
|
|
22
|
+
expect(payload).toBe(1)
|
|
23
|
+
expect(meta.a).toBe(10)
|
|
24
|
+
expect(meta.b).toBe(20)
|
|
25
25
|
})
|
|
26
|
-
emitter.emit("x",1,false,{a:10,b:20})
|
|
27
|
-
})
|
|
28
|
-
})
|
|
29
|
-
|
|
26
|
+
emitter.emit("x", 1, false, { a: 10, b: 20 })
|
|
27
|
+
})
|
|
28
|
+
})
|
|
@@ -1,214 +1,214 @@
|
|
|
1
1
|
import { describe, test, expect } from "vitest"
|
|
2
|
-
import { FastEvent } from "../event"
|
|
3
|
-
|
|
2
|
+
import { FastEvent } from "../event"
|
|
3
|
+
import { FastEventListener } from '../types';
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
const
|
|
9
|
-
const
|
|
5
|
+
|
|
6
|
+
describe("退订事件", () => {
|
|
7
|
+
test("基本退订事件", () => {
|
|
8
|
+
const emitter = new FastEvent()
|
|
9
|
+
const events: string[] = []
|
|
10
|
+
const subscriber = emitter.on("x", ({ payload, type }) => {
|
|
10
11
|
expect(type).toBe("x")
|
|
11
|
-
expect(payload).toBe(1)
|
|
12
|
+
expect(payload).toBe(1)
|
|
12
13
|
events.push(type)
|
|
13
14
|
})
|
|
14
|
-
emitter.emit("x",1)
|
|
15
|
+
emitter.emit("x", 1)
|
|
15
16
|
subscriber.off()
|
|
16
|
-
emitter.emit("x",1)
|
|
17
|
+
emitter.emit("x", 1)
|
|
17
18
|
expect(events).toEqual(["x"])
|
|
18
19
|
})
|
|
19
|
-
test("根据事件类型和侦听器进行退订",()=>{
|
|
20
|
-
const emitter = new FastEvent()
|
|
21
|
-
const events:string[] =[]
|
|
22
|
-
const listener =(payload
|
|
20
|
+
test("根据事件类型和侦听器进行退订", () => {
|
|
21
|
+
const emitter = new FastEvent()
|
|
22
|
+
const events: string[] = []
|
|
23
|
+
const listener: FastEventListener<string, number> = (({ payload, type }) => {
|
|
23
24
|
expect(type).toBe("x")
|
|
24
|
-
expect(payload).toBe(1)
|
|
25
|
+
expect(payload).toBe(1)
|
|
25
26
|
events.push(type)
|
|
26
|
-
}
|
|
27
|
-
emitter.on("x",listener)
|
|
28
|
-
emitter.emit("x",1)
|
|
29
|
-
emitter.off("x",listener)
|
|
30
|
-
emitter.emit("x",1)
|
|
27
|
+
})
|
|
28
|
+
emitter.on("x", listener)
|
|
29
|
+
emitter.emit("x", 1)
|
|
30
|
+
emitter.off("x", listener)
|
|
31
|
+
emitter.emit("x", 1)
|
|
31
32
|
expect(events).toEqual(["x"])
|
|
32
33
|
})
|
|
33
|
-
test("多级事件根据事件类型和侦听器进行退订",()=>{
|
|
34
|
-
const emitter = new FastEvent()
|
|
35
|
-
const events:string[] =[]
|
|
36
|
-
const listener =(payload
|
|
34
|
+
test("多级事件根据事件类型和侦听器进行退订", () => {
|
|
35
|
+
const emitter = new FastEvent()
|
|
36
|
+
const events: string[] = []
|
|
37
|
+
const listener: FastEventListener<string, number> = (({ payload, type }) => {
|
|
37
38
|
expect(type).toBe("a/b/c/d")
|
|
38
|
-
expect(payload).toBe(1)
|
|
39
|
+
expect(payload).toBe(1)
|
|
39
40
|
events.push(type)
|
|
40
|
-
}
|
|
41
|
-
emitter.on("a/b/c/d",listener)
|
|
42
|
-
emitter.emit("a/b/c/d",1)
|
|
43
|
-
emitter.off("a/b/c/d",listener) //
|
|
44
|
-
emitter.emit("a/b/c/d",1)
|
|
45
|
-
emitter.emit("a/b/c/d",2)
|
|
46
|
-
emitter.emit("a/b/c/d",3)
|
|
41
|
+
})
|
|
42
|
+
emitter.on("a/b/c/d", listener)
|
|
43
|
+
emitter.emit("a/b/c/d", 1)
|
|
44
|
+
emitter.off("a/b/c/d", listener) //
|
|
45
|
+
emitter.emit("a/b/c/d", 1)
|
|
46
|
+
emitter.emit("a/b/c/d", 2)
|
|
47
|
+
emitter.emit("a/b/c/d", 3)
|
|
47
48
|
expect(events).toEqual(["a/b/c/d"])
|
|
48
49
|
})
|
|
49
50
|
|
|
50
|
-
test("通配符事件退订",()=>{
|
|
51
|
-
const emitter = new FastEvent()
|
|
52
|
-
const events:string[] =[]
|
|
53
|
-
const listener =(payload
|
|
51
|
+
test("通配符事件退订", () => {
|
|
52
|
+
const emitter = new FastEvent()
|
|
53
|
+
const events: string[] = []
|
|
54
|
+
const listener: FastEventListener<string, number> = ({ payload, type }) => {
|
|
54
55
|
expect(type).toBe("a/b/c/d")
|
|
55
|
-
expect(payload).toBe(1)
|
|
56
|
+
expect(payload).toBe(1)
|
|
56
57
|
events.push(type)
|
|
57
58
|
}
|
|
58
|
-
emitter.on("a/b/c/*",listener)
|
|
59
|
-
emitter.emit("a/b/c/d",1)
|
|
60
|
-
emitter.off("a/b/c/*",listener) //
|
|
61
|
-
emitter.emit("a/b/c/d",1)
|
|
62
|
-
emitter.emit("a/b/c/d",2)
|
|
63
|
-
emitter.emit("a/b/c/d",3)
|
|
59
|
+
emitter.on("a/b/c/*", listener)
|
|
60
|
+
emitter.emit("a/b/c/d", 1)
|
|
61
|
+
emitter.off("a/b/c/*", listener) //
|
|
62
|
+
emitter.emit("a/b/c/d", 1)
|
|
63
|
+
emitter.emit("a/b/c/d", 2)
|
|
64
|
+
emitter.emit("a/b/c/d", 3)
|
|
64
65
|
expect(events).toEqual(["a/b/c/d"])
|
|
65
66
|
})
|
|
66
67
|
|
|
67
|
-
test("退订指定的侦听器",()=>{
|
|
68
|
-
const emitter = new FastEvent()
|
|
69
|
-
const events:string[] =[]
|
|
70
|
-
const listener =( payload
|
|
68
|
+
test("退订指定的侦听器", () => {
|
|
69
|
+
const emitter = new FastEvent()
|
|
70
|
+
const events: string[] = []
|
|
71
|
+
const listener: FastEventListener<string, number> = ({ payload, type }) => {
|
|
71
72
|
expect(type).toBe("a/b/c/d")
|
|
72
|
-
expect(payload).toBe(1)
|
|
73
|
+
expect(payload).toBe(1)
|
|
73
74
|
events.push(type)
|
|
74
75
|
}
|
|
75
|
-
emitter.on("a/b/c/*",listener)
|
|
76
|
-
emitter.on("a/b/*/*",listener)
|
|
77
|
-
emitter.on("a/*/*/*",listener)
|
|
78
|
-
emitter.on("*/*/*/*",listener)
|
|
79
|
-
emitter.emit("a/b/c/d",1)
|
|
80
|
-
emitter.off(listener)
|
|
81
|
-
emitter.emit("a/b/c/d",1)
|
|
82
|
-
emitter.emit("a/b/c/d",2)
|
|
83
|
-
emitter.emit("a/b/c/d",3)
|
|
84
|
-
emitter.emit("a/b/c/d",4)
|
|
85
|
-
expect(events).toEqual(["a/b/c/d","a/b/c/d","a/b/c/d","a/b/c/d"])
|
|
76
|
+
emitter.on("a/b/c/*", listener)
|
|
77
|
+
emitter.on("a/b/*/*", listener)
|
|
78
|
+
emitter.on("a/*/*/*", listener)
|
|
79
|
+
emitter.on("*/*/*/*", listener)
|
|
80
|
+
emitter.emit("a/b/c/d", 1)
|
|
81
|
+
emitter.off(listener)
|
|
82
|
+
emitter.emit("a/b/c/d", 1)
|
|
83
|
+
emitter.emit("a/b/c/d", 2)
|
|
84
|
+
emitter.emit("a/b/c/d", 3)
|
|
85
|
+
emitter.emit("a/b/c/d", 4)
|
|
86
|
+
expect(events).toEqual(["a/b/c/d", "a/b/c/d", "a/b/c/d", "a/b/c/d"])
|
|
86
87
|
})
|
|
87
|
-
test("退订指定的事件时不指定侦听器",()=>{
|
|
88
|
-
const emitter = new FastEvent()
|
|
89
|
-
const events:string[] =[]
|
|
90
|
-
const listener =( payload
|
|
88
|
+
test("退订指定的事件时不指定侦听器", () => {
|
|
89
|
+
const emitter = new FastEvent()
|
|
90
|
+
const events: string[] = []
|
|
91
|
+
const listener: FastEventListener<string, number> = ({ payload, type }) => {
|
|
91
92
|
events.push(type)
|
|
92
93
|
}
|
|
93
|
-
emitter.on("a",listener)
|
|
94
|
-
emitter.on("b",listener)
|
|
95
|
-
emitter.on("c",listener)
|
|
96
|
-
emitter.on("d",listener)
|
|
97
|
-
emitter.emit("a",1)
|
|
98
|
-
emitter.emit("a",1)
|
|
99
|
-
emitter.off("a")
|
|
100
|
-
emitter.emit("a",1)
|
|
101
|
-
emitter.emit("a",1)
|
|
102
|
-
emitter.emit("a",1)
|
|
103
|
-
emitter.emit("b",2)
|
|
104
|
-
emitter.emit("c",3)
|
|
105
|
-
emitter.emit("d",4)
|
|
106
|
-
expect(events).toEqual(["a","a","b","c","d"])
|
|
94
|
+
emitter.on("a", listener)
|
|
95
|
+
emitter.on("b", listener)
|
|
96
|
+
emitter.on("c", listener)
|
|
97
|
+
emitter.on("d", listener)
|
|
98
|
+
emitter.emit("a", 1)
|
|
99
|
+
emitter.emit("a", 1)
|
|
100
|
+
emitter.off("a")
|
|
101
|
+
emitter.emit("a", 1)
|
|
102
|
+
emitter.emit("a", 1)
|
|
103
|
+
emitter.emit("a", 1)
|
|
104
|
+
emitter.emit("b", 2)
|
|
105
|
+
emitter.emit("c", 3)
|
|
106
|
+
emitter.emit("d", 4)
|
|
107
|
+
expect(events).toEqual(["a", "a", "b", "c", "d"])
|
|
107
108
|
})
|
|
108
|
-
test("退订所有侦听器",()=>{
|
|
109
|
-
const emitter = new FastEvent()
|
|
110
|
-
const events:string[] =[]
|
|
111
|
-
const listener =( payload
|
|
109
|
+
test("退订所有侦听器", () => {
|
|
110
|
+
const emitter = new FastEvent()
|
|
111
|
+
const events: string[] = []
|
|
112
|
+
const listener: FastEventListener<string, number> = ({ payload, type }) => {
|
|
112
113
|
events.push(type)
|
|
113
114
|
}
|
|
114
|
-
emitter.on("a",listener)
|
|
115
|
-
emitter.on("b",listener)
|
|
116
|
-
emitter.on("c",listener)
|
|
117
|
-
emitter.on("d",listener)
|
|
118
|
-
emitter.emit("a",1)
|
|
119
|
-
emitter.emit("b",1)
|
|
120
|
-
emitter.emit("c",1)
|
|
121
|
-
emitter.emit("d",1)
|
|
115
|
+
emitter.on("a", listener)
|
|
116
|
+
emitter.on("b", listener)
|
|
117
|
+
emitter.on("c", listener)
|
|
118
|
+
emitter.on("d", listener)
|
|
119
|
+
emitter.emit("a", 1)
|
|
120
|
+
emitter.emit("b", 1)
|
|
121
|
+
emitter.emit("c", 1)
|
|
122
|
+
emitter.emit("d", 1)
|
|
122
123
|
emitter.offAll()
|
|
123
|
-
emitter.emit("a",1)
|
|
124
|
-
emitter.emit("a",1)
|
|
125
|
-
emitter.emit("b",1)
|
|
126
|
-
emitter.emit("b",1)
|
|
127
|
-
emitter.emit("c",1)
|
|
128
|
-
emitter.emit("c",1)
|
|
129
|
-
emitter.emit("d",1)
|
|
130
|
-
emitter.emit("d",1)
|
|
131
|
-
expect(events).toEqual(["a","b","c","d"])
|
|
124
|
+
emitter.emit("a", 1)
|
|
125
|
+
emitter.emit("a", 1)
|
|
126
|
+
emitter.emit("b", 1)
|
|
127
|
+
emitter.emit("b", 1)
|
|
128
|
+
emitter.emit("c", 1)
|
|
129
|
+
emitter.emit("c", 1)
|
|
130
|
+
emitter.emit("d", 1)
|
|
131
|
+
emitter.emit("d", 1)
|
|
132
|
+
expect(events).toEqual(["a", "b", "c", "d"])
|
|
132
133
|
})
|
|
133
|
-
test("退订含通配符的事件",()=>{
|
|
134
|
-
const emitter = new FastEvent()
|
|
135
|
-
const events:string[] =[]
|
|
136
|
-
const listener =( payload
|
|
134
|
+
test("退订含通配符的事件", () => {
|
|
135
|
+
const emitter = new FastEvent()
|
|
136
|
+
const events: string[] = []
|
|
137
|
+
const listener: FastEventListener<string, number> = ({ payload, type }) => {
|
|
137
138
|
events.push(type)
|
|
138
139
|
}
|
|
139
|
-
emitter.on("a",listener)
|
|
140
|
-
emitter.on("b",listener)
|
|
141
|
-
emitter.on("c",listener)
|
|
142
|
-
emitter.on("d",listener)
|
|
143
|
-
emitter.emit("a",1)
|
|
144
|
-
emitter.emit("b",1)
|
|
145
|
-
emitter.emit("c",1)
|
|
146
|
-
emitter.emit("d",1)
|
|
140
|
+
emitter.on("a", listener)
|
|
141
|
+
emitter.on("b", listener)
|
|
142
|
+
emitter.on("c", listener)
|
|
143
|
+
emitter.on("d", listener)
|
|
144
|
+
emitter.emit("a", 1)
|
|
145
|
+
emitter.emit("b", 1)
|
|
146
|
+
emitter.emit("c", 1)
|
|
147
|
+
emitter.emit("d", 1)
|
|
147
148
|
emitter.off("*")
|
|
148
|
-
emitter.emit("a",1)
|
|
149
|
-
emitter.emit("a",1)
|
|
150
|
-
emitter.emit("b",1)
|
|
151
|
-
emitter.emit("b",1)
|
|
152
|
-
emitter.emit("c",1)
|
|
153
|
-
emitter.emit("c",1)
|
|
154
|
-
emitter.emit("d",1)
|
|
155
|
-
emitter.emit("d",1)
|
|
156
|
-
expect(events).toEqual(["a","b","c","d"])
|
|
149
|
+
emitter.emit("a", 1)
|
|
150
|
+
emitter.emit("a", 1)
|
|
151
|
+
emitter.emit("b", 1)
|
|
152
|
+
emitter.emit("b", 1)
|
|
153
|
+
emitter.emit("c", 1)
|
|
154
|
+
emitter.emit("c", 1)
|
|
155
|
+
emitter.emit("d", 1)
|
|
156
|
+
emitter.emit("d", 1)
|
|
157
|
+
expect(events).toEqual(["a", "b", "c", "d"])
|
|
157
158
|
})
|
|
158
|
-
test("退订含通配符的事件",()=>{
|
|
159
|
-
const emitter = new FastEvent()
|
|
160
|
-
const events:string[] =[]
|
|
161
|
-
const listener =( payload
|
|
159
|
+
test("退订含通配符的事件", () => {
|
|
160
|
+
const emitter = new FastEvent()
|
|
161
|
+
const events: string[] = []
|
|
162
|
+
const listener: FastEventListener<string, number> = ({ payload, type }) => {
|
|
162
163
|
events.push(type)
|
|
163
164
|
}
|
|
164
|
-
emitter.on("a/*",listener)
|
|
165
|
-
emitter.on("b/*",listener)
|
|
166
|
-
emitter.on("c/*",listener)
|
|
167
|
-
emitter.on("d/*",listener)
|
|
168
|
-
emitter.emit("a/1",1)
|
|
169
|
-
emitter.emit("b/2",1)
|
|
170
|
-
emitter.emit("c/3",1)
|
|
171
|
-
emitter.emit("d/4",1)
|
|
165
|
+
emitter.on("a/*", listener)
|
|
166
|
+
emitter.on("b/*", listener)
|
|
167
|
+
emitter.on("c/*", listener)
|
|
168
|
+
emitter.on("d/*", listener)
|
|
169
|
+
emitter.emit("a/1", 1)
|
|
170
|
+
emitter.emit("b/2", 1)
|
|
171
|
+
emitter.emit("c/3", 1)
|
|
172
|
+
emitter.emit("d/4", 1)
|
|
172
173
|
emitter.off("a/*")
|
|
173
174
|
emitter.off("b/*")
|
|
174
|
-
emitter.emit("a/1",2)
|
|
175
|
-
emitter.emit("b/2",2)
|
|
176
|
-
emitter.emit("c/3",2)
|
|
177
|
-
emitter.emit("d/4",2)
|
|
178
|
-
expect(events).toEqual(["a/1","b/2","c/3","d/4","c/3","d/4"])
|
|
175
|
+
emitter.emit("a/1", 2)
|
|
176
|
+
emitter.emit("b/2", 2)
|
|
177
|
+
emitter.emit("c/3", 2)
|
|
178
|
+
emitter.emit("d/4", 2)
|
|
179
|
+
expect(events).toEqual(["a/1", "b/2", "c/3", "d/4", "c/3", "d/4"])
|
|
179
180
|
})
|
|
180
|
-
test("退订多层含通配符的事件",()=>{
|
|
181
|
-
const emitter = new FastEvent()
|
|
182
|
-
const events:string[] =[]
|
|
183
|
-
const listener =( payload
|
|
181
|
+
test("退订多层含通配符的事件", () => {
|
|
182
|
+
const emitter = new FastEvent()
|
|
183
|
+
const events: string[] = []
|
|
184
|
+
const listener: FastEventListener<string, number> = ({ payload, type }) => {
|
|
184
185
|
events.push(type)
|
|
185
186
|
}
|
|
186
|
-
emitter.on("a/*",listener)
|
|
187
|
-
emitter.on("a/b/*",listener)
|
|
188
|
-
emitter.on("a/b/c/*",listener)
|
|
189
|
-
emitter.on("a/b/c/d/*",listener)
|
|
190
|
-
emitter.on("a/b/c/d/e/*",listener)
|
|
187
|
+
emitter.on("a/*", listener)
|
|
188
|
+
emitter.on("a/b/*", listener)
|
|
189
|
+
emitter.on("a/b/c/*", listener)
|
|
190
|
+
emitter.on("a/b/c/d/*", listener)
|
|
191
|
+
emitter.on("a/b/c/d/e/*", listener)
|
|
191
192
|
|
|
192
|
-
emitter.emit("a/1",1)
|
|
193
|
-
emitter.emit("a/b/2",1)
|
|
194
|
-
emitter.emit("a/b/c/3",1)
|
|
195
|
-
emitter.emit("a/b/c/d/4",1)
|
|
196
|
-
emitter.emit("a/b/c/d/e/5",1)
|
|
193
|
+
emitter.emit("a/1", 1)
|
|
194
|
+
emitter.emit("a/b/2", 1)
|
|
195
|
+
emitter.emit("a/b/c/3", 1)
|
|
196
|
+
emitter.emit("a/b/c/d/4", 1)
|
|
197
|
+
emitter.emit("a/b/c/d/e/5", 1)
|
|
197
198
|
|
|
198
199
|
emitter.off("a/**")
|
|
199
|
-
emitter.emit("a/1",1)
|
|
200
|
-
emitter.emit("a/b/2",1)
|
|
201
|
-
emitter.emit("a/b/c/3",1)
|
|
202
|
-
emitter.emit("a/b/c/d/4",1)
|
|
203
|
-
emitter.emit("a/b/c/d/e/5",1)
|
|
204
|
-
emitter.emit("a/1",1)
|
|
205
|
-
emitter.emit("a/b/2",1)
|
|
206
|
-
emitter.emit("a/b/c/3",1)
|
|
207
|
-
emitter.emit("a/b/c/d/4",1)
|
|
208
|
-
emitter.emit("a/b/c/d/e/5",1)
|
|
200
|
+
emitter.emit("a/1", 1)
|
|
201
|
+
emitter.emit("a/b/2", 1)
|
|
202
|
+
emitter.emit("a/b/c/3", 1)
|
|
203
|
+
emitter.emit("a/b/c/d/4", 1)
|
|
204
|
+
emitter.emit("a/b/c/d/e/5", 1)
|
|
205
|
+
emitter.emit("a/1", 1)
|
|
206
|
+
emitter.emit("a/b/2", 1)
|
|
207
|
+
emitter.emit("a/b/c/3", 1)
|
|
208
|
+
emitter.emit("a/b/c/d/4", 1)
|
|
209
|
+
emitter.emit("a/b/c/d/e/5", 1)
|
|
209
210
|
|
|
210
|
-
expect(events).toEqual(["a/1","a/b/2","a/b/c/3","a/b/c/d/4","a/b/c/d/e/5"])
|
|
211
|
+
expect(events).toEqual(["a/1", "a/b/2", "a/b/c/3", "a/b/c/d/4", "a/b/c/d/e/5"])
|
|
211
212
|
})
|
|
212
213
|
})
|
|
213
214
|
|
|
214
|
-
|