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.
Files changed (80) hide show
  1. package/dist/devTools.js +2 -2
  2. package/dist/devTools.js.map +1 -1
  3. package/dist/devTools.mjs +2 -2
  4. package/dist/devTools.mjs.map +1 -1
  5. package/dist/index.d.mts +304 -53
  6. package/dist/index.d.ts +304 -53
  7. package/dist/index.js +1 -1
  8. package/dist/index.js.map +1 -1
  9. package/dist/index.mjs +1 -1
  10. package/dist/index.mjs.map +1 -1
  11. package/package.json +64 -50
  12. package/readme.md +517 -50
  13. package/.changeset/README.md +0 -8
  14. package/.changeset/config.json +0 -11
  15. package/.github/workflows/publish.yaml +0 -50
  16. package/.prettierrc.js +0 -20
  17. package/.vscode/launch.json +0 -20
  18. package/.vscode/settings.json +0 -18
  19. package/CHANGELOG.md +0 -60
  20. package/LICENSE +0 -21
  21. package/bench.png +0 -0
  22. package/dist/devTools.d.mts +0 -543
  23. package/dist/devTools.d.ts +0 -543
  24. package/example/README.md +0 -54
  25. package/example/eslint.config.js +0 -28
  26. package/example/index.html +0 -13
  27. package/example/package.json +0 -29
  28. package/example/pnpm-lock.yaml +0 -2047
  29. package/example/public/vite.svg +0 -1
  30. package/example/src/App.css +0 -42
  31. package/example/src/App.tsx +0 -60
  32. package/example/src/assets/react.svg +0 -1
  33. package/example/src/index.css +0 -68
  34. package/example/src/main.tsx +0 -10
  35. package/example/src/myEvent.ts +0 -32
  36. package/example/src/vite-env.d.ts +0 -1
  37. package/example/tsconfig.app.json +0 -26
  38. package/example/tsconfig.json +0 -7
  39. package/example/tsconfig.node.json +0 -24
  40. package/example/vite.config.ts +0 -7
  41. package/packages/native/index.ts +0 -1
  42. package/packages/turbo/.zig-cache/h/271c82d991949fd7788fd5451f0ca834.txt +0 -0
  43. package/packages/turbo/.zig-cache/h/timestamp +0 -0
  44. package/packages/turbo/.zig-cache/o/ebd7ddab8ffe003267120d598aecce68/dependencies.zig +0 -2
  45. package/packages/turbo/.zig-cache/z/c8114b040daa461a9e2eabd0357554a4 +0 -0
  46. package/packages/turbo/build.zig +0 -60
  47. package/packages/turbo/examples/basic.zig +0 -107
  48. package/packages/turbo/src/event.zig +0 -251
  49. package/packages/turbo/src/index.zig +0 -70
  50. package/packages/turbo/src/scope.zig +0 -104
  51. package/packages/turbo/src/types.zig +0 -88
  52. package/packages/turbo/src/utils.zig +0 -171
  53. package/readme_cn.md +0 -491
  54. package/src/__benchmarks__/index.ts +0 -3
  55. package/src/__benchmarks__/multi-level.ts +0 -40
  56. package/src/__benchmarks__/sample.ts +0 -40
  57. package/src/__benchmarks__/wildcard.ts +0 -41
  58. package/src/__tests__/emit.test.ts +0 -106
  59. package/src/__tests__/emitAsync.test.ts +0 -64
  60. package/src/__tests__/isPathMatched.test.ts +0 -205
  61. package/src/__tests__/many.test.ts +0 -22
  62. package/src/__tests__/meta.test.ts +0 -28
  63. package/src/__tests__/off.test.ts +0 -214
  64. package/src/__tests__/onany.test.ts +0 -212
  65. package/src/__tests__/once.test.ts +0 -70
  66. package/src/__tests__/retain.test.ts +0 -66
  67. package/src/__tests__/scope.test.ts +0 -110
  68. package/src/__tests__/types.test.ts +0 -145
  69. package/src/__tests__/waitFor.test.ts +0 -116
  70. package/src/__tests__/wildcard.test.ts +0 -185
  71. package/src/devTools.ts +0 -166
  72. package/src/event.ts +0 -741
  73. package/src/index.ts +0 -3
  74. package/src/scope.ts +0 -130
  75. package/src/types.ts +0 -66
  76. package/src/utils/WeakObjectMap.ts +0 -64
  77. package/src/utils/isPathMatched.ts +0 -40
  78. package/src/utils/removeItem.ts +0 -16
  79. package/tsconfig.json +0 -104
  80. package/tsup.config.ts +0 -30
