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.
@@ -1,213 +1,212 @@
1
1
  import { describe, test, expect } from "vitest"
2
- import { FastEvent } from "../event"
3
-
2
+ import { FastEvent } from "../event"
4
3
 
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})=>{
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
13
  allEvents.push(type)
14
14
  allValues.push(payload)
15
15
  })
16
- emitter.on("a",(payload,{type})=>{
16
+ emitter.on("a", ({ payload, type }) => {
17
17
  expect(type).toBe("a")
18
18
  values.push(payload)
19
19
  events.push(type)
20
20
  })
21
- emitter.on("b",(payload,{type})=>{
21
+ emitter.on("b", ({ payload, type }) => {
22
22
  expect(type).toBe("b")
23
23
  values.push(payload)
24
24
  events.push(type)
25
25
  })
26
- emitter.on("c",(payload,{type})=>{
26
+ emitter.on("c", ({ payload, type }) => {
27
27
  expect(type).toBe("c")
28
28
  values.push(payload)
29
29
  events.push(type)
30
30
  })
31
- emitter.on("d",(payload,{type})=>{
31
+ emitter.on("d", ({ payload, type }) => {
32
32
  expect(type).toBe("d")
33
33
  values.push(payload)
34
34
  events.push(type)
35
35
  })
36
- emitter.emit("a",1)
37
- emitter.emit("b",2)
38
- emitter.emit("c",3)
39
- emitter.emit("d",4)
36
+ emitter.emit("a", 1)
37
+ emitter.emit("b", 2)
38
+ emitter.emit("c", 3)
39
+ emitter.emit("d", 4)
40
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])
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
45
 
46
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})=>{
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
54
  allEvents.push(type)
55
55
  allValues.push(payload)
56
56
  })
57
- emitter.on("a",(payload,{type})=>{
57
+ emitter.on("a", ({ payload, type }) => {
58
58
  expect(type).toBe("a")
59
59
  values.push(payload)
60
60
  events.push(type)
61
61
  })
62
- emitter.on("b",(payload,{type})=>{
62
+ emitter.on("b", ({ payload, type }) => {
63
63
  expect(type).toBe("b")
64
64
  values.push(payload)
65
65
  events.push(type)
66
66
  })
67
- emitter.on("c",(payload,{type})=>{
67
+ emitter.on("c", ({ payload, type }) => {
68
68
  expect(type).toBe("c")
69
69
  values.push(payload)
70
70
  events.push(type)
71
71
  })
72
- emitter.on("d",(payload,{type})=>{
72
+ emitter.on("d", ({ payload, type }) => {
73
73
  expect(type).toBe("d")
74
74
  values.push(payload)
75
75
  events.push(type)
76
76
  })
77
- emitter.emit("a",1)
78
- emitter.emit("b",2)
79
- emitter.emit("c",3)
80
- emitter.emit("d",4)
77
+ emitter.emit("a", 1)
78
+ emitter.emit("b", 2)
79
+ emitter.emit("c", 3)
80
+ emitter.emit("d", 4)
81
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])
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
86
 
87
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})=>{
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
95
  allEvents.push(type)
96
96
  allValues.push(payload)
97
97
  })
98
- emitter.on("a.x.y",(payload,{type})=>{
98
+ emitter.on("a.x.y", ({ payload, type }) => {
99
99
  expect(type).toBe("a.x.y")
100
100
  values.push(payload)
101
101
  events.push(type)
102
102
  })
103
- emitter.on("b.m.n",(payload,{type})=>{
103
+ emitter.on("b.m.n", ({ payload, type }) => {
104
104
  expect(type).toBe("b.m.n")
105
105
  values.push(payload)
106
106
  events.push(type)
107
107
  })
108
- emitter.on("c.o.p",(payload,{type})=>{
108
+ emitter.on("c.o.p", ({ payload, type }) => {
109
109
  expect(type).toBe("c.o.p")
110
110
  values.push(payload)
111
111
  events.push(type)
112
112
  })
113
- emitter.on("d.q.r",(payload,{type})=>{
113
+ emitter.on("d.q.r", ({ payload, type }) => {
114
114
  expect(type).toBe("d.q.r")
115
115
  values.push(payload)
116
116
  events.push(type)
117
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)
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
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])
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
127
 
128
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})=>{
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
136
  allEvents.push(type)
137
137
  allValues.push(payload)
138
138
  })
139
- emitter.on("a",(payload,{type})=>{
139
+ emitter.on("a", ({ payload, type }) => {
140
140
  expect(type).toBe("a")
141
141
  values.push(payload)
142
142
  events.push(type)
143
143
  })
144
- emitter.on("b",(payload,{type})=>{
144
+ emitter.on("b", ({ payload, type }) => {
145
145
  expect(type).toBe("b")
146
146
  values.push(payload)
147
147
  events.push(type)
148
148
  })
149
- emitter.on("c",(payload,{type})=>{
149
+ emitter.on("c", ({ payload, type }) => {
150
150
  expect(type).toBe("c")
151
151
  values.push(payload)
152
152
  events.push(type)
153
153
  })
154
- emitter.on("d",(payload,{type})=>{
154
+ emitter.on("d", ({ payload, type }) => {
155
155
  expect(type).toBe("d")
156
156
  values.push(payload)
157
157
  events.push(type)
158
158
  })
159
- emitter.emit("a",1)
159
+ emitter.emit("a", 1)
160
160
  subscriber.off()
161
- emitter.emit("b",2)
162
- emitter.emit("c",3)
163
- emitter.emit("d",4)
161
+ emitter.emit("b", 2)
162
+ emitter.emit("c", 3)
163
+ emitter.emit("d", 4)
164
164
 
165
- expect(events).toEqual(["a","b","c","d"])
166
- expect(values).toEqual([1,2,3,4])
165
+ expect(events).toEqual(["a", "b", "c", "d"])
166
+ expect(values).toEqual([1, 2, 3, 4])
167
167
  expect(allEvents).toEqual(["a"])
168
- expect(allValues).toEqual([1])
168
+ expect(allValues).toEqual([1])
169
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})=>{
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
177
  allEvents.push(type)
178
178
  allValues.push(payload)
179
179
  })
180
- emitter.on("a",(payload,{type})=>{
180
+ emitter.on("a", ({ payload, type }) => {
181
181
  expect(type).toBe("a")
182
182
  values.push(payload)
183
183
  events.push(type)
184
184
  })
185
- emitter.on("b",(payload,{type})=>{
185
+ emitter.on("b", ({ payload, type }) => {
186
186
  expect(type).toBe("b")
187
187
  values.push(payload)
188
188
  events.push(type)
189
189
  })
190
- emitter.on("c",(payload,{type})=>{
190
+ emitter.on("c", ({ payload, type }) => {
191
191
  expect(type).toBe("c")
192
192
  values.push(payload)
193
193
  events.push(type)
194
194
  })
195
- emitter.on("d",(payload,{type})=>{
195
+ emitter.on("d", ({ payload, type }) => {
196
196
  expect(type).toBe("d")
197
197
  values.push(payload)
198
198
  events.push(type)
199
199
  })
200
- emitter.emit("a",1)
201
- emitter.emit("b",2)
202
- emitter.emit("c",3)
203
- emitter.emit("d",4)
200
+ emitter.emit("a", 1)
201
+ emitter.emit("b", 2)
202
+ emitter.emit("c", 3)
203
+ emitter.emit("d", 4)
204
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])
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
209
 
210
210
  })
211
211
  })
212
212
 
213
-
@@ -1,71 +1,70 @@
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.once("x",(payload,{type})=>{
4
+ describe("只订阅一次的事件的发布与订阅", async () => {
5
+ test("简单发布只订阅一次事件", () => {
6
+ const emitter = new FastEvent()
7
+ const events: string[] = []
8
+ emitter.once("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
12
  })
13
- emitter.emit("x",1)
14
- emitter.emit("x",1)
15
- emitter.emit("x",1)
16
- emitter.emit("x",1)
13
+ emitter.emit("x", 1)
14
+ emitter.emit("x", 1)
15
+ emitter.emit("x", 1)
16
+ emitter.emit("x", 1)
17
17
  expect(events).toEqual(["x"])
18
18
  })
19
- test("简单发布只订阅一次事件后取消",()=>{
20
- const emitter = new FastEvent()
21
- const events:string[]=[]
22
- const subscriber = emitter.once("x",(payload,{type})=>{
19
+ test("简单发布只订阅一次事件后取消", () => {
20
+ const emitter = new FastEvent()
21
+ const events: string[] = []
22
+ const subscriber = emitter.once("x", ({ payload, type }) => {
23
23
  expect(type).toBe("x")
24
- expect(payload).toBe(1)
25
- events.push(type)
24
+ expect(payload).toBe(1)
25
+ events.push(type)
26
26
  })
27
27
  subscriber.off()
28
- emitter.emit("x",1)
29
- emitter.emit("x",1)
30
- emitter.emit("x",1)
28
+ emitter.emit("x", 1)
29
+ emitter.emit("x", 1)
30
+ emitter.emit("x", 1)
31
31
  expect(events).toEqual([])
32
32
  })
33
- test("简单发布只订阅一次的多级事件",()=>{
34
- const emitter = new FastEvent()
35
- const events:string[] =[]
36
- emitter.once("a.b.c.d",(payload,{type})=>{
33
+ test("简单发布只订阅一次的多级事件", () => {
34
+ const emitter = new FastEvent()
35
+ const events: string[] = []
36
+ emitter.once("a.b.c.d", ({ payload, type }) => {
37
37
  expect(type).toBe("a.b.c.d")
38
- expect(payload).toBe(1)
38
+ expect(payload).toBe(1)
39
39
  events.push(type)
40
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)
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
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})=>{
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
52
  expect(type).toBe("a.b.c.d")
53
53
  values.push(payload)
54
54
  events.push(type)
55
55
  })
56
- emitter.on("a.b.c.d",(payload,{type})=>{
56
+ emitter.on("a.b.c.d", ({ payload, type }) => {
57
57
  expect(type).toBe("a.b.c.d")
58
58
  values.push(payload)
59
59
  events.push(type)
60
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])
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
67
  })
68
68
 
69
69
 
70
70
  })
71
-
@@ -1,66 +1,66 @@
1
1
  import { describe, test, expect } from "vitest"
2
- import { FastEvent } from "../event"
2
+ import { FastEvent } from "../event"
3
3
 
4
4
 
5
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})=>{
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
11
  expect(type).toBe("x")
12
- expect(payload).toBe(1)
12
+ expect(payload).toBe(1)
13
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})=>{
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
17
  expect(type).toBe("a.b.c1")
18
- expect(payload).toBe(1)
18
+ expect(payload).toBe(1)
19
19
  })
20
- emitter.on("a.b.c2",(payload,{type})=>{
20
+ emitter.on("a.b.c2", ({ payload, type }) => {
21
21
  expect(type).toBe("a.b.c2")
22
- expect(payload).toBe(2)
22
+ expect(payload).toBe(2)
23
23
  })
24
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[] = []
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
30
  // 订阅所有a.b.*事件,由于c1,c2是
31
- emitter.on("a.b.*",(payload,{type})=>{
31
+ emitter.on("a.b.*", ({ payload, type }) => {
32
32
  events.push(type)
33
33
  })
34
34
  expect(events).toEqual([])
35
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})=>{
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
45
  expect(type).toBe("a")
46
- expect(payload).toBe(1)
46
+ expect(payload).toBe(1)
47
47
  events.push(type)
48
48
  })
49
- emitter.on("a.b",(payload,{type})=>{
49
+ emitter.on("a.b", ({ payload, type }) => {
50
50
  expect(type).toBe("a.b")
51
- expect(payload).toBe(2)
51
+ expect(payload).toBe(2)
52
52
  events.push(type)
53
53
  })
54
- emitter.on("a.b.c",(payload,{type})=>{
54
+ emitter.on("a.b.c", ({ payload, type }) => {
55
55
  expect(type).toBe("a.b.c")
56
- expect(payload).toBe(3)
56
+ expect(payload).toBe(3)
57
57
  events.push(type)
58
58
  })
59
- emitter.on("a.b.c.d",(payload,{type})=>{
59
+ emitter.on("a.b.c.d", ({ payload, type }) => {
60
60
  expect(type).toBe("a.b.c.d")
61
- expect(payload).toBe(4)
61
+ expect(payload).toBe(4)
62
62
  events.push(type)
63
63
  })
64
- expect(events).toEqual(["a","a.b","a.b.c","a.b.c.d"])
64
+ expect(events).toEqual(["a", "a.b", "a.b.c", "a.b.c.d"])
65
65
  })
66
66
  })