Skip to content

Commit 82aaf08

Browse files
committed
feat: add FastMessage type
1 parent b639473 commit 82aaf08

8 files changed

Lines changed: 130 additions & 5 deletions

File tree

docs/zh/guide/use/typescript.md

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,66 @@ declare module 'fastevent' {
6464
}
6565
```
6666

67+
68+
## 消息类型
69+
70+
`FastEvent`监听器接收到的是`FastEventMessage`类型,该类型具有类型推断能力。
71+
72+
73+
```ts twoslash
74+
import { FastEvent } from 'fastevent';
75+
76+
type CustomEvents = {
77+
click: { x: number; y: number };
78+
mousemove: boolean;
79+
scroll: number;
80+
focus: string;
81+
};
82+
const emitter = new FastEvent<CustomEvents>();
83+
84+
emitter.on('click', (message) => {
85+
type MessageType = typeof message;
86+
message.type; // 'click'
87+
message.payload; // { x: number; y: number }
88+
});
89+
90+
```
91+
92+
`FastEvent`还额外提供了一个`FastMessage`类型用于不需要类型约束的场景。
93+
94+
```ts twoslash
95+
import { FastEvent,FastMessage } from 'fastevent';
96+
97+
type CustomEvents = {
98+
click: { x: number; y: number };
99+
mousemove: boolean;
100+
scroll: number;
101+
focus: string;
102+
};
103+
const emitter = new FastEvent<CustomEvents>();
104+
105+
// 构建类型推断和约束的消息
106+
type MessageType = typeof emitter.types.message
107+
const typedMessage:MessageType = {
108+
type:"click",
109+
payload: {
110+
x:100,
111+
y:100
112+
}
113+
}
114+
// 构建通用的消息
115+
const message:FastMessage = {
116+
type:"click",
117+
payload: 100
118+
}
119+
emitter.emit(message)
120+
121+
emitter.on('click', (message) => {
122+
123+
})
124+
125+
```
126+
67127
## 元数据类型
68128

69129
**`FastEvent`可以能自动推断全局元数据类型。**
@@ -191,6 +251,7 @@ const emitter = new FastEvent<CustomEvents, CustomMeta, CustomContext>({
191251
type EventType = typeof emitter.types.events;
192252
type MetaType = typeof emitter.types.meta;
193253
type ContextType = typeof emitter.types.context;
254+
type MessageType = typeof emitter.types.message;
194255

195256
```
196257

packages/native/src/__tests__/meta.test.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,16 @@ import { describe, test, expect } from "vitest"
22
import { FastEvent } from "../event"
33

