fastevent 1.0.4 → 1.1.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 (53) hide show
  1. package/.prettierrc.js +20 -0
  2. package/.vscode/settings.json +18 -0
  3. package/CHANGELOG.md +22 -6
  4. package/dist/devTools.d.mts +308 -0
  5. package/dist/devTools.d.ts +308 -0
  6. package/dist/devTools.js +3 -0
  7. package/dist/devTools.js.map +1 -0
  8. package/dist/devTools.mjs +3 -0
  9. package/dist/devTools.mjs.map +1 -0
  10. package/dist/index.d.mts +40 -17
  11. package/dist/index.d.ts +40 -17
  12. package/dist/index.js +1 -1
  13. package/dist/index.js.map +1 -1
  14. package/dist/index.mjs +1 -1
  15. package/dist/index.mjs.map +1 -1
  16. package/example/README.md +54 -0
  17. package/example/eslint.config.js +28 -0
  18. package/example/index.html +13 -0
  19. package/example/package.json +29 -0
  20. package/example/pnpm-lock.yaml +2047 -0
  21. package/example/public/vite.svg +1 -0
  22. package/example/src/App.css +42 -0
  23. package/example/src/App.tsx +60 -0
  24. package/example/src/assets/react.svg +1 -0
  25. package/example/src/index.css +68 -0
  26. package/example/src/main.tsx +10 -0
  27. package/example/src/vite-env.d.ts +1 -0
  28. package/example/tsconfig.app.json +26 -0
  29. package/example/tsconfig.json +7 -0
  30. package/example/tsconfig.node.json +24 -0
  31. package/example/vite.config.ts +7 -0
  32. package/package.json +15 -2
  33. package/readme.md +275 -66
  34. package/readme_cn.md +275 -70
  35. package/src/__tests__/emit.test.ts +68 -69
  36. package/src/__tests__/emitAsync.test.ts +41 -42
  37. package/src/__tests__/many.test.ts +15 -16
  38. package/src/__tests__/meta.test.ts +19 -19
  39. package/src/__tests__/off.test.ts +162 -162
  40. package/src/__tests__/onany.test.ts +97 -98
  41. package/src/__tests__/once.test.ts +42 -43
  42. package/src/__tests__/retain.test.ts +36 -36
  43. package/src/__tests__/scope.test.ts +38 -39
  44. package/src/__tests__/types.test.ts +97 -80
  45. package/src/__tests__/waitFor.test.ts +36 -29
  46. package/src/__tests__/wildcard.test.ts +114 -115
  47. package/src/devTools.ts +166 -0
  48. package/src/event.ts +272 -222
  49. package/src/scope.ts +64 -55
  50. package/src/types.ts +38 -34
  51. package/src/utils/WeakObjectMap.ts +64 -0
  52. package/tsconfig.json +103 -111
  53. package/tsup.config.ts +17 -6
@@ -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: '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: 'z', payload: 'payload3', meta: undefined });
106
106
  });
107
107
 
108
108
 
109
109
  })
110
110
 
111
-
@@ -2,127 +2,144 @@
2
2
  import { describe, test, expect } from "vitest"
3
3
  import type { Equal, Expect, NotAny } from '@type-challenges/utils'
4
4
  import { FastEvent } from "../event"
5
- import { ScopeEvents } from "../types"
5
+ import { ScopeEvents } from "../types"
6
6
 
7
7
 
