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.
package/src/scope.ts CHANGED
@@ -1,88 +1,89 @@
1
1
  import { FastEvent } from "./event";
2
- import { FastEventListener, FastEventListenOptions, FastEvents, FastEventSubscriber } from "./types";
2
+ import { FastEventListener, FastEventListenOptions, FastEventMessage, FastEvents, FastEventSubscriber } from "./types";
3
3
 
4
4
  export class FastEventScope<
5
- Events extends FastEvents = FastEvents,
6
- Types extends keyof Events = keyof Events,
7
- Meta = unknown
8
- >{
9
- constructor(public emitter:FastEvent<Events,Types,Meta>,public prefix:string){
10
- if(prefix.length>0 && !prefix.endsWith(emitter.options.delimiter!)){
5
+ Events extends FastEvents = FastEvents,
6
+ Meta extends Record<string, any> = Record<string, any>,
7
+ Types extends keyof Events = keyof Events
8
+ > {
9
+ constructor(public emitter: FastEvent<Events, Meta, Types>, public prefix: string) {
10
+ if (prefix.length > 0 && !prefix.endsWith(emitter.options.delimiter!)) {
11
11
  this.prefix = prefix + emitter.options.delimiter
12
- }
12
+ }
13
13
  }
14
- private _getScopeListener(listener:FastEventListener):FastEventListener{
14
+ private _getScopeListener(listener: FastEventListener): FastEventListener {
15
15
  const scopePrefix = this.prefix
16
- if(scopePrefix.length===0) return listener
17
- const scopeListener = function(payload:any,{type}:{type:string}){
18
- if(type.startsWith(scopePrefix)){
19
- type = type.substring(scopePrefix.length)
16
+ if (scopePrefix.length === 0) return listener
17
+ const scopeListener = function (message: FastEventMessage) {
18
+ if (message.type.startsWith(scopePrefix)) {
19
+ return listener(Object.assign({}, message, {
20
+ type: message.type.substring(scopePrefix.length)
21
+ }))
20
22
  }
21
- return listener(payload,{type})
22
- }
23
+ }
23
24
  // 当启用scope时对监听器进行包装
24
25
  //@ts-ignore
25
26
  listener.__wrappedListener = scopeListener
26
27
  return listener
27
28
  }
28
- private _getScopeType(type:string){
29
- return type===undefined ? undefined : this.prefix + type
30
- }
31
-
32
- public on<T extends string>(type: T, listener: FastEventListener<T,Events[T],Meta>, options?:FastEventListenOptions ): FastEventSubscriber
33
- public on<T extends Types=Types>(type: T, listener: FastEventListener<T,Events[T],Meta>, options?:FastEventListenOptions): FastEventSubscriber
34
- public on(type: '**', listener: FastEventListener<any,any,Meta>): FastEventSubscriber
35
- public on(): FastEventSubscriber{
36
- const args = [...arguments] as [any,any,any]
37
- args[0] = this._getScopeType(args[0])
38
- args[1] = this._getScopeListener(args[1])
29
+ private _getScopeType(type: string) {
30
+ return type === undefined ? undefined : this.prefix + type
31
+ }
32
+
33
+ public on<T extends string>(type: T, listener: FastEventListener<T, Events[T], Meta>, options?: FastEventListenOptions): FastEventSubscriber
34
+ public on<T extends Types = Types>(type: T, listener: FastEventListener<T, Events[T], Meta>, options?: FastEventListenOptions): FastEventSubscriber
35
+ public on(type: '**', listener: FastEventListener<any, any, Meta>): FastEventSubscriber
36
+ public on(): FastEventSubscriber {
37
+ const args = [...arguments] as [any, any, any]
38
+ args[0] = this._getScopeType(args[0])
39
+ args[1] = this._getScopeListener(args[1])
39
40
  return this.emitter.on(...args)
40
41
  }
41
42
 
42
- public once<T extends string>(type: T, listener: FastEventListener<T,Events[T],Meta>, options?:FastEventListenOptions ): FastEventSubscriber
43
- public once<T extends Types = Types>(type: T, listener: FastEventListener<Types,Events[T],Meta>, options?:FastEventListenOptions ): FastEventSubscriber
44
- public once(): FastEventSubscriber{
45
- return this.on(arguments[0],arguments[1],Object.assign({},arguments[2],{count:1}))
43
+ public once<T extends string>(type: T, listener: FastEventListener<T, Events[T], Meta>, options?: FastEventListenOptions): FastEventSubscriber
44
+ public once<T extends Types = Types>(type: T, listener: FastEventListener<Types, Events[T], Meta>, options?: FastEventListenOptions): FastEventSubscriber
45
+ public once(): FastEventSubscriber {
46
+ return this.on(arguments[0], arguments[1], Object.assign({}, arguments[2], { count: 1 }))
46
47
  }
47
48
 
48
- onAny<P=any>(listener: FastEventListener<Types,P,Meta>, options?:FastEventListenOptions): FastEventSubscriber {
49
+ onAny<P = any>(listener: FastEventListener<Types, P, Meta>, options?: FastEventListenOptions): FastEventSubscriber {
49
50
  const type = this.prefix + '**'
50
- return this.on(type as any,listener,options)
51
- }
52
- offAll(){
51
+ return this.on(type as any, listener, options)
52
+ }
53
+ offAll() {
53
54
  this.emitter.offAll(this.prefix)
54
- }
55
- off(listener: FastEventListener<any, any,any>):void
56
- off(type: string, listener: FastEventListener<any, any, any>):void
57
- off(type: Types, listener: FastEventListener<any, any, any>):void
58
- off(type: string):void
59
- off(type: Types):void
60
- off(){
61
- const args = arguments as unknown as [any,any]
62
- if(typeof(args[0])==='string'){
55
+ }
56
+ off(listener: FastEventListener<any, any, any>): void
57
+ off(type: string, listener: FastEventListener<any, any, any>): void
58
+ off(type: Types, listener: FastEventListener<any, any, any>): void
59
+ off(type: string): void
60
+ off(type: Types): void
61
+ off() {
62
+ const args = arguments as unknown as [any, any]
63
+ if (typeof (args[0]) === 'string') {
63
64
  args[0] = this._getScopeType(args[0])
64
65
  }
65
66
  this.emitter.off(...args)
66
- }
67
- clear(){
67
+ }
68
+ clear() {
68
69
  this.offAll()
69
70
  }
70
- public emit<R=any>(type:string,payload?:any,retain?:boolean):R[]
71
- public emit<R=any>(type:Types,payload?:Events[Types],retain?:boolean):R[]
72
- public emit<R=any>():R[]{
71
+ public emit<R = any>(type: string, payload?: any, retain?: boolean): R[]
72
+ public emit<R = any>(type: Types, payload?: Events[Types], retain?: boolean): R[]
73
+ public emit<R = any>(): R[] {
73
74
  const type = arguments[0] as string
74
- const payload = arguments[1]
75
+ const payload = arguments[1]
75
76
  const retain = arguments[2] as boolean
76
- return this.emitter.emit(this._getScopeType(type)!,payload,retain)
77
+ return this.emitter.emit(this._getScopeType(type)!, payload, retain)
77
78
  }
78
- public waitFor<R=any>(type:string,timeout?:number):Promise<R>
79
- public waitFor<R=any>(type:Types,timeout?:number):Promise<R>
80
- public waitFor<R=any>():Promise<R>{
79
+ public waitFor<R = any>(type: string, timeout?: number): Promise<R>
80
+ public waitFor<R = any>(type: Types, timeout?: number): Promise<R>
81
+ public waitFor<R = any>(): Promise<R> {
81
82
  const type = arguments[0] as string
82
83
  const timeout = arguments[1] as number
83
- return this.emitter.waitFor(this._getScopeType(type)!,timeout)
84
+ return this.emitter.waitFor(this._getScopeType(type)!, timeout)
84
85
  }
85
- public scope(prefix:string){
86
+ public scope(prefix: string) {
86
87
  return this.emitter.scope(this._getScopeType(prefix)!)
87
88
  }
88
89
  }
package/src/types.ts CHANGED
@@ -1,57 +1,62 @@
1
1
 
2
- export type FastEventMeta<T=string,M=unknown> = M & {type:T}
2
+
3
+
4
+ export type FastEventMessage<T = string, P = any, M = unknown> = {
5
+ type: T
6
+ payload: P
7
+ meta: M
8
+ }
3
9
 
4
10
 
5
11
  export type FastEventListener<
6
- T = string,
7
- P = any,
12
+ T = string,
13
+ P = any,
8
14
  M = unknown
9
- > = ( payload:P, meta:FastEventMeta<T,M> ) => any | Promise<any>
15
+ > = (message: FastEventMessage<T, P, M>) => any | Promise<any>
10
16
 
11
- export type FastListenerNode = {
12
- __listeners: (FastEventListener<any,any,any> | [FastEventListener<any,any>,number])[];
17
+ export type FastListenerNode = {
18
+ __listeners: (FastEventListener<any, any, any> | [FastEventListener<any, any>, number])[];
13
19
  } & {
14
- [key:string]: FastListenerNode
20
+ [key: string]: FastListenerNode
15
21
  }
16
22
 
17
23
  export type FastEventSubscriber = {
18
- off:()=>void
19
- }
24
+ off: () => void
25
+ }
20
26
 
21
- export interface FastEventListenerMeta{
27
+ export interface FastEventListenerMeta {
22
28
  emitter?: string
23
- }
29
+ }
24
30
 
25
31
  export type FastListeners = FastListenerNode
26
32
 
27
- export type FastEventOptions<M=unknown> = {
33
+ export type FastEventOptions<M = unknown> = {
28
34
  // 事件分隔符
29
35
  delimiter?: string
30
36
  // 侦听器函数执行上下文
31
- context? : any
37
+ context?: any
32
38
  // 当执行侦听器函数出错时是否忽略,默认true
33
- ignoreErrors?: boolean
39
+ ignoreErrors?: boolean
34
40
  // 当侦听器函数执行出错时的回调,用于诊断时使用,可以打印错误信息
35
- onListenerError?: ((type:string,error:Error)=>void)
36
- // 额外的元数据,当触发事件时传递给侦听器
37
- meta?:M
41
+ onListenerError?: ((type: string, error: Error) => void)
42
+ // 额外的全局元数据,当触发事件时传递给侦听器
43
+ meta?: M
38
44
  // 当创建新侦听器时回调
39
- onAddListener?:(type:string[],listener:FastEventListener)=>void
45
+ onAddListener?: (type: string[], listener: FastEventListener) => void
40
46
  // 当移除侦听器时回调
41
- onRemoveListener?:(type:string[],listener:FastEventListener)=>void
42
- }
47
+ onRemoveListener?: (type: string[], listener: FastEventListener) => void
48
+ }
49
+
50
+ export type FastEvents = Record<string, any>
43
51
 
44
- export type FastEvents = Record<string,any>
45
52
 
46
53
  export type ScopeEvents<T extends Record<string, any>, Prefix extends string> = {
47
54
  [K in keyof T as K extends `${Prefix}/${infer Rest}` ? Rest : never]: T[K];
48
- };
55
+ };
49
56
 
50
-
51
-
52
- export type FastEventListenOptions={
57
+ export type FastEventListenOptions = {
53
58
  // 侦听执行次数,当为1时为单次侦听,为0时为永久侦听,其他值为执行次数,每执行一次减一,减到0时移除侦听器
54
- count?:number
59
+ count?: number
55
60
  // 将侦听器添加到侦听器列表的头部
56
- prepend?:boolean
57
- }
61
+ prepend?: boolean
62
+ }
package/src/typestest.ts DELETED
@@ -1,102 +0,0 @@
1
- // eslint-disable no-unused-vars
2
- import type { Equal, Expect, NotAny } from '@type-challenges/utils'
3
- import { FastEvent } from "./event"
4
- import { ScopeEvents } from "./types"
5
-
6
-
7
-
8
- interface CustomEvents{
9
- a : boolean
10
- b : number
11
- c : string,
12
- "x/y/z/a": 1
13
- "x/y/z/b": 2
14
- "x/y/z/c":3
15
- }
16
-
17
- type ScopeCustomEvents = ScopeEvents<CustomEvents,'x/y/z'>
18
-
19
- type cases = [
20
- Expect<Equal<ScopeCustomEvents,{
21
- a:1
22
- b:2
23
- c:3
24
- }>>
25
- ]
26
-
27
-
28
- const emitter = new FastEvent<CustomEvents>()
29
-
30
- emitter.on("a",(payload,{type})=>{
31
- type cases = [
32
- Expect<Equal<typeof type,"a">>,
33
- Expect<Equal<typeof payload,boolean>>
34
- ]
35
- })
36
-
37
- emitter.on("b",(payload,{type})=>{
38
- type cases = [
39
- Expect<Equal<typeof type,"b">>,
40
- Expect<Equal<typeof payload,number>>
41
- ]
42
- })
43
-
44
- emitter.once("a",(payload,{type})=>{
45
- type cases = [
46
- Expect<Equal<typeof type,"a">>,
47
- Expect<Equal<typeof payload,boolean>>
48
- ]
49
- })
50
-
51
- emitter.once("b",(payload,{type})=>{
52
- type cases = [
53
- Expect<Equal<typeof type,"b">>,
54
- Expect<Equal<typeof payload,number>>
55
- ]
56
- })
57
- emitter.emit("x/y/z",1)
58
-
59
-
60
- // ----- scope -----
61
-
62
- const scope = emitter.scope("x/y/z")
63
-
64
- scope.on("a",(payload,{type})=>{
65
- type cases = [
66
- Expect<Equal<typeof type,"a">>,
67
- Expect<Equal<typeof payload,1>>
68
- ]
69
- })
70
-
71
- scope.on("b",(payload,{type})=>{
72
- type cases = [
73
- Expect<Equal<typeof type,"b">>,
74
- Expect<Equal<typeof payload,2>>
75
- ]
76
- })
77
-
78
- scope.once("a",(payload,{type})=>{
79
- type cases = [
80
- Expect<Equal<typeof type,"a">>,
81
- Expect<Equal<typeof payload,1>>
82
- ]
83
- })
84
-
85
- scope.once("c",(payload,{type})=>{
86
- type cases = [
87
- Expect<Equal<typeof type,"c">>,
88
- Expect<Equal<typeof payload,3>>
89
- ]
90
- })
91
- emitter.on("x/y/z",(payload,{type})=>{
92
- type cases = [
93
- Expect<Equal<typeof type,"x/y/z">>,
94
- Expect<Equal<typeof payload,unknown>>
95
- ]
96
- })
97
- emitter.on("x/y/z/a",(payload,{type})=>{
98
- type cases = [
99
- Expect<Equal<typeof type,"x/y/z/a">>,
100
- Expect<Equal<typeof payload,1>>
101
- ]
102
- })