44
describe("传递Meta数据", async () => {
5+
test("没有meta数据", () => {
6+
const emitter = new FastEvent()
7+
emitter.on("x", ({ payload, type, meta }, args) => {
8+
expect(type).toBe("x")
9+
expect(payload).toBe(1)
10+
expect(meta).toBeUndefined()
11+
expect(args.meta).toBeUndefined()
12+
})
13+
emitter.emit("x", 1)
14+
})
515
test("全局meta数据", () => {
616
const emitter = new FastEvent({
717
meta: { a: 1 }

packages/native/src/__tests__/types/emit.types.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { describe, test, expect } from "vitest"
33
import type { Equal, Expect, NotAny } from '@type-challenges/utils'
44
import { FastEvent } from "../../event"
55
import { FastEventScopeMeta } from "../../scope"
6+
import { FastMessage } from "../../types"
67

78
describe("emit类型系统测试", () => {
89
interface CustomEvents {

packages/native/src/__tests__/types/scope.types.test.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { describe, test, expect } from "vitest"
44
import type { Equal, Expect, NotAny } from '@type-challenges/utils'
55
import { FastEvent } from "../../event"
66
import { FastEventScope, FastEventScopeMeta, FastEventScopeOptions } from "../../scope"
7-
import { ChangeFieldType, FastEventMeta, FastEventOptions, FastEvents, PickScopeEvents, RequiredItems } from "../../types"
7+
import { ChangeFieldType, FastEventMeta, FastEventOptions, FastEvents, FastMessage, PickScopeEvents, RequiredItems } from "../../types"
88

99
declare module "../../types" {
1010
interface FastEventMeta {
@@ -38,6 +38,7 @@ describe("事件作用域类型测试", () => {
3838
})
3939

4040

41+
4142
})
4243
test("scope事件类型测试", () => {
4344
type CustomScopeEvents = {

packages/native/src/__tests__/types/types.test.ts

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import { describe, test, expect } from "vitest"
44
import type { Equal, Expect } from '@type-challenges/utils'
55
import { FastEvent } from "../../event"
6-
import { FastEvents } from "../../types"
6+
import { FastEvents, FastMessage } from "../../types"
77

88

99
describe("types", () => {
@@ -39,7 +39,49 @@ describe("types", () => {
3939
Expect<Equal<typeof emitter.types.context, CustomContext>>,
4040
]
4141
})
42+
test("消息类型约束", () => {
4243

44+
type CustomEvents = {
45+
click: { x: number; y: number };
46+
mousemove: boolean;
47+
scroll: number;
48+
focus: string;
49+
};
50+
const emitter = new FastEvent<CustomEvents>();
51+
52+
// 构建类型推断和约束的消息
53+
type MessageType = typeof emitter.types.message
54+
const typedMessage: MessageType = {
55+
type: "click",
56+
payload: {
57+
x: 100,
58+
y: 100
59+
},
60+
meta: {} as any
61+
}
62+
63+
64+
emitter.emit({
65+
type: "click",
66+
payload: {
67+
x: 100,
68+
y: 100
69+
}
70+
})
71+
72+
73+
74+
// 构建通用的消息
75+
const message: FastMessage = {
76+
type: "click",
77+
payload: 100
78+
}
79+
emitter.emit(message)
80+
81+
emitter.on('click', (message) => {
82+
83+
})
84+
})
4385
})
4486

4587

packages/native/src/event.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ import {
1616
DeepPartial,
1717
FastEventMeta,
1818
Expand,
19-
Dict
19+
Dict,
20+
OptionalItems
2021
} from './types';
2122
import { parseEmitArgs } from './utils/parseEmitArgs';
2223
import { isPathMatched } from './utils/isPathMatched';
@@ -68,7 +69,8 @@ export class FastEvent<
6869
types = {
6970
events: undefined as unknown as AllEvents,
7071
meta: undefined as unknown as Expand<FastEventMeta & Meta & Record<string, any>>,
71-
context: undefined as unknown as Expand<Fallback<Context, typeof this>>
72+
context: undefined as unknown as Expand<Fallback<Context, typeof this>>,
73+
message: undefined as unknown as FastEventMessage<AllEvents, Expand<FastEventMeta & Meta & Record<string, any>>>
7274
}
7375

7476
/**

packages/native/src/scope.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ export class FastEventScope<
3030
types = {
3131
events: undefined as unknown as Events,
3232
meta: undefined as unknown as FinalMeta,
33-
context: undefined as unknown as Fallback<Context, typeof this>
33+
context: undefined as unknown as Fallback<Context, typeof this>,
34+
message: undefined as unknown as FastEventMessage<Events, FinalMeta>
3435
}
3536
prefix: string = ''
3637
emitter!: FastEvent<Events>

packages/native/src/types.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,13 @@ export interface FaseEventMessageExtends {
1010
export interface FastEventMeta {
1111

1212
}
13+
14+
export type FastMessage<P = any> = {
15+
type: string
16+
payload: P
17+
meta?: Record<string, any>
18+
}
19+
1320
export type FastEventMessage<
1421
Events extends Record<string, any> = Record<string, any>,
1522
M = any

0 commit comments

Comments
 (0)