fastevent 0.0.1 → 1.0.1
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/.changeset/README.md +8 -0
- package/.changeset/config.json +11 -0
- package/.github/workflows/publish.yaml +50 -0
- package/.vscode/launch.json +20 -0
- package/CHANGELOG.md +14 -0
- package/LICENSE +21 -0
- package/bench.png +0 -0
- package/dist/index.d.mts +205 -0
- package/dist/index.d.ts +205 -0
- package/dist/index.js +2 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +2 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +34 -12
- package/readme.md +282 -0
- package/readme_cn.md +282 -0
- package/src/__benchmarks__/index.ts +3 -0
- package/src/__benchmarks__/multi-level.ts +40 -0
- package/src/__benchmarks__/sample.ts +40 -0
- package/src/__benchmarks__/wildcard.ts +41 -0
- package/src/__tests__/emit.test.ts +94 -0
- package/src/__tests__/emitAsync.test.ts +65 -0
- package/src/__tests__/isPathMatched.test.ts +205 -0
- package/src/__tests__/many.test.ts +23 -0
- package/src/__tests__/meta.test.ts +29 -0
- package/src/__tests__/off.test.ts +214 -0
- package/src/__tests__/onany.test.ts +173 -0
- package/src/__tests__/once.test.ts +71 -0
- package/src/__tests__/retain.test.ts +66 -0
- package/src/__tests__/scope.test.ts +111 -0
- package/src/__tests__/waitFor.test.ts +109 -0
- package/src/__tests__/wildcard.test.ts +186 -0
- package/src/event.ts +470 -5
- package/src/index.ts +3 -1
- package/src/scope.ts +88 -0
- package/src/types.ts +57 -0
- package/src/typestest.ts +102 -0
- package/src/utils/isPathMatched.ts +40 -0
- package/src/utils/removeItem.ts +16 -0
- package/tsconfig.json +112 -0
- package/tsup.config.ts +19 -0
|
@@ -0,0 +1,205 @@
|
|
|
1
|
+
import { describe, test, expect } from "vitest"
|
|
2
|
+
import { isPathMatched } from "../utils/isPathMatched";
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
describe("isPathMatched", ()=>{
|
|
6
|
+
test('Empty Path and Pattern Match', () => {
|
|
7
|
+
// Arrange
|
|
8
|
+
const path: string[] = [];
|
|
9
|
+
const pattern: string[] = [];
|
|
10
|
+
|
|
11
|
+
// Act
|
|
12
|
+
const result = isPathMatched(path, pattern);
|
|
13
|
+
|
|
14
|
+
// Assert
|
|
15
|
+
expect(result).toBe(true);
|
|
16
|
+
});
|
|
17
|
+
test('should match single element arrays with same value', () => {
|
|
18
|
+
// Arrange
|
|
19
|
+
const path = ["a"];
|
|
20
|
+
const pattern = ["a"];
|
|
21
|
+
|
|
22
|
+
// Act
|
|
23
|
+
const result = isPathMatched(path, pattern);
|
|
24
|
+
|
|
25
|
+
// Assert
|
|
26
|
+
expect(result).toBe(true);
|
|
27
|
+
});
|
|
28
|
+
test('should match wildcards', () => {
|
|
29
|
+
// Arrange
|
|
30
|
+
const path = ["*"];
|
|
31
|
+
const pattern = ["*"];
|
|
32
|
+
|
|
33
|
+
// Act
|
|
34
|
+
const result = isPathMatched(path, pattern);
|
|
35
|
+
|
|
36
|
+
// Assert
|
|
37
|
+
expect(result).toBe(true);
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
test('should match single wildcard pattern correctly', () => {
|
|
41
|
+
// Arrange
|
|
42
|
+
const path = ["anything"];
|
|
43
|
+
const pattern = ["*"];
|
|
44
|
+
|
|
45
|
+
// Act
|
|
46
|
+
const result = isPathMatched(path, pattern);
|
|
47
|
+
|
|
48
|
+
// Assert
|
|
49
|
+
expect(result).toBe(true);
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
test('should match multiple elements with exact values', () => {
|
|
53
|
+
// Arrange
|
|
54
|
+
const path = ["a", "b", "c"];
|
|
55
|
+
const pattern = ["a", "b", "c"];
|
|
56
|
+
|
|
57
|
+
// Act
|
|
58
|
+
const result = isPathMatched(path, pattern);
|
|
59
|
+
|
|
60
|
+
// Assert
|
|
61
|
+
expect(result).toBe(true);
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
test('should match path with multiple wildcards in pattern', () => {
|
|
65
|
+
// Arrange
|
|
66
|
+
const path = ["a", "b", "c"];
|
|
67
|
+
const pattern = ["*", "b", "*"];
|
|
68
|
+
|
|
69
|
+
// Act
|
|
70
|
+
const result = isPathMatched(path, pattern);
|
|
71
|
+
|
|
72
|
+
// Assert
|
|
73
|
+
expect(result).toBe(true);
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
test('should match path when pattern ends with double star', () => {
|
|
77
|
+
// Arrange
|
|
78
|
+
const path = ["a", "b", "c", "d"];
|
|
79
|
+
const pattern = ["a", "b", "**"];
|
|
80
|
+
|
|
81
|
+
// Act
|
|
82
|
+
const result = isPathMatched(path, pattern);
|
|
83
|
+
|
|
84
|
+
// Assert
|
|
85
|
+
expect(result).toBe(true);
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
test('should return false when arrays have different lengths without double star pattern', () => {
|
|
89
|
+
// Arrange
|
|
90
|
+
const path = ['a', 'b'];
|
|
91
|
+
const pattern = ['a'];
|
|
92
|
+
|
|
93
|
+
// Act
|
|
94
|
+
const result = isPathMatched(path, pattern);
|
|
95
|
+
|
|
96
|
+
// Assert
|
|
97
|
+
expect(result).toBe(false);
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
test('Double Star with Empty Remaining Path', () => {
|
|
101
|
+
// Arrange
|
|
102
|
+
const path = ["a", "b"];
|
|
103
|
+
const pattern = ["a", "b", "**"];
|
|
104
|
+
|
|
105
|
+
// Act
|
|
106
|
+
const result = isPathMatched(path, pattern);
|
|
107
|
+
|
|
108
|
+
// Assert
|
|
109
|
+
expect(result).toBe(true);
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
test('should match pattern with mixed * and ** wildcards', () => {
|
|
113
|
+
// Arrange
|
|
114
|
+
const path = ["a", "b", "c", "d"];
|
|
115
|
+
const pattern = ["*", "b", "**"];
|
|
116
|
+
|
|
117
|
+
// Act
|
|
118
|
+
const result = isPathMatched(path, pattern);
|
|
119
|
+
|
|
120
|
+
// Assert
|
|
121
|
+
expect(result).toBe(true);
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
test('should match path partially when pattern ends with double star', () => {
|
|
125
|
+
// Arrange
|
|
126
|
+
const path = ["x", "y"];
|
|
127
|
+
const pattern = ["x", "**"];
|
|
128
|
+
|
|
129
|
+
// Act
|
|
130
|
+
const result = isPathMatched(path, pattern);
|
|
131
|
+
|
|
132
|
+
// Assert
|
|
133
|
+
expect(result).toBe(true);
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
test('should match single element path with double star pattern', () => {
|
|
137
|
+
// Arrange
|
|
138
|
+
const path = ["x"];
|
|
139
|
+
const pattern = ["**"];
|
|
140
|
+
|
|
141
|
+
// Act
|
|
142
|
+
const result = isPathMatched(path, pattern);
|
|
143
|
+
|
|
144
|
+
// Assert
|
|
145
|
+
expect(result).toBe(true);
|
|
146
|
+
});
|
|
147
|
+
|
|
148
|
+
|
|
149
|
+
test('should return false when pattern length exceeds path length', () => {
|
|
150
|
+
// Arrange
|
|
151
|
+
const path = ["a"];
|
|
152
|
+
const pattern = ["a", "b"];
|
|
153
|
+
|
|
154
|
+
// Act
|
|
155
|
+
const result = isPathMatched(path, pattern);
|
|
156
|
+
|
|
157
|
+
// Assert
|
|
158
|
+
expect(result).toBe(false);
|
|
159
|
+
});
|
|
160
|
+
|
|
161
|
+
test('should match path with special characters', () => {
|
|
162
|
+
// Arrange
|
|
163
|
+
const path = ['$', '#', '@'];
|
|
164
|
+
const pattern = ['$', '*', '@'];
|
|
165
|
+
|
|
166
|
+
// Act
|
|
167
|
+
const result = isPathMatched(path, pattern);
|
|
168
|
+
|
|
169
|
+
// Assert
|
|
170
|
+
expect(result).toBe(true);
|
|
171
|
+
});
|
|
172
|
+
|
|
173
|
+
test('should return false when double star pattern is in middle of pattern array', () => {
|
|
174
|
+
// Arrange
|
|
175
|
+
const path = ["a", "b", "c"];
|
|
176
|
+
const pattern = ["a", "**", "c"];
|
|
177
|
+
|
|
178
|
+
// Act
|
|
179
|
+
const result = isPathMatched(path, pattern);
|
|
180
|
+
|
|
181
|
+
// Assert
|
|
182
|
+
expect(result).toBe(false);
|
|
183
|
+
});
|
|
184
|
+
|
|
185
|
+
test('should match path with consecutive wildcards in pattern', () => {
|
|
186
|
+
// Arrange
|
|
187
|
+
const path = ["a", "b", "c"];
|
|
188
|
+
const pattern = ["*", "*", "c"];
|
|
189
|
+
|
|
190
|
+
// Act
|
|
191
|
+
const result = isPathMatched(path, pattern);
|
|
192
|
+
|
|
193
|
+
// Assert
|
|
194
|
+
expect(result).toBe(true);
|
|
195
|
+
});
|
|
196
|
+
|
|
197
|
+
test("多路匹配路径",()=>{
|
|
198
|
+
expect(isPathMatched(["a","b","c","d","e"],["**"])).toBeTruthy()
|
|
199
|
+
expect(isPathMatched(["a","b","c","d","e"],["a","**"]) ).toBeTruthy()
|
|
200
|
+
expect(isPathMatched(["a","b","c","d","e"],["a","b","**"]) ).toBeTruthy()
|
|
201
|
+
expect(isPathMatched(["a","b","c","d","e"],["a","b","c","**"])).toBeTruthy()
|
|
202
|
+
expect(isPathMatched(["a","b","c","d","e"],["a","b","c","d","**"])).toBeTruthy()
|
|
203
|
+
})
|
|
204
|
+
|
|
205
|
+
})
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { describe, test, expect } from "vitest"
|
|
2
|
+
import { FastEvent } from "../event"
|
|
3
|
+
|
|
4
|
+
describe("指定次数事件的发布与订阅",async ()=>{
|
|
5
|
+
test("简单发布只订阅一次事件",()=>{
|
|
6
|
+
const emitter = new FastEvent()
|
|
7
|
+
const events:string[] =[]
|
|
8
|
+
emitter.on("x",(payload,{type})=>{
|
|
9
|
+
expect(type).toBe("x")
|
|
10
|
+
expect(payload).toBe(1)
|
|
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
|
+
|
|
20
|
+
})
|
|
21
|
+
|
|
22
|
+
})
|
|
23
|
+
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { describe, test, expect } from "vitest"
|
|
2
|
+
import { FastEvent } from "../event"
|
|
3
|
+
|
|
4
|
+
describe("传递Meta数据",async ()=>{
|
|
5
|
+
test("全局meta数据",()=>{
|
|
6
|
+
const emitter = new FastEvent({
|
|
7
|
+
meta:{ a:1 }
|
|
8
|
+
})
|
|
9
|
+
emitter.on("x",(payload,{type,a})=>{
|
|
10
|
+
expect(type).toBe("x")
|
|
11
|
+
expect(payload).toBe(1)
|
|
12
|
+
expect(a).toBe(1)
|
|
13
|
+
})
|
|
14
|
+
emitter.emit("x",1)
|
|
15
|
+
})
|
|
16
|
+
test("emit时传递meta数据",()=>{
|
|
17
|
+
const emitter = new FastEvent({
|
|
18
|
+
meta:{ a:1,b: 2 }
|
|
19
|
+
})
|
|
20
|
+
emitter.on("x",(payload,{type,a,b})=>{
|
|
21
|
+
expect(type).toBe("x")
|
|
22
|
+
expect(payload).toBe(1)
|
|
23
|
+
expect(a).toBe(10)
|
|
24
|
+
expect(b).toBe(20)
|
|
25
|
+
})
|
|
26
|
+
emitter.emit("x",1,false,{a:10,b:20})
|
|
27
|
+
})
|
|
28
|
+
})
|
|
29
|
+
|
|
@@ -0,0 +1,214 @@
|
|
|
1
|
+
import { describe, test, expect } from "vitest"
|
|
2
|
+
import { FastEvent } from "../event"
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
describe("退订事件", ()=>{
|
|
6
|
+
test("基本退订事件",()=>{
|
|
7
|
+
const emitter = new FastEvent()
|
|
8
|
+
const events:string[] =[]
|
|
9
|
+
const subscriber = emitter.on("x",(payload,{type})=>{
|
|
10
|
+
expect(type).toBe("x")
|
|
11
|
+
expect(payload).toBe(1)
|
|
12
|
+
events.push(type)
|
|
13
|
+
})
|
|
14
|
+
emitter.emit("x",1)
|
|
15
|
+
subscriber.off()
|
|
16
|
+
emitter.emit("x",1)
|
|
17
|
+
expect(events).toEqual(["x"])
|
|
18
|
+
})
|
|
19
|
+
test("根据事件类型和侦听器进行退订",()=>{
|
|
20
|
+
const emitter = new FastEvent()
|
|
21
|
+
const events:string[] =[]
|
|
22
|
+
const listener =(payload:any,{type}:{type:string})=>{
|
|
23
|
+
expect(type).toBe("x")
|
|
24
|
+
expect(payload).toBe(1)
|
|
25
|
+
events.push(type)
|
|
26
|
+
}
|
|
27
|
+
emitter.on("x",listener)
|
|
28
|
+
emitter.emit("x",1)
|
|
29
|
+
emitter.off("x",listener)
|
|
30
|
+
emitter.emit("x",1)
|
|
31
|
+
expect(events).toEqual(["x"])
|
|
32
|
+
})
|
|
33
|
+
test("多级事件根据事件类型和侦听器进行退订",()=>{
|
|
34
|
+
const emitter = new FastEvent()
|
|
35
|
+
const events:string[] =[]
|
|
36
|
+
const listener =(payload:any,{type}:{type:string})=>{
|
|
37
|
+
expect(type).toBe("a/b/c/d")
|
|
38
|
+
expect(payload).toBe(1)
|
|
39
|
+
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)
|
|
47
|
+
expect(events).toEqual(["a/b/c/d"])
|
|
48
|
+
})
|
|
49
|
+
|
|
50
|
+
test("通配符事件退订",()=>{
|
|
51
|
+
const emitter = new FastEvent()
|
|
52
|
+
const events:string[] =[]
|
|
53
|
+
const listener =(payload:any,{type}:{type:string})=>{
|
|
54
|
+
expect(type).toBe("a/b/c/d")
|
|
55
|
+
expect(payload).toBe(1)
|
|
56
|
+
events.push(type)
|
|
57
|
+
}
|
|
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)
|
|
64
|
+
expect(events).toEqual(["a/b/c/d"])
|
|
65
|
+
})
|
|
66
|
+
|
|
67
|
+
test("退订指定的侦听器",()=>{
|
|
68
|
+
const emitter = new FastEvent()
|
|
69
|
+
const events:string[] =[]
|
|
70
|
+
const listener =( payload:any,{type}:{type:string} )=>{
|
|
71
|
+
expect(type).toBe("a/b/c/d")
|
|
72
|
+
expect(payload).toBe(1)
|
|
73
|
+
events.push(type)
|
|
74
|
+
}
|
|
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"])
|
|
86
|
+
})
|
|
87
|
+
test("退订指定的事件时不指定侦听器",()=>{
|
|
88
|
+
const emitter = new FastEvent()
|
|
89
|
+
const events:string[] =[]
|
|
90
|
+
const listener =( payload:any,{type}:{type:string} )=>{
|
|
91
|
+
events.push(type)
|
|
92
|
+
}
|
|
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"])
|
|
107
|
+
})
|
|
108
|
+
test("退订所有侦听器",()=>{
|
|
109
|
+
const emitter = new FastEvent()
|
|
110
|
+
const events:string[] =[]
|
|
111
|
+
const listener =( payload:any,{type}:{type:string} )=>{
|
|
112
|
+
events.push(type)
|
|
113
|
+
}
|
|
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)
|
|
122
|
+
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"])
|
|
132
|
+
})
|
|
133
|
+
test("退订含通配符的事件",()=>{
|
|
134
|
+
const emitter = new FastEvent()
|
|
135
|
+
const events:string[] =[]
|
|
136
|
+
const listener =( payload:any,{type}:{type:string} )=>{
|
|
137
|
+
events.push(type)
|
|
138
|
+
}
|
|
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)
|
|
147
|
+
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"])
|
|
157
|
+
})
|
|
158
|
+
test("退订含通配符的事件",()=>{
|
|
159
|
+
const emitter = new FastEvent()
|
|
160
|
+
const events:string[] =[]
|
|
161
|
+
const listener =( payload:any,{type}:{type:string} )=>{
|
|
162
|
+
events.push(type)
|
|
163
|
+
}
|
|
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)
|
|
172
|
+
emitter.off("a/*")
|
|
173
|
+
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"])
|
|
179
|
+
})
|
|
180
|
+
test("退订多层含通配符的事件",()=>{
|
|
181
|
+
const emitter = new FastEvent()
|
|
182
|
+
const events:string[] =[]
|
|
183
|
+
const listener =( payload:any,{type}:{type:string} )=>{
|
|
184
|
+
events.push(type)
|
|
185
|
+
}
|
|
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)
|
|
191
|
+
|
|
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)
|
|
197
|
+
|
|
198
|
+
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)
|
|
209
|
+
|
|
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
|
+
})
|
|
212
|
+
})
|
|
213
|
+
|
|
214
|
+
|
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
import { describe, test, expect } from "vitest"
|
|
2
|
+
import { FastEvent } from "../event"
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
describe("订阅所有事件", ()=>{
|
|
6
|
+
test("订阅所有简单事件",()=>{
|
|
7
|
+
const emitter = new FastEvent()
|
|
8
|
+
const allEvents:string[] =[]
|
|
9
|
+
const allValues:number[]=[]
|
|
10
|
+
const events:string[] =[]
|
|
11
|
+
const values:number[]=[]
|
|
12
|
+
emitter.onAny((payload,{type})=>{
|
|
13
|
+
allEvents.push(type)
|
|
14
|
+
allValues.push(payload)
|
|
15
|
+
})
|
|
16
|
+
emitter.on("a",(payload,{type})=>{
|
|
17
|
+
expect(type).toBe("a")
|
|
18
|
+
values.push(payload)
|
|
19
|
+
events.push(type)
|
|
20
|
+
})
|
|
21
|
+
emitter.on("b",(payload,{type})=>{
|
|
22
|
+
expect(type).toBe("b")
|
|
23
|
+
values.push(payload)
|
|
24
|
+
events.push(type)
|
|
25
|
+
})
|
|
26
|
+
emitter.on("c",(payload,{type})=>{
|
|
27
|
+
expect(type).toBe("c")
|
|
28
|
+
values.push(payload)
|
|
29
|
+
events.push(type)
|
|
30
|
+
})
|
|
31
|
+
emitter.on("d",(payload,{type})=>{
|
|
32
|
+
expect(type).toBe("d")
|
|
33
|
+
values.push(payload)
|
|
34
|
+
events.push(type)
|
|
35
|
+
})
|
|
36
|
+
emitter.emit("a",1)
|
|
37
|
+
emitter.emit("b",2)
|
|
38
|
+
emitter.emit("c",3)
|
|
39
|
+
emitter.emit("d",4)
|
|
40
|
+
|
|
41
|
+
expect(events).toEqual(["a","b","c","d"])
|
|
42
|
+
expect(values).toEqual([1,2,3,4])
|
|
43
|
+
expect(allEvents).toEqual(["a","b","c","d"])
|
|
44
|
+
expect(allValues).toEqual([1,2,3,4])
|
|
45
|
+
|
|
46
|
+
})
|
|
47
|
+
test("通过on订阅所有简单事件",()=>{
|
|
48
|
+
const emitter = new FastEvent()
|
|
49
|
+
const allEvents:string[] =[]
|
|
50
|
+
const allValues:number[]=[]
|
|
51
|
+
const events:string[] =[]
|
|
52
|
+
const values:number[]=[]
|
|
53
|
+
emitter.on("**",(payload,{type})=>{
|
|
54
|
+
allEvents.push(type)
|
|
55
|
+
allValues.push(payload)
|
|
56
|
+
})
|
|
57
|
+
emitter.on("a",(payload,{type})=>{
|
|
58
|
+
expect(type).toBe("a")
|
|
59
|
+
values.push(payload)
|
|
60
|
+
events.push(type)
|
|
61
|
+
})
|
|
62
|
+
emitter.on("b",(payload,{type})=>{
|
|
63
|
+
expect(type).toBe("b")
|
|
64
|
+
values.push(payload)
|
|
65
|
+
events.push(type)
|
|
66
|
+
})
|
|
67
|
+
emitter.on("c",(payload,{type})=>{
|
|
68
|
+
expect(type).toBe("c")
|
|
69
|
+
values.push(payload)
|
|
70
|
+
events.push(type)
|
|
71
|
+
})
|
|
72
|
+
emitter.on("d",(payload,{type})=>{
|
|
73
|
+
expect(type).toBe("d")
|
|
74
|
+
values.push(payload)
|
|
75
|
+
events.push(type)
|
|
76
|
+
})
|
|
77
|
+
emitter.emit("a",1)
|
|
78
|
+
emitter.emit("b",2)
|
|
79
|
+
emitter.emit("c",3)
|
|
80
|
+
emitter.emit("d",4)
|
|
81
|
+
|
|
82
|
+
expect(events).toEqual(["a","b","c","d"])
|
|
83
|
+
expect(values).toEqual([1,2,3,4])
|
|
84
|
+
expect(allEvents).toEqual(["a","b","c","d"])
|
|
85
|
+
expect(allValues).toEqual([1,2,3,4])
|
|
86
|
+
|
|
87
|
+
})
|
|
88
|
+
test("订阅所有简单事件",()=>{
|
|
89
|
+
const emitter = new FastEvent()
|
|
90
|
+
const allEvents:string[] =[]
|
|
91
|
+
const allValues:number[]=[]
|
|
92
|
+
const events:string[] =[]
|
|
93
|
+
const values:number[]=[]
|
|
94
|
+
emitter.onAny((payload,{type})=>{
|
|
95
|
+
allEvents.push(type)
|
|
96
|
+
allValues.push(payload)
|
|
97
|
+
})
|
|
98
|
+
emitter.on("a.x.y",(payload,{type})=>{
|
|
99
|
+
expect(type).toBe("a.x.y")
|
|
100
|
+
values.push(payload)
|
|
101
|
+
events.push(type)
|
|
102
|
+
})
|
|
103
|
+
emitter.on("b.m.n",(payload,{type})=>{
|
|
104
|
+
expect(type).toBe("b.m.n")
|
|
105
|
+
values.push(payload)
|
|
106
|
+
events.push(type)
|
|
107
|
+
})
|
|
108
|
+
emitter.on("c.o.p",(payload,{type})=>{
|
|
109
|
+
expect(type).toBe("c.o.p")
|
|
110
|
+
values.push(payload)
|
|
111
|
+
events.push(type)
|
|
112
|
+
})
|
|
113
|
+
emitter.on("d.q.r",(payload,{type})=>{
|
|
114
|
+
expect(type).toBe("d.q.r")
|
|
115
|
+
values.push(payload)
|
|
116
|
+
events.push(type)
|
|
117
|
+
})
|
|
118
|
+
emitter.emit("a.x.y",1)
|
|
119
|
+
emitter.emit("b.m.n",2)
|
|
120
|
+
emitter.emit("c.o.p",3)
|
|
121
|
+
emitter.emit("d.q.r",4)
|
|
122
|
+
|
|
123
|
+
expect(events).toEqual(["a.x.y","b.m.n","c.o.p","d.q.r"])
|
|
124
|
+
expect(values).toEqual([1,2,3,4])
|
|
125
|
+
expect(allEvents).toEqual(["a.x.y","b.m.n","c.o.p","d.q.r"])
|
|
126
|
+
expect(allValues).toEqual([1,2,3,4])
|
|
127
|
+
|
|
128
|
+
})
|
|
129
|
+
test("取消订阅所有简单事件",()=>{
|
|
130
|
+
const emitter = new FastEvent()
|
|
131
|
+
const allEvents:string[] =[]
|
|
132
|
+
const allValues:number[]=[]
|
|
133
|
+
const events:string[] =[]
|
|
134
|
+
const values:number[]=[]
|
|
135
|
+
const subscriber = emitter.onAny((payload,{type})=>{
|
|
136
|
+
allEvents.push(type)
|
|
137
|
+
allValues.push(payload)
|
|
138
|
+
})
|
|
139
|
+
emitter.on("a",(payload,{type})=>{
|
|
140
|
+
expect(type).toBe("a")
|
|
141
|
+
values.push(payload)
|
|
142
|
+
events.push(type)
|
|
143
|
+
})
|
|
144
|
+
emitter.on("b",(payload,{type})=>{
|
|
145
|
+
expect(type).toBe("b")
|
|
146
|
+
values.push(payload)
|
|
147
|
+
events.push(type)
|
|
148
|
+
})
|
|
149
|
+
emitter.on("c",(payload,{type})=>{
|
|
150
|
+
expect(type).toBe("c")
|
|
151
|
+
values.push(payload)
|
|
152
|
+
events.push(type)
|
|
153
|
+
})
|
|
154
|
+
emitter.on("d",(payload,{type})=>{
|
|
155
|
+
expect(type).toBe("d")
|
|
156
|
+
values.push(payload)
|
|
157
|
+
events.push(type)
|
|
158
|
+
})
|
|
159
|
+
emitter.emit("a",1)
|
|
160
|
+
subscriber.off()
|
|
161
|
+
emitter.emit("b",2)
|
|
162
|
+
emitter.emit("c",3)
|
|
163
|
+
emitter.emit("d",4)
|
|
164
|
+
|
|
165
|
+
expect(events).toEqual(["a","b","c","d"])
|
|
166
|
+
expect(values).toEqual([1,2,3,4])
|
|
167
|
+
expect(allEvents).toEqual(["a"])
|
|
168
|
+
expect(allValues).toEqual([1])
|
|
169
|
+
})
|
|
170
|
+
|
|
171
|
+
})
|
|
172
|
+
|
|
173
|
+
|