|
3 | 3 | import { describe, test, expect } from 'vitest'; |
4 | 4 | import type { Equal, Expect, NotAny } from '@type-challenges/utils'; |
5 | 5 | import { FastEvent } from '../../event'; |
6 | | -import { FastEventScope, FastEventScopeMeta } from '../../scope'; |
| 6 | +import { FastEventScope, FastEventScopeExtend, FastEventScopeMeta } from '../../scope'; |
7 | 7 | import { ChangeFieldType, FastEventMeta, FastEvents, RequiredItems, ScopeEvents } from '../../types'; |
8 | 8 |
|
9 | 9 | declare module '../../types' { |
@@ -194,14 +194,13 @@ describe('作用域上下文类型系统', () => { |
194 | 194 | }; |
195 | 195 | const emitter = new FastEvent<Events>(); |
196 | 196 |
|
197 | | - class CustomScope extends FastEventScope<ScopeEvents<Events, 'rooms/a'>> { |
| 197 | + class CustomScope extends FastEventScope { |
198 | 198 | test() {} |
199 | 199 | } |
200 | 200 | type S = ScopeEvents<Events, 'rooms/a'>; |
201 | 201 |
|
202 | 202 | function getRoomScope<Prefix extends string>(prefix: Prefix) { |
203 | | - // return emitter.scope(prefix, new CustomScope<`rooms/${Prefix}`>()); |
204 | | - return emitter.scope(prefix, new CustomScope()); |
| 203 | + return emitter.scope(`rooms/${prefix}`, new CustomScope()); |
205 | 204 | } |
206 | 205 |
|
207 | 206 | const scope = getRoomScope('y'); |
@@ -261,4 +260,65 @@ describe('作用域上下文类型系统', () => { |
261 | 260 | b1.test; |
262 | 261 | b1.test2; |
263 | 262 | }); |
| 263 | + test('继承scope类32', () => { |
| 264 | + type Events = { |
| 265 | + 'rooms/*/users/online': { name: string; status?: number }; |
| 266 | + 'rooms/*/users/*/online': { name: string; status?: number }; |
| 267 | + 'rooms/*/users/*/offline': boolean; |
| 268 | + 'rooms/*/posts/**': number; |
| 269 | + 'rooms/*/posts/*/online': number; |
| 270 | + }; |
| 271 | + const emitter = new FastEvent<Events>(); |
| 272 | + |
| 273 | + class CustomScope extends FastEventScope { |
| 274 | + join(name: string) {} |
| 275 | + leave() {} |
| 276 | + } |
| 277 | + type S = ScopeEvents<Events, 'rooms/y'>; |
| 278 | + |
| 279 | + function getRoom<Prefix extends string>(prefix: Prefix) { |
| 280 | + return emitter.scope(`rooms/${prefix}`, new CustomScope()); // as FastEventScopeExtend<Events, `rooms/${Prefix}`, CustomScope>; |
| 281 | + } |
| 282 | + |
| 283 | + const room = getRoom('y'); |
| 284 | + type RoomEvents = typeof room.types.events; |
| 285 | + room.join('fisher'); |
| 286 | + room.leave(); |
| 287 | + room.on('posts/a', (message) => { |
| 288 | + message.type; |
| 289 | + message.payload; |
| 290 | + }); |
| 291 | + room.on('users/online', (message) => { |
| 292 | + type cases = [Expect<Equal<typeof message.type, 'users/online'>>, Expect<Equal<typeof message.payload, { name: string; status?: number }>>]; |
| 293 | + }); |
| 294 | + }); |
| 295 | + |
| 296 | + test('继承scope类4', () => { |
| 297 | + type Events = { |
| 298 | + 'rooms/*/users/online': { name: string; status?: number }; |
| 299 | + 'rooms/*/users/*/online': { name: string; status?: number }; |
| 300 | + 'rooms/*/users/*/offline': boolean; |
| 301 | + 'rooms/*/posts/**': number; |
| 302 | + 'rooms/*/posts/*/online': number; |
| 303 | + }; |
| 304 | + const emitter = new FastEvent<Events>(); |
| 305 | + |
| 306 | + class User extends FastEventScope { |
| 307 | + login(name: string) {} |
| 308 | + logout() {} |
| 309 | + } |
| 310 | + |
| 311 | + const useScope = emitter.scope(`rooms/x`); |
| 312 | + const jack = useScope.scope('users/jack', new User()); |
| 313 | + |
| 314 | + jack.login(''); |
| 315 | + jack.logout(); |
| 316 | + |
| 317 | + jack.on('online', (message) => { |
| 318 | + type cases = [Expect<Equal<typeof message.type, 'online'>>, Expect<Equal<typeof message.payload, { name: string; status?: number }>>]; |
| 319 | + }); |
| 320 | + jack.on('offline', (message) => { |
| 321 | + type cases = [Expect<Equal<typeof message.type, 'offline'>>, Expect<Equal<typeof message.payload, boolean>>]; |
| 322 | + }); |
| 323 | + }); |
264 | 324 | }); |
0 commit comments