@@ -1,106 +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
- emitter.on("x", ({ type, payload }) => {
8
- expect(type).toBe("x")
9
- expect(payload).toBe(1)
10
- })
11
- emitter.emit("x", 1)
12
- })
13
- test("简单发布订阅事件后取消", () => {
14
- const emitter = new FastEvent()
15
- const events: string[] = []
16
- const subscriber = emitter.on("x", ({ type, payload }) => {
17
- expect(type).toBe("x")
18
- expect(payload).toBe(1)
19
- events.push(type)
20
- })
21
- emitter.emit("x", 1)
22
- expect(events).toEqual(["x"])
23
- subscriber.off()
24
- emitter.emit("x", 1)
25
- emitter.emit("x", 1)
26
- emitter.emit("x", 1)
27
- expect(events).toEqual(["x"])
28
- })
29
- test("发布订阅层级事件", () => {
30
- const emitter = new FastEvent()
31
- const events: string[] = []
32
- emitter.on("a.b.c", ({ type, payload }) => {
33
- expect(type).toBe("a.b.c")
34
- expect(payload).toBe(1)
35
- events.push(type)
36
- })
37
- emitter.emit("a", 1)
38
- emitter.emit("a.b", 1)
39
- emitter.emit("a.b.c", 1)
40
- expect(events).toEqual(["a.b.c"])
41
- })
42
- test("返回事件执行结果", () => {
43
- const emitter = new FastEvent()
44
- for (let i = 1; i <= 10; i++) {
45
- emitter.on("x", () => {
46
- return i
47
- })
48
- }
49
- const results = emitter.emit("x", 1)
50
- expect(results).toEqual([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
51
- })
52
- test("侦听器执行出错返回事件执行结果", async () => {
53
- const emitter = new FastEvent()
54
- for (let i = 1; i <= 10; i++) {
55
- emitter.on("x", () => {
56
- if (i % 2 == 0) throw new Error("custom")
57
- return i
58
- })
59
- }
60
- const results = emitter.emit("x", 1)
61
- for (let i = 1; i <= 10; i++) {
62
- if (i % 2 == 0) expect(results[i - 1]).toBeInstanceOf(Error)
63
- else expect(results[i - 1]).toBe(i)
64
- }
65
- })
66
- test("侦听器执行出错时emit出错", async () => {
67
- const emitter = new FastEvent({ ignoreErrors: false })
68
- const err = new Error("custom")
69
- for (let i = 1; i <= 10; i++) {
70
- emitter.on("x", () => {
71
- if (i % 2 == 0) throw err
72
- return i
73
- })
74
- }
75
- expect(() => emitter.emit("x", 1)).toThrow(err);
76
- // @ts-ignore, 当执行侦听器出错时会在错误对象上挂载一个_listener属性代表当前执行的侦听器路径
77
- expect(err._emitter).toBe("x")
78
-
79
- })
80
- test("添加侦听器时指定顺序", async () => {
81
- const emitter = new FastEvent()
82
- const types: number[] = []
83
- emitter.on("x", () => {
84
- types.push(1)
85
- })
86
-
87
- emitter.on("x", () => {
88
- types.push(2)
89
- }, { prepend: true })
90
- emitter.emit("x")
91
- expect(types).toEqual([2, 1])
92
- })
93
- test("简单发布订阅message事件", () => {
94
- const emitter = new FastEvent()
95
- emitter.on("x", ({ type, payload, meta }) => {
96
- expect(type).toBe("x")
97
- expect(meta.a).toBe(1)
98
- expect(payload).toBe(1)
99
- })
100
- emitter.emit({
101
- type: "x",
102
- payload: 1,
103
- meta: { a: 1 }
104
- })
105
- })
106
- })
@@ -1,64 +0,0 @@
1
- import { describe, test, expect } from "vitest"
2
- import { FastEvent } from "../event"
3
-
4
- describe("异步发布与订阅", async () => {
5
- test("异步发布订阅事件", async () => {
6
- const emitter = new FastEvent()
7
- const events: any[] = []
8
- emitter.on("x", async ({ payload, type }) => {
9
- events.push({ type, payload })
10
- })
11
- await emitter.emitAsync("x", 1)
12
- expect(events[0].type).toBe("x")
13
- expect(events[0].payload).toBe(1)
14
- })
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
- })
21
- await emitter.emitAsync("x", 1)
22
- expect(events).toEqual(["x"])
23
- subscriber.off()
24
- await emitter.emitAsync("x", 1)
25
- await emitter.emitAsync("x", 1)
26
- await emitter.emitAsync("x", 1)
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 ({ type }) => {
33
- events.push(type)
34
- })
35
- await emitter.emitAsync("a", 1)
36
- await emitter.emitAsync("a.b", 1)
37
- await emitter.emitAsync("a.b.c", 1)
38
- expect(events).toEqual(["a.b.c"])
39
- })
40
- test("返回事件执行结果", async () => {
41
- const emitter = new FastEvent()
42
- for (let i = 1; i <= 10; i++) {
43
- emitter.on("x", async () => {
44
- return i
45
- })
46
- }
47
- const results = await emitter.emitAsync("x", 1)
48
- expect(results).toEqual([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
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")
55
- return i
56
- })
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)
62
- }
63
- })
64
- })
@@ -1,205 +0,0 @@
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
- })
@@ -1,22 +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.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
- })
@@ -1,28 +0,0 @@
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, meta }) => {
10
- expect(type).toBe("x")
11
- expect(payload).toBe(1)
12
- expect(meta.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, meta }) => {
21
- expect(type).toBe("x")
22
- expect(payload).toBe(1)
23
- expect(meta.a).toBe(10)
24
- expect(meta.b).toBe(20)
25
- })
26
- emitter.emit("x", 1, false, { a: 10, b: 20 })
27
- })
28
- })
@@ -1,214 +0,0 @@
1
- import { describe, test, expect } from "vitest"
2
- import { FastEvent } from "../event"
3
- import { FastEventListener } from '../types';
4
-
5
-
6
- describe("退订事件", () => {
7
- test("基本退订事件", () => {
8
- const emitter = new FastEvent()
9
- const events: string[] = []
10
- const subscriber = emitter.on("x", ({ payload, type }) => {
11
- expect(type).toBe("x")
12
- expect(payload).toBe(1)
13
- events.push(type)
14
- })
15
- emitter.emit("x", 1)
16
- subscriber.off()
17
- emitter.emit("x", 1)
18
- expect(events).toEqual(["x"])
19
- })
20
- test("根据事件类型和侦听器进行退订", () => {
21
- const emitter = new FastEvent()
22
- const events: string[] = []
23
- const listener: FastEventListener<string, number> = (({ payload, type }) => {
24
- expect(type).toBe("x")
25
- expect(payload).toBe(1)
26
- events.push(type)
27
- })
28
- emitter.on("x", listener)
29
- emitter.emit("x", 1)
30
- emitter.off("x", listener)
31
- emitter.emit("x", 1)
32
- expect(events).toEqual(["x"])
33
- })
34
- test("多级事件根据事件类型和侦听器进行退订", () => {
35
- const emitter = new FastEvent()
36
- const events: string[] = []
37
- const listener: FastEventListener<string, number> = (({ payload, type }) => {
38
- expect(type).toBe("a/b/c/d")
39
- expect(payload).toBe(1)
40
- events.push(type)
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)
48
- expect(events).toEqual(["a/b/c/d"])
49
- })
50
-
51
- test("通配符事件退订", () => {
52
- const emitter = new FastEvent()
53
- const events: string[] = []
54
- const listener: FastEventListener<string, number> = ({ payload, type }) => {
55
- expect(type).toBe("a/b/c/d")
56
- expect(payload).toBe(1)
57
- events.push(type)
58
- }
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)
65
- expect(events).toEqual(["a/b/c/d"])
66
- })
67
-
68
- test("退订指定的侦听器", () => {
69
- const emitter = new FastEvent()
70
- const events: string[] = []
71
- const listener: FastEventListener<string, number> = ({ payload, type }) => {
72
- expect(type).toBe("a/b/c/d")
73
- expect(payload).toBe(1)
74
- events.push(type)
75
- }
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"])
87
- })
88
- test("退订指定的事件时不指定侦听器", () => {
89
- const emitter = new FastEvent()
90
- const events: string[] = []
91
- const listener: FastEventListener<string, number> = ({ payload, type }) => {
92
- events.push(type)
93
- }
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"])
108
- })
109
- test("退订所有侦听器", () => {
110
- const emitter = new FastEvent()
111
- const events: string[] = []
112
- const listener: FastEventListener<string, number> = ({ payload, type }) => {
113
- events.push(type)
114
- }
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)
123
- emitter.offAll()
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"])
133
- })
134
- test("退订含通配符的事件", () => {
135
- const emitter = new FastEvent()
136
- const events: string[] = []
137
- const listener: FastEventListener<string, number> = ({ payload, type }) => {
138
- events.push(type)
139
- }
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)
148
- emitter.off("*")
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"])
158
- })
159
- test("退订含通配符的事件", () => {
160
- const emitter = new FastEvent()
161
- const events: string[] = []
162
- const listener: FastEventListener<string, number> = ({ payload, type }) => {
163
- events.push(type)
164
- }
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)
173
- emitter.off("a/*")
174
- emitter.off("b/*")
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"])
180
- })
181
- test("退订多层含通配符的事件", () => {
182
- const emitter = new FastEvent()
183
- const events: string[] = []
184
- const listener: FastEventListener<string, number> = ({ payload, type }) => {
185
- events.push(type)
186
- }
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)
192
-
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)
198
-
199
- emitter.off("a/**")
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)
210
-
211
- expect(events).toEqual(["a/1", "a/b/2", "a/b/c/3", "a/b/c/d/4", "a/b/c/d/e/5"])
212
- })
213
- })
214
-