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,67 +1,67 @@
1
1
  import { describe, test, expect } from "vitest"
2
- import { FastEvent } from "../event"
3
-
2
+ import { FastEvent } from "../event"
4
3
 
5
- describe("scope", ()=>{
6
- test("scope简单的发布订阅事件",()=>{
7
- const emitter = new FastEvent()
4
+
5
+ describe("scope", () => {
6
+ test("scope简单的发布订阅事件", () => {
7
+ const emitter = new FastEvent()
8
8
  const scope = emitter.scope("a/b/c")
9
- const events:string[] =[]
10
- scope.on("x", (payload,{type})=>{
9
+ const events: string[] = []
10
+ scope.on("x", ({ type }) => {
11
11
  events.push(type)
12
12
  })
13
- emitter.on("a/b/c/x", (payload,{type})=>{
13
+ emitter.on("a/b/c/x", ({ type }) => {
14
14
  events.push(type)
15
15
  })
16
- scope.emit("x",1)
17
- expect(events).toEqual(["x","a/b/c/x"])
16
+ scope.emit("x", 1)
17
+ expect(events).toEqual(["x", "a/b/c/x"])
18
18
  })
19
19
 
20
- test("scope通过off简单的退订事件",()=>{
21
- const emitter = new FastEvent()
20
+ test("scope通过off简单的退订事件", () => {
21
+ const emitter = new FastEvent()
22
22
  const scope = emitter.scope("a/b/c")
23
23
 
24
- const events:string[] =[]
25
- const subscriber =scope.on("x", (payload,{type})=>{
24
+ const events: string[] = []
25
+ const subscriber = scope.on("x", ({ type }) => {
26
26
  events.push(type)
27
27
  })
28
- emitter.on("a/b/c/x", (payload,{type})=>{
28
+ emitter.on("a/b/c/x", ({ type }) => {
29
29
  events.push(type)
30
30
  })
31
- scope.emit("x",1)
31
+ scope.emit("x", 1)
32
32
  subscriber.off()
33
- scope.emit("x",1)
34
- expect(events).toEqual(["x","a/b/c/x","a/b/c/x"])
33
+ scope.emit("x", 1)
34
+ expect(events).toEqual(["x", "a/b/c/x", "a/b/c/x"])
35
35
  })
36
36
 
37
- test("scope off退订事件",()=>{
38
- const emitter = new FastEvent()
37
+ test("scope off退订事件", () => {
38
+ const emitter = new FastEvent()
39
39
  const scope = emitter.scope("a/b/c")
40
40
 
41
- const events:string[] =[]
42
- scope.on("x", (payload,{type})=>{
41
+ const events: string[] = []
42
+ scope.on("x", ({ type }) => {
43
43
  events.push(type)
44
44
  })
45
- emitter.on("a/b/c/x", (payload,{type})=>{
45
+ emitter.on("a/b/c/x", ({ type }) => {
46
46
  events.push(type)
47
47
  })
48
- scope.emit("x",1)
48
+ scope.emit("x", 1)
49
49
  scope.off('x') // 等效于退订a/b/c/x事件
50
- scope.emit("x",1)
51
- expect(events).toEqual(["x","a/b/c/x"])
50
+ scope.emit("x", 1)
51
+ expect(events).toEqual(["x", "a/b/c/x"])
52
52
  })
53
- test("scope once布订阅事件",()=>{
54
- const emitter = new FastEvent()
53
+ test("scope once布订阅事件", () => {
54
+ const emitter = new FastEvent()
55
55
  const scope = emitter.scope("a/b/c")
56
- const events:string[] =[]
57
- scope.once("x", (payload,{type})=>{
56
+ const events: string[] = []
57
+ scope.once("x", ({ type }) => {
58
58
  events.push(type)
59
59
  })
60
- emitter.once("a/b/c/x", (payload,{type})=>{
60
+ emitter.once("a/b/c/x", ({ type }) => {
61
61
  events.push(type)
62
62
  })
63
- scope.emit("x",1)
64
- expect(events).toEqual(["x","a/b/c/x"])
63
+ scope.emit("x", 1)
64
+ expect(events).toEqual(["x", "a/b/c/x"])
65
65
  })
66
66
 
67
67
  test('scope waitFor', async () => {
@@ -95,17 +95,16 @@ describe("scope", ()=>{
95
95
  ]);
96
96
 
97
97
  expect(results[0].status).toBe('fulfilled');
98
- expect(results[0]).toHaveProperty('value', 'payload1');
99
-
100
- expect(results[1].status).toBe('rejected');
98
+ expect((results[0] as any).value).toEqual({ type: 'a/b/c/x', payload: 'payload1', meta: undefined });
99
+
100
+ expect(results[1].status).toBe('rejected');
101
101
  //@ts-ignore
102
102
  expect(results[1].reason).toBeInstanceOf(Error);
103
-
103
+
104
104
  expect(results[2].status).toBe('fulfilled');
105
- expect(results[2]).toHaveProperty('value', 'payload3');
105
+ expect((results[2] as any).value).toEqual({ type: 'a/b/c/z', payload: 'payload3', meta: undefined });
106
106
  });
107
107
 
108
108
 
109
109
  })
110
110
 
111
-
@@ -0,0 +1,134 @@
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
+ // ----- scope -----
89
+
90
+ const scope = emitter.scope("x/y/z")
91
+
92
+ scope.on("a", (message) => {
93
+ type cases = [
94
+ Expect<Equal<typeof message.type, "a">>,
95
+ Expect<Equal<typeof message.payload, 1>>
96
+ ]
97
+ })
98
+
99
+ scope.on("b", (message) => {
100
+ type cases = [
101
+ Expect<Equal<typeof message.type, "b">>,
102
+ Expect<Equal<typeof message.payload, 2>>
103
+ ]
104
+ })
105
+
106
+ scope.once("a", (message) => {
107
+ type cases = [
108
+ Expect<Equal<typeof message.type, "a">>,
109
+ Expect<Equal<typeof message.payload, 1>>
110
+ ]
111
+ })
112
+
113
+ scope.once("c", (message) => {
114
+ type cases = [
115
+ Expect<Equal<typeof message.type, "c">>,
116
+ Expect<Equal<typeof message.payload, 3>>
117
+ ]
118
+ })
119
+ emitter.on("x/y/z", (message) => {
120
+ type cases = [
121
+ Expect<Equal<typeof message.type, "x/y/z">>,
122
+ Expect<Equal<typeof message.payload, unknown>>
123
+ ]
124
+ })
125
+
126
+ emitter.on("x/y/z/a", (message) => {
127
+ type cases = [
128
+ Expect<Equal<typeof message.type, "x/y/z/a">>,
129
+ Expect<Equal<typeof message.payload, 1>>
130
+ ]
131
+ })
132
+
133
+ })
134
+ })
@@ -1,9 +1,9 @@
1
- import { describe, test, expect } from "vitest"
2
- import { FastEvent } from "../event"
1
+ import { describe, test, expect } from "vitest"
2
+ import { FastEvent } from "../event"
3
3
 
4
4
 
5
5
 
6
- describe("waitfor",()=>{
6
+ describe("waitfor", () => {
7
7
 
8
8
  test('should resolve promise when event is emitted immediately', () => {
9
9
  return new Promise<void>((resolve) => {
@@ -11,23 +11,27 @@ describe("waitfor",()=>{
11
11
  // Arrange
12
12
  const eventType = 'test-event';
13
13
  const expectedPayload = { data: 'test data' };
14
-
14
+
15
15
  // Act
16
16
  // Create a promise for waitFor and store it
17
17
  const waitPromise = emitter.waitFor(eventType);
18
-
18
+
19
19
  // Emit the event immediately after calling waitFor
20
20
  emitter.emit(eventType, expectedPayload);
21
-
21
+
22
22
  // Assert
23
23
  // Wait for the promise to resolve and check the result
24
- waitPromise.then(result=>{
25
- expect(result).toEqual(expectedPayload);
24
+ waitPromise.then(result => {
25
+ expect(result).toEqual({
26
+ type: eventType,
27
+ payload: expectedPayload,
28
+ meta: undefined
29
+ });
26
30
  resolve()
27
31
  })
28
-
29
- })
30
- });
32
+
33
+ })
34
+ });
31
35
  test('should handle multiple events waiting simultaneously', async () => {
32
36
  return new Promise<void>((resolve) => {
33
37
  const emitter = new FastEvent();
@@ -35,30 +39,30 @@ describe("waitfor",()=>{
35
39
  const event1Promise = emitter.waitFor('event1');
36
40
  const event2Promise = emitter.waitFor('event2');
37
41
  const event3Promise = emitter.waitFor('event3');
38
-
42
+
39
43
  // Act
40
44
  setTimeout(() => {
41
- emitter.emit('event2', 'payload2');
45
+ emitter.emit('event1', 'payload1');
42
46
  }, 100);
43
-
47
+
44
48
  setTimeout(() => {
45
- emitter.emit('event1', 'payload1');
49
+ emitter.emit('event2', 'payload2');
46
50
  }, 200);
47
-
51
+
48
52
  setTimeout(() => {
49
53
  emitter.emit('event3', 'payload3');
50
54
  }, 300);
51
-
55
+
52
56
  // Assert
53
57
  Promise.all([
54
58
  event1Promise,
55
59
  event2Promise,
56
60
  event3Promise
57
- ]).then(results=>{
61
+ ]).then(results => {
58
62
  expect(results).toEqual([
59
- 'payload1',
60
- 'payload2',
61
- 'payload3'
63
+ { type: 'event1', payload: 'payload1', meta: undefined },
64
+ { type: 'event2', payload: 'payload2', meta: undefined },
65
+ { type: 'event3', payload: 'payload3', meta: undefined }
62
66
  ]);
63
67
  resolve()
64
68
  })
@@ -78,12 +82,12 @@ describe("waitfor",()=>{
78
82
  }, 100);
79
83
 
80
84
  setTimeout(() => {
81
- emitter.emit('event3', 'payload2');
85
+ emitter.emit('event2', 'payload2');
82
86
  }, 300);
83
87
 
84
88
  // Event2 will timeout before emission
85
89
  setTimeout(() => {
86
- emitter.emit('event2', 'payload3');
90
+ emitter.emit('event3', 'payload3');
87
91
  }, 300);
88
92
 
89
93
  // Assert
@@ -94,16 +98,18 @@ describe("waitfor",()=>{
94
98
  ]);
95
99
 
96
100
  expect(results[0].status).toBe('fulfilled');
97
- expect(results[0]).toHaveProperty('value', 'payload1');
98
-
99
- expect(results[1].status).toBe('rejected');
101
+ expect((results[0] as any).value).toEqual({ type: 'event1', payload: 'payload1', meta: undefined });
102
+
103
+
104
+ expect(results[1].status).toBe('rejected');
100
105
  //@ts-ignore
101
106
  expect(results[1].reason).toBeInstanceOf(Error);
102
-
107
+
103
108
  expect(results[2].status).toBe('fulfilled');
104
- expect(results[2]).toHaveProperty('value', 'payload2');
109
+ expect((results[2] as any).value).toEqual({ type: 'event3', payload: 'payload3', meta: undefined });
110
+
105
111
  });
106
-
112
+
107
113
 
108
114
 
109
115
  })