fastevent 1.1.3 → 2.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/dist/devTools.js +2 -2
- package/dist/devTools.js.map +1 -1
- package/dist/devTools.mjs +2 -2
- package/dist/devTools.mjs.map +1 -1
- package/dist/index.d.mts +304 -53
- package/dist/index.d.ts +304 -53
- 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 +64 -50
- package/readme.md +517 -50
- package/.changeset/README.md +0 -8
- package/.changeset/config.json +0 -11
- package/.github/workflows/publish.yaml +0 -50
- package/.prettierrc.js +0 -20
- package/.vscode/launch.json +0 -20
- package/.vscode/settings.json +0 -18
- package/CHANGELOG.md +0 -60
- package/LICENSE +0 -21
- package/bench.png +0 -0
- package/dist/devTools.d.mts +0 -543
- package/dist/devTools.d.ts +0 -543
- package/example/README.md +0 -54
- package/example/eslint.config.js +0 -28
- package/example/index.html +0 -13
- package/example/package.json +0 -29
- package/example/pnpm-lock.yaml +0 -2047
- package/example/public/vite.svg +0 -1
- package/example/src/App.css +0 -42
- package/example/src/App.tsx +0 -60
- package/example/src/assets/react.svg +0 -1
- package/example/src/index.css +0 -68
- package/example/src/main.tsx +0 -10
- package/example/src/myEvent.ts +0 -32
- package/example/src/vite-env.d.ts +0 -1
- package/example/tsconfig.app.json +0 -26
- package/example/tsconfig.json +0 -7
- package/example/tsconfig.node.json +0 -24
- package/example/vite.config.ts +0 -7
- package/packages/native/index.ts +0 -1
- package/packages/turbo/.zig-cache/h/271c82d991949fd7788fd5451f0ca834.txt +0 -0
- package/packages/turbo/.zig-cache/h/timestamp +0 -0
- package/packages/turbo/.zig-cache/o/ebd7ddab8ffe003267120d598aecce68/dependencies.zig +0 -2
- package/packages/turbo/.zig-cache/z/c8114b040daa461a9e2eabd0357554a4 +0 -0
- package/packages/turbo/build.zig +0 -60
- package/packages/turbo/examples/basic.zig +0 -107
- package/packages/turbo/src/event.zig +0 -251
- package/packages/turbo/src/index.zig +0 -70
- package/packages/turbo/src/scope.zig +0 -104
- package/packages/turbo/src/types.zig +0 -88
- package/packages/turbo/src/utils.zig +0 -171
- package/readme_cn.md +0 -491
- package/src/__benchmarks__/index.ts +0 -3
- package/src/__benchmarks__/multi-level.ts +0 -40
- package/src/__benchmarks__/sample.ts +0 -40
- package/src/__benchmarks__/wildcard.ts +0 -41
- package/src/__tests__/emit.test.ts +0 -106
- package/src/__tests__/emitAsync.test.ts +0 -64
- package/src/__tests__/isPathMatched.test.ts +0 -205
- package/src/__tests__/many.test.ts +0 -22
- package/src/__tests__/meta.test.ts +0 -28
- package/src/__tests__/off.test.ts +0 -214
- package/src/__tests__/onany.test.ts +0 -212
- package/src/__tests__/once.test.ts +0 -70
- package/src/__tests__/retain.test.ts +0 -66
- package/src/__tests__/scope.test.ts +0 -110
- package/src/__tests__/types.test.ts +0 -145
- package/src/__tests__/waitFor.test.ts +0 -116
- package/src/__tests__/wildcard.test.ts +0 -185
- package/src/devTools.ts +0 -166
- package/src/event.ts +0 -741
- package/src/index.ts +0 -3
- package/src/scope.ts +0 -130
- package/src/types.ts +0 -66
- package/src/utils/WeakObjectMap.ts +0 -64
- package/src/utils/isPathMatched.ts +0 -40
- package/src/utils/removeItem.ts +0 -16
- package/tsconfig.json +0 -104
- package/tsup.config.ts +0 -30
|
@@ -1,212 +0,0 @@
|
|
|
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
|
-
test("通过onAny订阅所有简单事件", () => {
|
|
171
|
-
const emitter = new FastEvent()
|
|
172
|
-
const allEvents: string[] = []
|
|
173
|
-
const allValues: number[] = []
|
|
174
|
-
const events: string[] = []
|
|
175
|
-
const values: number[] = []
|
|
176
|
-
emitter.onAny(({ payload, type }) => {
|
|
177
|
-
allEvents.push(type)
|
|
178
|
-
allValues.push(payload)
|
|
179
|
-
})
|
|
180
|
-
emitter.on("a", ({ payload, type }) => {
|
|
181
|
-
expect(type).toBe("a")
|
|
182
|
-
values.push(payload)
|
|
183
|
-
events.push(type)
|
|
184
|
-
})
|
|
185
|
-
emitter.on("b", ({ payload, type }) => {
|
|
186
|
-
expect(type).toBe("b")
|
|
187
|
-
values.push(payload)
|
|
188
|
-
events.push(type)
|
|
189
|
-
})
|
|
190
|
-
emitter.on("c", ({ payload, type }) => {
|
|
191
|
-
expect(type).toBe("c")
|
|
192
|
-
values.push(payload)
|
|
193
|
-
events.push(type)
|
|
194
|
-
})
|
|
195
|
-
emitter.on("d", ({ payload, type }) => {
|
|
196
|
-
expect(type).toBe("d")
|
|
197
|
-
values.push(payload)
|
|
198
|
-
events.push(type)
|
|
199
|
-
})
|
|
200
|
-
emitter.emit("a", 1)
|
|
201
|
-
emitter.emit("b", 2)
|
|
202
|
-
emitter.emit("c", 3)
|
|
203
|
-
emitter.emit("d", 4)
|
|
204
|
-
|
|
205
|
-
expect(events).toEqual(["a", "b", "c", "d"])
|
|
206
|
-
expect(values).toEqual([1, 2, 3, 4])
|
|
207
|
-
expect(allEvents).toEqual(["a", "b", "c", "d"])
|
|
208
|
-
expect(allValues).toEqual([1, 2, 3, 4])
|
|
209
|
-
|
|
210
|
-
})
|
|
211
|
-
})
|
|
212
|
-
|
|
@@ -1,70 +0,0 @@
|
|
|
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.once("x", ({ payload, type }) => {
|
|
9
|
-
expect(type).toBe("x")
|
|
10
|
-
expect(payload).toBe(1)
|
|
11
|
-
events.push(type)
|
|
12
|
-
})
|
|
13
|
-
emitter.emit("x", 1)
|
|
14
|
-
emitter.emit("x", 1)
|
|
15
|
-
emitter.emit("x", 1)
|
|
16
|
-
emitter.emit("x", 1)
|
|
17
|
-
expect(events).toEqual(["x"])
|
|
18
|
-
})
|
|
19
|
-
test("简单发布只订阅一次事件后取消", () => {
|
|
20
|
-
const emitter = new FastEvent()
|
|
21
|
-
const events: string[] = []
|
|
22
|
-
const subscriber = emitter.once("x", ({ payload, type }) => {
|
|
23
|
-
expect(type).toBe("x")
|
|
24
|
-
expect(payload).toBe(1)
|
|
25
|
-
events.push(type)
|
|
26
|
-
})
|
|
27
|
-
subscriber.off()
|
|
28
|
-
emitter.emit("x", 1)
|
|
29
|
-
emitter.emit("x", 1)
|
|
30
|
-
emitter.emit("x", 1)
|
|
31
|
-
expect(events).toEqual([])
|
|
32
|
-
})
|
|
33
|
-
test("简单发布只订阅一次的多级事件", () => {
|
|
34
|
-
const emitter = new FastEvent()
|
|
35
|
-
const events: string[] = []
|
|
36
|
-
emitter.once("a.b.c.d", ({ payload, type }) => {
|
|
37
|
-
expect(type).toBe("a.b.c.d")
|
|
38
|
-
expect(payload).toBe(1)
|
|
39
|
-
events.push(type)
|
|
40
|
-
})
|
|
41
|
-
emitter.emit("a.b.c.d", 1)
|
|
42
|
-
emitter.emit("a.b.c.d", 2)
|
|
43
|
-
emitter.emit("a.b.c.d", 3)
|
|
44
|
-
emitter.emit("a.b.c.d", 4)
|
|
45
|
-
expect(events).toEqual(["a.b.c.d"])
|
|
46
|
-
})
|
|
47
|
-
test("混合发布只订阅一次的多级事件", () => {
|
|
48
|
-
const emitter = new FastEvent()
|
|
49
|
-
const events: string[] = []
|
|
50
|
-
const values: number[] = []
|
|
51
|
-
emitter.once("a.b.c.d", ({ payload, type }) => {
|
|
52
|
-
expect(type).toBe("a.b.c.d")
|
|
53
|
-
values.push(payload)
|
|
54
|
-
events.push(type)
|
|
55
|
-
})
|
|
56
|
-
emitter.on("a.b.c.d", ({ payload, type }) => {
|
|
57
|
-
expect(type).toBe("a.b.c.d")
|
|
58
|
-
values.push(payload)
|
|
59
|
-
events.push(type)
|
|
60
|
-
})
|
|
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
|
-
emitter.emit("a.b.c.d", 4)
|
|
65
|
-
expect(events).toEqual(["a.b.c.d", "a.b.c.d", "a.b.c.d", "a.b.c.d", "a.b.c.d"])
|
|
66
|
-
expect(values).toEqual([1, 1, 2, 3, 4])
|
|
67
|
-
})
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
})
|
|
@@ -1,66 +0,0 @@
|
|
|
1
|
-
import { describe, test, expect } from "vitest"
|
|
2
|
-
import { FastEvent } from "../event"
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
describe("订阅与发布retain事件", async () => {
|
|
7
|
-
test("简单发布订阅retain事件", () => {
|
|
8
|
-
const emitter = new FastEvent()
|
|
9
|
-
emitter.emit("x", 1, true)
|
|
10
|
-
emitter.on("x", ({ payload, type }) => {
|
|
11
|
-
expect(type).toBe("x")
|
|
12
|
-
expect(payload).toBe(1)
|
|
13
|
-
})
|
|
14
|
-
emitter.emit("a.b.c1", 1, true)
|
|
15
|
-
emitter.emit("a.b.c2", 2, true)
|
|
16
|
-
emitter.on("a.b.c1", ({ payload, type }) => {
|
|
17
|
-
expect(type).toBe("a.b.c1")
|
|
18
|
-
expect(payload).toBe(1)
|
|
19
|
-
})
|
|
20
|
-
emitter.on("a.b.c2", ({ payload, type }) => {
|
|
21
|
-
expect(type).toBe("a.b.c2")
|
|
22
|
-
expect(payload).toBe(2)
|
|
23
|
-
})
|
|
24
|
-
})
|
|
25
|
-
test("不处理通配符的retain事件", () => {
|
|
26
|
-
const emitter = new FastEvent()
|
|
27
|
-
emitter.emit("a.b.c1", 1, true)
|
|
28
|
-
emitter.emit("a.b.c2", 2, true)
|
|
29
|
-
const events: string[] = []
|
|
30
|
-
// 订阅所有a.b.*事件,由于c1,c2是
|
|
31
|
-
emitter.on("a.b.*", ({ payload, type }) => {
|
|
32
|
-
events.push(type)
|
|
33
|
-
})
|
|
34
|
-
expect(events).toEqual([])
|
|
35
|
-
})
|
|
36
|
-
test("简单发布订阅retain事件", () => {
|
|
37
|
-
const emitter = new FastEvent()
|
|
38
|
-
const events: string[] = []
|
|
39
|
-
emitter.emit("a", 1, true)
|
|
40
|
-
emitter.emit("a.b", 2, true)
|
|
41
|
-
emitter.emit("a.b.c", 3, true)
|
|
42
|
-
emitter.emit("a.b.c.d", 4, true)
|
|
43
|
-
|
|
44
|
-
emitter.on("a", ({ payload, type }) => {
|
|
45
|
-
expect(type).toBe("a")
|
|
46
|
-
expect(payload).toBe(1)
|
|
47
|
-
events.push(type)
|
|
48
|
-
})
|
|
49
|
-
emitter.on("a.b", ({ payload, type }) => {
|
|
50
|
-
expect(type).toBe("a.b")
|
|
51
|
-
expect(payload).toBe(2)
|
|
52
|
-
events.push(type)
|
|
53
|
-
})
|
|
54
|
-
emitter.on("a.b.c", ({ payload, type }) => {
|
|
55
|
-
expect(type).toBe("a.b.c")
|
|
56
|
-
expect(payload).toBe(3)
|
|
57
|
-
events.push(type)
|
|
58
|
-
})
|
|
59
|
-
emitter.on("a.b.c.d", ({ payload, type }) => {
|
|
60
|
-
expect(type).toBe("a.b.c.d")
|
|
61
|
-
expect(payload).toBe(4)
|
|
62
|
-
events.push(type)
|
|
63
|
-
})
|
|
64
|
-
expect(events).toEqual(["a", "a.b", "a.b.c", "a.b.c.d"])
|
|
65
|
-
})
|
|
66
|
-
})
|
|
@@ -1,110 +0,0 @@
|
|
|
1
|
-
import { describe, test, expect } from "vitest"
|
|
2
|
-
import { FastEvent } from "../event"
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
describe("scope", () => {
|
|
6
|
-
test("scope简单的发布订阅事件", () => {
|
|
7
|
-
const emitter = new FastEvent()
|
|
8
|
-
const scope = emitter.scope("a/b/c")
|
|
9
|
-
const events: string[] = []
|
|
10
|
-
scope.on("x", ({ type }) => {
|
|
11
|
-
events.push(type)
|
|
12
|
-
})
|
|
13
|
-
emitter.on("a/b/c/x", ({ type }) => {
|
|
14
|
-
events.push(type)
|
|
15
|
-
})
|
|
16
|
-
scope.emit("x", 1)
|
|
17
|
-
expect(events).toEqual(["x", "a/b/c/x"])
|
|
18
|
-
})
|
|
19
|
-
|
|
20
|
-
test("scope通过off简单的退订事件", () => {
|
|
21
|
-
const emitter = new FastEvent()
|
|
22
|
-
const scope = emitter.scope("a/b/c")
|
|
23
|
-
|
|
24
|
-
const events: string[] = []
|
|
25
|
-
const subscriber = scope.on("x", ({ type }) => {
|
|
26
|
-
events.push(type)
|
|
27
|
-
})
|
|
28
|
-
emitter.on("a/b/c/x", ({ type }) => {
|
|
29
|
-
events.push(type)
|
|
30
|
-
})
|
|
31
|
-
scope.emit("x", 1)
|
|
32
|
-
subscriber.off()
|
|
33
|
-
scope.emit("x", 1)
|
|
34
|
-
expect(events).toEqual(["x", "a/b/c/x", "a/b/c/x"])
|
|
35
|
-
})
|
|
36
|
-
|
|
37
|
-
test("scope off退订事件", () => {
|
|
38
|
-
const emitter = new FastEvent()
|
|
39
|
-
const scope = emitter.scope("a/b/c")
|
|
40
|
-
|
|
41
|
-
const events: string[] = []
|
|
42
|
-
scope.on("x", ({ type }) => {
|
|
43
|
-
events.push(type)
|
|
44
|
-
})
|
|
45
|
-
emitter.on("a/b/c/x", ({ type }) => {
|
|
46
|
-
events.push(type)
|
|
47
|
-
})
|
|
48
|
-
scope.emit("x", 1)
|
|
49
|
-
scope.off('x') // 等效于退订a/b/c/x事件
|
|
50
|
-
scope.emit("x", 1)
|
|
51
|
-
expect(events).toEqual(["x", "a/b/c/x"])
|
|
52
|
-
})
|
|
53
|
-
test("scope once布订阅事件", () => {
|
|
54
|
-
const emitter = new FastEvent()
|
|
55
|
-
const scope = emitter.scope("a/b/c")
|
|
56
|
-
const events: string[] = []
|
|
57
|
-
scope.once("x", ({ type }) => {
|
|
58
|
-
events.push(type)
|
|
59
|
-
})
|
|
60
|
-
emitter.once("a/b/c/x", ({ type }) => {
|
|
61
|
-
events.push(type)
|
|
62
|
-
})
|
|
63
|
-
scope.emit("x", 1)
|
|
64
|
-
expect(events).toEqual(["x", "a/b/c/x"])
|
|
65
|
-
})
|
|
66
|
-
|
|
67
|
-
test('scope waitFor', async () => {
|
|
68
|
-
const emitter = new FastEvent();
|
|
69
|
-
const scope = emitter.scope("a/b/c")
|
|
70
|
-
|
|
71
|
-
// Arrange
|
|
72
|
-
const event1Promise = scope.waitFor('x', 500);
|
|
73
|
-
const event2Promise = scope.waitFor('y', 200);
|
|
74
|
-
const event3Promise = scope.waitFor('z', 1000);
|
|
75
|
-
|
|
76
|
-
// Act
|
|
77
|
-
setTimeout(() => {
|
|
78
|
-
scope.emit('x', 'payload1');
|
|
79
|
-
}, 100);
|
|
80
|
-
|
|
81
|
-
setTimeout(() => {
|
|
82
|
-
scope.emit('y', 'payload2');
|
|
83
|
-
}, 300);
|
|
84
|
-
|
|
85
|
-
// Event2 will timeout before emission
|
|
86
|
-
setTimeout(() => {
|
|
87
|
-
scope.emit('z', 'payload3');
|
|
88
|
-
}, 300);
|
|
89
|
-
|
|
90
|
-
// Assert
|
|
91
|
-
const results = await Promise.allSettled([
|
|
92
|
-
event1Promise,
|
|
93
|
-
event2Promise,
|
|
94
|
-
event3Promise
|
|
95
|
-
]);
|
|
96
|
-
|
|
97
|
-
expect(results[0].status).toBe('fulfilled');
|
|
98
|
-
expect((results[0] as any).value).toEqual({ type: 'x', payload: 'payload1', meta: undefined });
|
|
99
|
-
|
|
100
|
-
expect(results[1].status).toBe('rejected');
|
|
101
|
-
//@ts-ignore
|
|
102
|
-
expect(results[1].reason).toBeInstanceOf(Error);
|
|
103
|
-
|
|
104
|
-
expect(results[2].status).toBe('fulfilled');
|
|
105
|
-
expect((results[2] as any).value).toEqual({ type: 'z', payload: 'payload3', meta: undefined });
|
|
106
|
-
});
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
})
|
|
110
|
-
|
|
@@ -1,145 +0,0 @@
|
|
|
1
|
-
// eslint-disable no-unused-vars
|
|
2
|
-
import { describe, test, expect } from "vitest"
|
|
3
|
-
import type { Equal, Expect, NotAny } from '@type-challenges/utils'
|
|
4
|
-
import { FastEvent } from "../event"
|
|
5
|
-
import { ScopeEvents } from "../types"
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
describe("Types", () => {
|
|
9
|
-
test("Types tests", () => {
|
|
10
|
-
interface CustomEvents {
|
|
11
|
-
a: boolean
|
|
12
|
-
b: number
|
|
13
|
-
c: string,
|
|
14
|
-
"x/y/z/a": 1
|
|
15
|
-
"x/y/z/b": 2
|
|
16
|
-
"x/y/z/c": 3
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
type ScopeCustomEvents = ScopeEvents<CustomEvents, 'x/y/z'>
|
|
20
|
-
|
|
21
|
-
type cases = [
|
|
22
|
-
Expect<Equal<ScopeCustomEvents, {
|
|
23
|
-
a: 1
|
|
24
|
-
b: 2
|
|
25
|
-
c: 3
|
|
26
|
-
}>>
|
|
27
|
-
]
|
|
28
|
-
|
|
29
|
-
interface CustomMeta {
|
|
30
|
-
a: number
|
|
31
|
-
b: string
|
|
32
|
-
c: boolean
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
const emitter = new FastEvent<CustomEvents, CustomMeta>()
|
|
36
|
-
|
|
37
|
-
emitter.on("a", (message) => {
|
|
38
|
-
message.meta
|
|
39
|
-
type cases = [
|
|
40
|
-
Expect<Equal<typeof message.meta, CustomMeta>>
|
|
41
|
-
]
|
|
42
|
-
})
|
|
43
|
-
|
|
44
|
-
emitter.on("a", (message) => {
|
|
45
|
-
type cases = [
|
|
46
|
-
Expect<Equal<typeof message.type, "a">>,
|
|
47
|
-
Expect<Equal<typeof message.payload, boolean>>
|
|
48
|
-
]
|
|
49
|
-
})
|
|
50
|
-
|
|
51
|
-
emitter.onAny((message) => {
|
|
52
|
-
type cases = [
|
|
53
|
-
Expect<Equal<typeof message.type, string>>,
|
|
54
|
-
Expect<Equal<typeof message.payload, any>>
|
|
55
|
-
]
|
|
56
|
-
})
|
|
57
|
-
|
|
58
|
-
emitter.onAny<number>((message) => {
|
|
59
|
-
type cases = [
|
|
60
|
-
Expect<Equal<typeof message.type, string>>,
|
|
61
|
-
Expect<Equal<typeof message.payload, number>>
|
|
62
|
-
]
|
|
63
|
-
})
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
emitter.on("b", (message) => {
|
|
67
|
-
type cases = [
|
|
68
|
-
Expect<Equal<typeof message.type, "b">>,
|
|
69
|
-
Expect<Equal<typeof message.payload, number>>
|
|
70
|
-
]
|
|
71
|
-
})
|
|
72
|
-
|
|
73
|
-
emitter.once("a", (message) => {
|
|
74
|
-
type cases = [
|
|
75
|
-
Expect<Equal<typeof message.type, "a">>,
|
|
76
|
-
Expect<Equal<typeof message.payload, boolean>>
|
|
77
|
-
]
|
|
78
|
-
})
|
|
79
|
-
|
|
80
|
-
emitter.once("b", (message) => {
|
|
81
|
-
type cases = [
|
|
82
|
-
Expect<Equal<typeof message.type, "b">>,
|
|
83
|
-
Expect<Equal<typeof message.payload, number>>
|
|
84
|
-
]
|
|
85
|
-
})
|
|
86
|
-
emitter.emit("x/y/z", 1)
|
|
87
|
-
|
|
88
|
-
emitter.on("x/y/z", (message) => {
|
|
89
|
-
type cases = [
|
|
90
|
-
Expect<Equal<typeof message.type, "x/y/z">>,
|
|
91
|
-
Expect<Equal<typeof message.payload, unknown>>
|
|
92
|
-
]
|
|
93
|
-
})
|
|
94
|
-
|
|
95
|
-
emitter.on("x/y/z/a", (message) => {
|
|
96
|
-
type cases = [
|
|
97
|
-
Expect<Equal<typeof message.type, "x/y/z/a">>,
|
|
98
|
-
Expect<Equal<typeof message.payload, 1>>
|
|
99
|
-
]
|
|
100
|
-
})
|
|
101
|
-
emitter.waitFor("x/y/z/a").then((message) => {
|
|
102
|
-
type cases = [
|
|
103
|
-
Expect<Equal<typeof message.type, "x/y/z/a">>,
|
|
104
|
-
Expect<Equal<typeof message.payload, 1>>
|
|
105
|
-
]
|
|
106
|
-
})
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
// ----- scope -----
|
|
111
|
-
|
|
112
|
-
const scope = emitter.scope("x/y/z")
|
|
113
|
-
|
|
114
|
-
scope.on("a", (message) => {
|
|
115
|
-
type cases = [
|
|
116
|
-
Expect<Equal<typeof message.type, "a">>,
|
|
117
|
-
Expect<Equal<typeof message.payload, 1>>
|
|
118
|
-
]
|
|
119
|
-
})
|
|
120
|
-
|
|
121
|
-
scope.on("b", (message) => {
|
|
122
|
-
type cases = [
|
|
123
|
-
Expect<Equal<typeof message.type, "b">>,
|
|
124
|
-
Expect<Equal<typeof message.payload, 2>>
|
|
125
|
-
]
|
|
126
|
-
})
|
|
127
|
-
|
|
128
|
-
scope.once("a", (message) => {
|
|
129
|
-
type cases = [
|
|
130
|
-
Expect<Equal<typeof message.type, "a">>,
|
|
131
|
-
Expect<Equal<typeof message.payload, 1>>
|
|
132
|
-
]
|
|
133
|
-
})
|
|
134
|
-
|
|
135
|
-
scope.once("c", (message) => {
|
|
136
|
-
type cases = [
|
|
137
|
-
Expect<Equal<typeof message.type, "c">>,
|
|
138
|
-
Expect<Equal<typeof message.payload, 3>>
|
|
139
|
-
]
|
|
140
|
-
})
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
})
|
|
145
|
-
})
|