Skip to content

Commit b20e6f2

Browse files
committed
update
1 parent 9521acb commit b20e6f2

17 files changed

Lines changed: 196 additions & 87 deletions

File tree

docs/zh/guide/use/typescript.md

Lines changed: 53 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,13 @@ declare module 'fastevent' {
6767

6868
## 消息类型
6969

70-
`FastEvent`监听器接收到的是`TypedFastEventMessage`类型,该类型具有类型推断能力。
70+
`FastEvent`提供三种类型用于处理类型化的消息:
71+
72+
- `TypedFastEventMessage`类型
73+
- `FastEventMessage`类型
74+
- `typeof FastEvent.types.message`类型
7175

76+
`FastEvent`监听器接收到的是`TypedFastEventMessage`类型,该类型具有类型推断能力。
7277

7378
```ts twoslash
7479
import { FastEvent } from 'fastevent';
@@ -89,7 +94,8 @@ emitter.on('click', (message) => {
8994

9095
```
9196

92-
`FastEvent`还额外提供了一个`FastMessage`类型用于不需要类型约束的场景。
97+
98+
如果需要构建一个受约束的消息,可以使用`typeof FastEvent.types.message`
9399

94100
```ts twoslash
95101
import { FastEvent,FastEventMessage } from 'fastevent';
@@ -100,17 +106,33 @@ type CustomEvents = {
100106
scroll: number;
101107
focus: string;
102108
};
109+
103110
const emitter = new FastEvent<CustomEvents>();
104111

105112
// 构建类型推断和约束的消息
106-
type MessageType = typeof emitter.types.message
113+
type MessageType = typeof emitter.types.message // [!code ++]
107114
const typedMessage:MessageType = {
108115
type:"click",
109116
payload: {
110117
x:100,
111118
y:100
112119
}
113-
}
120+
}
121+
122+
```
123+
`FastEvent`还额外提供了一个`FastEventMessage`类型用于不需要类型约束的场景。
124+
125+
```ts twoslash
126+
import { FastEvent,FastEventMessage } from 'fastevent';
127+
128+
type CustomEvents = {
129+
click: { x: number; y: number };
130+
mousemove: boolean;
131+
scroll: number;
132+
focus: string;
133+
};
134+
const emitter = new FastEvent<CustomEvents>();
135+
114136
// 构建通用的消息,没有类型推断和约束
115137
const message:FastEventMessage = {
116138
type:"click",
@@ -222,6 +244,33 @@ emitter.on('click', function (message) {
222244
//
223245
```
224246

247+
248+
## 监听器类型
249+
250+
`FastEvent`提供以下监听器函数类型。
251+
252+
- `TypedFastEventListener`
253+
254+
255+
```ts twoslash
256+
import { FastEvent,FastEventMessage } from 'fastevent';
257+
258+
type CustomEvents = {
259+
click: { x: number; y: number };
260+
mousemove: boolean;
261+
scroll: number;
262+
focus: string;
263+
};
264+
const emitter = new FastEvent<CustomEvents>();
265+
266+
type ClickListener = typeof emitter.types.listeners['click']
267+
268+
```
269+
270+
- `TypedFastEventAnyListener`
271+
- `FastEventListener`
272+
273+
225274
## 检索类型
226275

227276
`FastEvent``FastEventScope`都提供了`types`对象用于检索事件类型。

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

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { describe, test, expect } from "vitest"
22
import { FastEvent } from "../event"
3-
import { FastEventListener } from '../types';
3+
import { TypedFastEventListener } from '../types';
44

55

66
describe("退订事件", () => {
@@ -91,7 +91,7 @@ describe("退订事件", () => {
9191
test("根据事件类型和监听器进行退订", () => {
9292
const emitter = new FastEvent()
9393
const events: string[] = []
94-
const listener: FastEventListener<string, number> = (({ payload, type }) => {
94+
const listener: TypedFastEventListener<string, number> = (({ payload, type }) => {
9595
expect(type).toBe("x")
9696
expect(payload).toBe(1)
9797
events.push(type)
@@ -105,7 +105,7 @@ describe("退订事件", () => {
105105
test("多级事件根据事件类型和监听器进行退订", () => {
106106
const emitter = new FastEvent()
107107
const events: string[] = []
108-
const listener: FastEventListener<string, number> = (({ payload, type }) => {
108+
const listener: TypedFastEventListener<string, number> = (({ payload, type }) => {
109109
expect(type).toBe("a/b/c/d")
110110
expect(payload).toBe(1)
111111
events.push(type)
@@ -122,7 +122,7 @@ describe("退订事件", () => {
122122
test("通配符事件退订", () => {
123123
const emitter = new FastEvent()
124124
const events: string[] = []
125-
const listener: FastEventListener<string, number> = ({ payload, type }) => {
125+
const listener: TypedFastEventListener<string, number> = ({ payload, type }) => {
126126
expect(type).toBe("a/b/c/d")
127127
expect(payload).toBe(1)
128128
events.push(type)
@@ -139,7 +139,7 @@ describe("退订事件", () => {
139139
test("退订指定的监听器", () => {
140140
const emitter = new FastEvent()
141141
const events: string[] = []
142-
const listener: FastEventListener<string, number> = ({ payload, type }) => {
142+
const listener: TypedFastEventListener<string, number> = ({ payload, type }) => {
143143
expect(type).toBe("a/b/c/d")
144144
expect(payload).toBe(1)
145145
events.push(type)
@@ -159,7 +159,7 @@ describe("退订事件", () => {
159159
test("退订指定的事件时不指定监听器", () => {
160160
const emitter = new FastEvent()
161161
const events: string[] = []
162-
const listener: FastEventListener<string, number> = ({ payload, type }) => {
162+
const listener: TypedFastEventListener<string, number> = ({ payload, type }) => {
163163
events.push(type)
164164
}
165165
emitter.on("a", listener)
@@ -180,7 +180,7 @@ describe("退订事件", () => {
180180
test("退订所有监听器", () => {
181181
const emitter = new FastEvent()
182182
const events: string[] = []
183-
const listener: FastEventListener<string, number> = ({ payload, type }) => {
183+
const listener: TypedFastEventListener<string, number> = ({ payload, type }) => {
184184
events.push(type)
185185
}
186186
emitter.on("a", listener)
@@ -205,7 +205,7 @@ describe("退订事件", () => {
205205
test("退订含通配符的事件", () => {
206206
const emitter = new FastEvent()
207207
const events: string[] = []
208-
const listener: FastEventListener<string, number> = ({ payload, type }) => {
208+
const listener: TypedFastEventListener<string, number> = ({ payload, type }) => {
209209
events.push(type)
210210
}
211211
emitter.on("a", listener)
@@ -230,7 +230,7 @@ describe("退订事件", () => {
230230
test("退订含通配符的事件", () => {
231231
const emitter = new FastEvent()
232232
const events: string[] = []
233-
const listener: FastEventListener<string, number> = ({ payload, type }) => {
233+
const listener: TypedFastEventListener<string, number> = ({ payload, type }) => {
234234
events.push(type)
235235
}
236236
emitter.on("a/*", listener)
@@ -252,7 +252,7 @@ describe("退订事件", () => {
252252
test("退订多层含通配符的事件", () => {
253253
const emitter = new FastEvent()
254254
const events: string[] = []
255-
const listener: FastEventListener<string, number> = ({ payload, type }) => {
255+
const listener: TypedFastEventListener<string, number> = ({ payload, type }) => {
256256
events.push(type)
257257
}
258258
emitter.on("a/*", listener)

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// eslint-disable no-unused-vars
22
import { describe, test, expect } from "vitest"
33
import { FastEvent } from "../event"
4-
import { FastEventListener } from '../types';
4+
import { TypedFastEventListener } from '../types';
55
import { FastEventScope } from "../scope";
66

77

@@ -207,7 +207,7 @@ describe("scope", () => {
207207
const emitter = new FastEvent();
208208
const listener = (({ type }) => {
209209
anyEvents.push(type)
210-
}) as FastEventListener
210+
}) as TypedFastEventListener
211211

212212
emitter.onAny(listener)
213213

@@ -256,7 +256,7 @@ describe("scope", () => {
256256
const emitter = new FastEvent();
257257
const listener = (({ type }) => {
258258
anyEvents.push(type)
259-
}) as FastEventListener
259+
}) as TypedFastEventListener
260260

261261
emitter.onAny(listener)
262262

@@ -313,7 +313,7 @@ describe("scope", () => {
313313
let receiveMeta: any
314314
const listener = (({ type, meta }, args) => {
315315
receiveMeta = meta
316-
}) as FastEventListener
316+
}) as TypedFastEventListener
317317

318318
emitter.onAny(listener)
319319

@@ -352,7 +352,7 @@ describe("scope", () => {
352352
test('nested scope with meta', async () => {
353353
const listener = (({ type, meta }) => {
354354
receiveMeta = meta
355-
}) as FastEventListener
355+
}) as TypedFastEventListener
356356

357357
const emitter = new FastEvent({
358358
meta: { 'root': 1 }

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

Lines changed: 41 additions & 3 deletions
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, FastEventMessageExtends, FastEventMessage } from "../../types"
6+
import { FastEvents, FastEventMessageExtends, FastEventMessage, FastEventListener, TypedFastEventListener } from "../../types"
77

88

99
describe("types", () => {
@@ -110,16 +110,54 @@ describe("types", () => {
110110

111111

112112
// 构建通用的消息
113-
const message: FastEventMessage = {
113+
const message: FastEventMessage<string> = {
114114
type: "click",
115-
payload: 100
115+
payload: "100"
116116
}
117117
emitter.emit(message)
118118

119119
emitter.on('click', (message) => {
120120

121121
})
122122
})
123+
test("所有监听器类型", () => {
124+
125+
type CustomMeta = { x: number; y: number; z?: number };
126+
type CustomEvents = {
127+
click: { x: number; y: number };
128+
mousemove: boolean;
129+
scroll: number;
130+
focus: string;
131+
};
132+
type CustomContext = {
133+
name: string,
134+
age: number
135+
address: string
136+
};
137+
const emitter = new FastEvent<CustomEvents, CustomMeta, CustomContext>({
138+
context: {
139+
name: "hello",
140+
age: 18,
141+
address: "beijing"
142+
}
143+
});
144+
145+
type ListenerTypes = typeof emitter.types.listeners
146+
147+
type cases = [
148+
Expect<Equal<ListenerTypes['click'], TypedFastEventListener<"click", {
149+
x: number;
150+
y: number;
151+
}, {
152+
[x: string]: any;
153+
x: number;
154+
y: number;
155+
z?: number | undefined;
156+
}, any>
157+
>>
158+
]
159+
})
160+
123161
})
124162

125163

0 commit comments

Comments
 (0)