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
|
@@ -2,185 +2,184 @@ import { describe, test, expect } from "vitest"
|
|
|
2
2
|
import { FastEvent } from "../event"
|
|
3
3
|
import { FastEventSubscriber } from "../types"
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
describe("基于通配符的发布与订阅",async ()=>{
|
|
7
|
-
|
|
8
|
-
test("使用通配符发布订阅层级事件",()=>{
|
|
9
|
-
const emitter = new FastEvent()
|
|
10
|
-
const events:string[]=[]
|
|
11
|
-
const subscribers:FastEventSubscriber[]=[]
|
|
12
|
-
subscribers.push(emitter.on("*/*/*",(payload,
|
|
5
|
+
|
|
6
|
+
describe("基于通配符的发布与订阅", async () => {
|
|
7
|
+
|
|
8
|
+
test("使用通配符发布订阅层级事件", () => {
|
|
9
|
+
const emitter = new FastEvent()
|
|
10
|
+
const events: string[] = []
|
|
11
|
+
const subscribers: FastEventSubscriber[] = []
|
|
12
|
+
subscribers.push(emitter.on("*/*/*", ({ payload, type }) => {
|
|
13
13
|
expect(type).toBe("a/b/c")
|
|
14
14
|
expect(payload).toBe(1)
|
|
15
15
|
events.push(type)
|
|
16
16
|
}))
|
|
17
|
-
subscribers.push(emitter.on("a/*/*",(payload,
|
|
17
|
+
subscribers.push(emitter.on("a/*/*", ({ payload, type }) => {
|
|
18
18
|
expect(type).toBe("a/b/c")
|
|
19
19
|
expect(payload).toBe(1)
|
|
20
20
|
events.push(type)
|
|
21
21
|
}))
|
|
22
|
-
subscribers.push(emitter.on("a/b/*",(payload,
|
|
22
|
+
subscribers.push(emitter.on("a/b/*", ({ payload, type }) => {
|
|
23
23
|
expect(type).toBe("a/b/c")
|
|
24
24
|
expect(payload).toBe(1)
|
|
25
25
|
events.push(type)
|
|
26
26
|
}))
|
|
27
|
-
emitter.emit("a/b/c",1)
|
|
28
|
-
expect(events).toEqual(["a/b/c","a/b/c","a/b/c"])
|
|
29
|
-
emitter.emit("a/b/c",1)
|
|
30
|
-
expect(events).toEqual(["a/b/c","a/b/c","a/b/c","a/b/c","a/b/c","a/b/c"])
|
|
31
|
-
subscribers.forEach(subscriber=>subscriber.off())
|
|
32
|
-
expect(events).toEqual(["a/b/c","a/b/c","a/b/c","a/b/c","a/b/c","a/b/c"])
|
|
27
|
+
emitter.emit("a/b/c", 1)
|
|
28
|
+
expect(events).toEqual(["a/b/c", "a/b/c", "a/b/c"])
|
|
29
|
+
emitter.emit("a/b/c", 1)
|
|
30
|
+
expect(events).toEqual(["a/b/c", "a/b/c", "a/b/c", "a/b/c", "a/b/c", "a/b/c"])
|
|
31
|
+
subscribers.forEach(subscriber => subscriber.off())
|
|
32
|
+
expect(events).toEqual(["a/b/c", "a/b/c", "a/b/c", "a/b/c", "a/b/c", "a/b/c"])
|
|
33
33
|
})
|
|
34
34
|
|
|
35
|
-
test("使用通配符发布订阅层级事件11",()=>{
|
|
36
|
-
const emitter = new FastEvent()
|
|
37
|
-
const events:string[]=[]
|
|
38
|
-
const subscribers:FastEventSubscriber[]=[]
|
|
39
|
-
subscribers.push(emitter.on("*/*/*",(payload,
|
|
35
|
+
test("使用通配符发布订阅层级事件11", () => {
|
|
36
|
+
const emitter = new FastEvent()
|
|
37
|
+
const events: string[] = []
|
|
38
|
+
const subscribers: FastEventSubscriber[] = []
|
|
39
|
+
subscribers.push(emitter.on("*/*/*", ({ payload, type }) => {
|
|
40
40
|
expect(type).toBe("a/b/c")
|
|
41
41
|
expect(payload).toBe(1)
|
|
42
42
|
events.push(type)
|
|
43
|
-
}))
|
|
44
|
-
emitter.emit("a/b/c",1)
|
|
45
|
-
expect(events).toEqual(["a/b/c"])
|
|
43
|
+
}))
|
|
44
|
+
emitter.emit("a/b/c", 1)
|
|
45
|
+
expect(events).toEqual(["a/b/c"])
|
|
46
46
|
})
|
|
47
47
|
|
|
48
48
|
|
|
49
|
-
test("使用通配符发布订阅层级事件12",()=>{
|
|
50
|
-
const emitter = new FastEvent()
|
|
51
|
-
const events:string[]=[]
|
|
52
|
-
const subscribers:FastEventSubscriber[]=[]
|
|
53
|
-
subscribers.push(emitter.on("a/*/*",(payload,
|
|
49
|
+
test("使用通配符发布订阅层级事件12", () => {
|
|
50
|
+
const emitter = new FastEvent()
|
|
51
|
+
const events: string[] = []
|
|
52
|
+
const subscribers: FastEventSubscriber[] = []
|
|
53
|
+
subscribers.push(emitter.on("a/*/*", ({ payload, type }) => {
|
|
54
54
|
expect(type).toBe("a/b/c")
|
|
55
55
|
expect(payload).toBe(1)
|
|
56
56
|
events.push(type)
|
|
57
|
-
}))
|
|
58
|
-
emitter.emit("a/b/c",1)
|
|
59
|
-
expect(events).toEqual(["a/b/c"])
|
|
57
|
+
}))
|
|
58
|
+
emitter.emit("a/b/c", 1)
|
|
59
|
+
expect(events).toEqual(["a/b/c"])
|
|
60
60
|
})
|
|
61
|
-
test("使用通配符发布订阅层级事件13",()=>{
|
|
62
|
-
const emitter = new FastEvent()
|
|
63
|
-
const events:string[]=[]
|
|
64
|
-
const subscribers:FastEventSubscriber[]=[]
|
|
65
|
-
subscribers.push(emitter.on("a/b/*",(payload,
|
|
61
|
+
test("使用通配符发布订阅层级事件13", () => {
|
|
62
|
+
const emitter = new FastEvent()
|
|
63
|
+
const events: string[] = []
|
|
64
|
+
const subscribers: FastEventSubscriber[] = []
|
|
65
|
+
subscribers.push(emitter.on("a/b/*", ({ payload, type }) => {
|
|
66
66
|
expect(type).toBe("a/b/c")
|
|
67
67
|
expect(payload).toBe(1)
|
|
68
68
|
events.push(type)
|
|
69
|
-
}))
|
|
70
|
-
emitter.emit("a/b/c",1)
|
|
71
|
-
expect(events).toEqual(["a/b/c"])
|
|
69
|
+
}))
|
|
70
|
+
emitter.emit("a/b/c", 1)
|
|
71
|
+
expect(events).toEqual(["a/b/c"])
|
|
72
72
|
})
|
|
73
73
|
|
|
74
|
-
test("使用通配符发布订阅层级事件14",()=>{
|
|
75
|
-
const emitter = new FastEvent()
|
|
76
|
-
const events:string[]=[]
|
|
77
|
-
const subscribers:FastEventSubscriber[]=[]
|
|
78
|
-
subscribers.push(emitter.on("*/*/*",(payload,
|
|
74
|
+
test("使用通配符发布订阅层级事件14", () => {
|
|
75
|
+
const emitter = new FastEvent()
|
|
76
|
+
const events: string[] = []
|
|
77
|
+
const subscribers: FastEventSubscriber[] = []
|
|
78
|
+
subscribers.push(emitter.on("*/*/*", ({ payload, type }) => {
|
|
79
79
|
expect(type).toBe("a/b/c")
|
|
80
80
|
expect(payload).toBe(1)
|
|
81
81
|
events.push(type)
|
|
82
82
|
}))
|
|
83
|
-
subscribers.push(emitter.on("*/*/c",(payload,
|
|
83
|
+
subscribers.push(emitter.on("*/*/c", ({ payload, type }) => {
|
|
84
84
|
expect(type).toBe("a/b/c")
|
|
85
85
|
expect(payload).toBe(1)
|
|
86
86
|
events.push(type)
|
|
87
87
|
}))
|
|
88
|
-
subscribers.push(emitter.on("*/b/c",(payload,
|
|
88
|
+
subscribers.push(emitter.on("*/b/c", ({ payload, type }) => {
|
|
89
89
|
expect(type).toBe("a/b/c")
|
|
90
90
|
expect(payload).toBe(1)
|
|
91
91
|
events.push(type)
|
|
92
92
|
}))
|
|
93
|
-
emitter.emit("a/b/c",1)
|
|
94
|
-
expect(events).toEqual(["a/b/c","a/b/c","a/b/c"])
|
|
95
|
-
emitter.emit("a/b/c",1)
|
|
96
|
-
expect(events).toEqual(["a/b/c","a/b/c","a/b/c","a/b/c","a/b/c","a/b/c"])
|
|
97
|
-
subscribers.forEach(subscriber=>subscriber.off())
|
|
98
|
-
expect(events).toEqual(["a/b/c","a/b/c","a/b/c","a/b/c","a/b/c","a/b/c"])
|
|
93
|
+
emitter.emit("a/b/c", 1)
|
|
94
|
+
expect(events).toEqual(["a/b/c", "a/b/c", "a/b/c"])
|
|
95
|
+
emitter.emit("a/b/c", 1)
|
|
96
|
+
expect(events).toEqual(["a/b/c", "a/b/c", "a/b/c", "a/b/c", "a/b/c", "a/b/c"])
|
|
97
|
+
subscribers.forEach(subscriber => subscriber.off())
|
|
98
|
+
expect(events).toEqual(["a/b/c", "a/b/c", "a/b/c", "a/b/c", "a/b/c", "a/b/c"])
|
|
99
99
|
})
|
|
100
|
-
test("使用通配符发布订阅层级事件15",()=>{
|
|
101
|
-
const emitter = new FastEvent()
|
|
102
|
-
const events:string[]=[]
|
|
103
|
-
const subscribers:FastEventSubscriber[]=[]
|
|
104
|
-
subscribers.push(emitter.on("*/b/c",(payload,
|
|
100
|
+
test("使用通配符发布订阅层级事件15", () => {
|
|
101
|
+
const emitter = new FastEvent()
|
|
102
|
+
const events: string[] = []
|
|
103
|
+
const subscribers: FastEventSubscriber[] = []
|
|
104
|
+
subscribers.push(emitter.on("*/b/c", ({ payload, type }) => {
|
|
105
105
|
expect(type).toBe("a/b/c")
|
|
106
106
|
expect(payload).toBe(1)
|
|
107
107
|
events.push(type)
|
|
108
108
|
}))
|
|
109
|
-
emitter.emit("a/b/c",1)
|
|
110
|
-
expect(events).toEqual(["a/b/c"])
|
|
109
|
+
emitter.emit("a/b/c", 1)
|
|
110
|
+
expect(events).toEqual(["a/b/c"])
|
|
111
111
|
})
|
|
112
|
-
test("使用通配符发布订阅层级事件16",()=>{
|
|
113
|
-
const emitter = new FastEvent()
|
|
114
|
-
const events:string[]=[]
|
|
115
|
-
const subscribers:FastEventSubscriber[]=[]
|
|
116
|
-
subscribers.push(emitter.on("*/b/*/d/*/f",(payload,
|
|
112
|
+
test("使用通配符发布订阅层级事件16", () => {
|
|
113
|
+
const emitter = new FastEvent()
|
|
114
|
+
const events: string[] = []
|
|
115
|
+
const subscribers: FastEventSubscriber[] = []
|
|
116
|
+
subscribers.push(emitter.on("*/b/*/d/*/f", ({ payload, type }) => {
|
|
117
117
|
events.push(type)
|
|
118
118
|
}))
|
|
119
|
-
emitter.emit("1/b/1/d/1/f",1)
|
|
120
|
-
emitter.emit("2/b/2/d/2/f",1)
|
|
121
|
-
emitter.emit("3/b/3/d/3/f",1)
|
|
122
|
-
emitter.emit("4/b/4/d/4/f",1)
|
|
123
|
-
emitter.emit("5/b/5/d/5/f",1)
|
|
124
|
-
expect(events).toEqual(["1/b/1/d/1/f","2/b/2/d/2/f","3/b/3/d/3/f","4/b/4/d/4/f","5/b/5/d/5/f"])
|
|
119
|
+
emitter.emit("1/b/1/d/1/f", 1)
|
|
120
|
+
emitter.emit("2/b/2/d/2/f", 1)
|
|
121
|
+
emitter.emit("3/b/3/d/3/f", 1)
|
|
122
|
+
emitter.emit("4/b/4/d/4/f", 1)
|
|
123
|
+
emitter.emit("5/b/5/d/5/f", 1)
|
|
124
|
+
expect(events).toEqual(["1/b/1/d/1/f", "2/b/2/d/2/f", "3/b/3/d/3/f", "4/b/4/d/4/f", "5/b/5/d/5/f"])
|
|
125
125
|
})
|
|
126
|
-
test("使用通配符发布订阅层级事件17",()=>{
|
|
126
|
+
test("使用通配符发布订阅层级事件17", () => {
|
|
127
127
|
return new Promise<void>((resolve) => {
|
|
128
128
|
const emitter = new FastEvent()
|
|
129
129
|
emitter.on('a/b/c/*', () => {
|
|
130
130
|
resolve()
|
|
131
|
-
})
|
|
131
|
+
})
|
|
132
132
|
emitter.emit('a/b/c/x', 1)
|
|
133
133
|
emitter.emit('a/b/c/x', 1)
|
|
134
134
|
})
|
|
135
|
-
})
|
|
136
|
-
test("使用多级路径匹配",()=>{
|
|
137
|
-
const emitter = new FastEvent()
|
|
138
|
-
const payloads:number[]=[]
|
|
139
|
-
const events:string[]=[]
|
|
140
|
-
emitter.on("a/**",(payload,
|
|
135
|
+
})
|
|
136
|
+
test("使用多级路径匹配", () => {
|
|
137
|
+
const emitter = new FastEvent()
|
|
138
|
+
const payloads: number[] = []
|
|
139
|
+
const events: string[] = []
|
|
140
|
+
emitter.on("a/**", ({ payload, type }) => {
|
|
141
141
|
events.push(type)
|
|
142
|
-
payloads.push(payload)
|
|
143
|
-
})
|
|
144
|
-
emitter.emit("a/b/c/d/e/f",1)
|
|
142
|
+
payloads.push(payload)
|
|
143
|
+
})
|
|
144
|
+
emitter.emit("a/b/c/d/e/f", 1)
|
|
145
145
|
expect(events).toEqual(["a/b/c/d/e/f"])
|
|
146
|
-
expect(payloads).toEqual([1])
|
|
146
|
+
expect(payloads).toEqual([1])
|
|
147
147
|
})
|
|
148
|
-
test("使用多级路径匹配2",()=>{
|
|
149
|
-
const emitter = new FastEvent()
|
|
150
|
-
const payloads:number[]=[]
|
|
151
|
-
const events:string[]=[]
|
|
152
|
-
emitter.on("a/**",(payload,
|
|
148
|
+
test("使用多级路径匹配2", () => {
|
|
149
|
+
const emitter = new FastEvent()
|
|
150
|
+
const payloads: number[] = []
|
|
151
|
+
const events: string[] = []
|
|
152
|
+
emitter.on("a/**", ({ payload, type }) => {
|
|
153
153
|
events.push(type)
|
|
154
|
-
payloads.push(payload)
|
|
154
|
+
payloads.push(payload)
|
|
155
155
|
})
|
|
156
|
-
|
|
157
|
-
emitter.emit("a/b",1)
|
|
158
|
-
emitter.emit("a/b/c",2)
|
|
159
|
-
emitter.emit("a/b/c/d",3)
|
|
160
|
-
emitter.emit("a/b/c/d/e",4)
|
|
161
|
-
emitter.emit("a/b/c/d/e/f",5)
|
|
162
|
-
expect(events).toEqual(["a/b","a/b/c","a/b/c/d","a/b/c/d/e","a/b/c/d/e/f"])
|
|
163
|
-
expect(payloads).toEqual([1,2,3,4,5])
|
|
156
|
+
|
|
157
|
+
emitter.emit("a/b", 1)
|
|
158
|
+
emitter.emit("a/b/c", 2)
|
|
159
|
+
emitter.emit("a/b/c/d", 3)
|
|
160
|
+
emitter.emit("a/b/c/d/e", 4)
|
|
161
|
+
emitter.emit("a/b/c/d/e/f", 5)
|
|
162
|
+
expect(events).toEqual(["a/b", "a/b/c", "a/b/c/d", "a/b/c/d/e", "a/b/c/d/e/f"])
|
|
163
|
+
expect(payloads).toEqual([1, 2, 3, 4, 5])
|
|
164
164
|
})
|
|
165
|
-
test("使用多级路径匹配3",()=>{
|
|
166
|
-
const emitter = new FastEvent()
|
|
167
|
-
const payloads:number[]=[]
|
|
168
|
-
const events:string[]=[]
|
|
169
|
-
emitter.on("a/**",(payload,
|
|
165
|
+
test("使用多级路径匹配3", () => {
|
|
166
|
+
const emitter = new FastEvent()
|
|
167
|
+
const payloads: number[] = []
|
|
168
|
+
const events: string[] = []
|
|
169
|
+
emitter.on("a/**", ({ payload, type }) => {
|
|
170
170
|
events.push(type)
|
|
171
|
-
payloads.push(payload)
|
|
171
|
+
payloads.push(payload)
|
|
172
172
|
})
|
|
173
|
-
emitter.on("a/b/*",(payload,
|
|
173
|
+
emitter.on("a/b/*", ({ payload, type }) => {
|
|
174
174
|
events.push(type)
|
|
175
|
-
payloads.push(payload)
|
|
176
|
-
})
|
|
177
|
-
emitter.emit("a/b/c",1)
|
|
178
|
-
emitter.emit("a/b/c/d",2)
|
|
179
|
-
emitter.emit("a/b/c/d/e",3)
|
|
180
|
-
emitter.emit("a/b/c/d/e/f",4)
|
|
181
|
-
expect(events).toEqual(["a/b/c","a/b/c","a/b/c/d","a/b/c/d/e","a/b/c/d/e/f"])
|
|
182
|
-
expect(payloads).toEqual([1,1,2,3,4])
|
|
175
|
+
payloads.push(payload)
|
|
176
|
+
})
|
|
177
|
+
emitter.emit("a/b/c", 1)
|
|
178
|
+
emitter.emit("a/b/c/d", 2)
|
|
179
|
+
emitter.emit("a/b/c/d/e", 3)
|
|
180
|
+
emitter.emit("a/b/c/d/e/f", 4)
|
|
181
|
+
expect(events).toEqual(["a/b/c", "a/b/c", "a/b/c/d", "a/b/c/d/e", "a/b/c/d/e/f"])
|
|
182
|
+
expect(payloads).toEqual([1, 1, 2, 3, 4])
|
|
183
183
|
})
|
|
184
|
-
})
|
|
185
|
-
|
|
186
|
-
|
|
184
|
+
})
|
|
185
|
+
|