8
- describe("Types",()=>{
9
- test("Types tests",()=>{
10
- interface CustomEvents{
11
- a : boolean
12
- b : number
13
- c : string,
8
+ describe("Types", () => {
9
+ test("Types tests", () => {
10
+ interface CustomEvents {
11
+ a: boolean
12
+ b: number
13
+ c: string,
14
14
  "x/y/z/a": 1
15
15
  "x/y/z/b": 2
16
- "x/y/z/c":3
16
+ "x/y/z/c": 3
17
17
  }
18
18
 
19
- type ScopeCustomEvents = ScopeEvents<CustomEvents,'x/y/z'>
19
+ type ScopeCustomEvents = ScopeEvents<CustomEvents, 'x/y/z'>
20
20
 
21
21
  type cases = [
22
- Expect<Equal<ScopeCustomEvents,{
23
- a:1
24
- b:2
25
- c:3
22
+ Expect<Equal<ScopeCustomEvents, {
23
+ a: 1
24
+ b: 2
25
+ c: 3
26
26
  }>>
27
- ]
27
+ ]
28
28
 
29
- interface CustomMeta{
30
- a : number
31
- b : string
32
- c : boolean
29
+ interface CustomMeta {
30
+ a: number
31
+ b: string
32
+ c: boolean
33
33
  }
34
34
 
35
- const emitter = new FastEvent<CustomEvents,CustomMeta>()
35
+ const emitter = new FastEvent<CustomEvents, CustomMeta>()
36
36
 
37
- emitter.on("a",(payload,meta)=>{
37
+ emitter.on("a", (message) => {
38
+ message.meta
38
39
  type cases = [
39
- Expect<Equal<typeof meta,CustomMeta & {
40
- type:'a'
41
- }>>,
42
- ]
43
- meta.a
44
- meta.b
40
+ Expect<Equal<typeof message.meta, CustomMeta>>
41
+ ]
45
42
  })
46
43
 
47
- emitter.on("a",(payload,{type})=>{
44
+ emitter.on("a", (message) => {
48
45
  type cases = [
49
- Expect<Equal<typeof type,"a">>,
50
- Expect<Equal<typeof payload,boolean>>
51
- ]
46
+ Expect<Equal<typeof message.type, "a">>,
47
+ Expect<Equal<typeof message.payload, boolean>>
48
+ ]
52
49
  })
53
50
 
54
- emitter.onAny((payload,{type})=>{
51
+ emitter.onAny((message) => {
55
52
  type cases = [
56
- Expect<Equal<typeof type,string>>,
57
- Expect<Equal<typeof payload,any>>
58
- ]
53
+ Expect<Equal<typeof message.type, string>>,
54
+ Expect<Equal<typeof message.payload, any>>
55
+ ]
59
56
  })
60
57
 
61
- emitter.on("b",(payload,{type})=>{
58
+ emitter.onAny<number>((message) => {
62
59
  type cases = [
63
- Expect<Equal<typeof type,"b">>,
64
- Expect<Equal<typeof payload,number>>
65
- ]
60
+ Expect<Equal<typeof message.type, string>>,
61
+ Expect<Equal<typeof message.payload, number>>
62
+ ]
66
63
  })
67
64
 
68
- emitter.once("a",(payload,{type})=>{
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) => {
69
74
  type cases = [
70
- Expect<Equal<typeof type,"a">>,
71
- Expect<Equal<typeof payload,boolean>>
72
- ]
75
+ Expect<Equal<typeof message.type, "a">>,
76
+ Expect<Equal<typeof message.payload, boolean>>
77
+ ]
73
78
  })
74
79
 
75
- emitter.once("b",(payload,{type})=>{
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) => {
76
102
  type cases = [
77
- Expect<Equal<typeof type,"b">>,
78
- Expect<Equal<typeof payload,number>>
79
- ]
103
+ Expect<Equal<typeof message.type, "x/y/z/a">>,
104
+ Expect<Equal<typeof message.payload, 1>>
105
+ ]
80
106
  })
81
- emitter.emit("x/y/z",1)
107
+
108
+
82
109
 
83
110
  // ----- scope -----
84
111
 
85
112
  const scope = emitter.scope("x/y/z")
86
113
 
87
- scope.on("a",(payload,{type})=>{
114
+ scope.on("a", (message) => {
88
115
  type cases = [
89
- Expect<Equal<typeof type,"a">>,
90
- Expect<Equal<typeof payload,1>>
91
- ]
92
- })
116
+ Expect<Equal<typeof message.type, "a">>,
117
+ Expect<Equal<typeof message.payload, 1>>
118
+ ]
119
+ })
93
120
 
94
- scope.on("b",(payload,{type})=>{
121
+ scope.on("b", (message) => {
95
122
  type cases = [
96
- Expect<Equal<typeof type,"b">>,
97
- Expect<Equal<typeof payload,2>>
98
- ]
99
- })
123
+ Expect<Equal<typeof message.type, "b">>,
124
+ Expect<Equal<typeof message.payload, 2>>
125
+ ]
126
+ })
100
127
 
101
- scope.once("a",(payload,{type})=>{
128
+ scope.once("a", (message) => {
102
129
  type cases = [
103
- Expect<Equal<typeof type,"a">>,
104
- Expect<Equal<typeof payload,1>>
105
- ]
106
- })
130
+ Expect<Equal<typeof message.type, "a">>,
131
+ Expect<Equal<typeof message.payload, 1>>
132
+ ]
133
+ })
107
134
 
108
- scope.once("c",(payload,{type})=>{
135
+ scope.once("c", (message) => {
109
136
  type cases = [
110
- Expect<Equal<typeof type,"c">>,
111
- Expect<Equal<typeof payload,3>>
112
- ]
113
- })
114
- emitter.on("x/y/z",(payload,{type})=>{
115
- type cases = [
116
- Expect<Equal<typeof type,"x/y/z">>,
117
- Expect<Equal<typeof payload,unknown>>
118
- ]
137
+ Expect<Equal<typeof message.type, "c">>,
138
+ Expect<Equal<typeof message.payload, 3>>
139
+ ]
119
140
  })
120
- emitter.on("x/y/z/a",(payload,{type})=>{
121
- type cases = [
122
- Expect<Equal<typeof type,"x/y/z/a">>,
123
- Expect<Equal<typeof payload,1>>
124
- ]
125
- })
126
-
127
- })
141
+
142
+
143
+
144
+ })
128
145
  })
@@ -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,31 @@ 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
+
56
+
52
57
  // Assert
53
58
  Promise.all([
54
59
  event1Promise,
55
60
  event2Promise,
56
61
  event3Promise
57
- ]).then(results=>{
62
+ ]).then(results => {
58
63
  expect(results).toEqual([
59
- 'payload1',
60
- 'payload2',
61
- 'payload3'
64
+ { type: 'event1', payload: 'payload1', meta: undefined },
65
+ { type: 'event2', payload: 'payload2', meta: undefined },
66
+ { type: 'event3', payload: 'payload3', meta: undefined }
62
67
  ]);
63
68
  resolve()
64
69
  })
@@ -78,12 +83,12 @@ describe("waitfor",()=>{
78
83
  }, 100);
79
84
 
80
85
  setTimeout(() => {
81
- emitter.emit('event3', 'payload2');
86
+ emitter.emit('event2', 'payload2');
82
87
  }, 300);
83
88
 
84
89
  // Event2 will timeout before emission
85
90
  setTimeout(() => {
86
- emitter.emit('event2', 'payload3');
91
+ emitter.emit('event3', 'payload3');
87
92
  }, 300);
88
93
 
89
94
  // Assert
@@ -94,16 +99,18 @@ describe("waitfor",()=>{
94
99
  ]);
95
100
 
96
101
  expect(results[0].status).toBe('fulfilled');
97
- expect(results[0]).toHaveProperty('value', 'payload1');
98
-
99
- expect(results[1].status).toBe('rejected');
102
+ expect((results[0] as any).value).toEqual({ type: 'event1', payload: 'payload1', meta: undefined });
103
+
104
+
105
+ expect(results[1].status).toBe('rejected');
100
106
  //@ts-ignore
101
107
  expect(results[1].reason).toBeInstanceOf(Error);
102
-
108
+
103
109
  expect(results[2].status).toBe('fulfilled');
104
- expect(results[2]).toHaveProperty('value', 'payload2');
110
+ expect((results[2] as any).value).toEqual({ type: 'event3', payload: 'payload3', meta: undefined });
111
+
105
112
  });
106
-
113
+
107
114
 
108
115
 
109
116
  })