1
- import { deepEqual , defineProperty , Promisify } from 'cosmokit'
1
+ import { defineProperty , Promisify } from 'cosmokit'
2
2
import { Context } from './context'
3
3
import { Fiber , FiberState } from './fiber'
4
- import { symbols } from './utils'
4
+ import { DisposableList , symbols } from './utils'
5
5
6
6
export function isBailed ( value : any ) {
7
7
return value !== null && value !== false && value !== undefined
@@ -19,15 +19,15 @@ declare module './context' {
19
19
/* eslint-disable max-len */
20
20
[ Context . events ] : Events < this>
21
21
parallel < K extends keyof GetEvents < this> > ( name : K , ...args : Parameters < GetEvents < this> [ K ] > ) : Promise < void >
22
- parallel < K extends keyof GetEvents < this> > ( thisArg : ThisType < GetEvents < this> [ K ] > , name : K , ...args : Parameters < GetEvents < this> [ K ] > ) : Promise < void >
22
+ parallel < K extends keyof GetEvents < this> > ( thisArg : NoInfer < ThisType < GetEvents < this> [ K ] > > , name : K , ...args : Parameters < GetEvents < this> [ K ] > ) : Promise < void >
23
23
emit < K extends keyof GetEvents < this> > ( name : K , ...args : Parameters < GetEvents < this> [ K ] > ) : void
24
- emit < K extends keyof GetEvents < this> > ( thisArg : ThisType < GetEvents < this> [ K ] > , name : K , ...args : Parameters < GetEvents < this> [ K ] > ) : void
24
+ emit < K extends keyof GetEvents < this> > ( thisArg : NoInfer < ThisType < GetEvents < this> [ K ] > > , name : K , ...args : Parameters < GetEvents < this> [ K ] > ) : void
25
25
serial < K extends keyof GetEvents < this> > ( name : K , ...args : Parameters < GetEvents < this> [ K ] > ) : Promisify < ReturnType < GetEvents < this> [ K ] > >
26
- serial < K extends keyof GetEvents < this> > ( thisArg : ThisType < GetEvents < this> [ K ] > , name : K , ...args : Parameters < GetEvents < this> [ K ] > ) : Promisify < ReturnType < GetEvents < this> [ K ] > >
26
+ serial < K extends keyof GetEvents < this> > ( thisArg : NoInfer < ThisType < GetEvents < this> [ K ] > > , name : K , ...args : Parameters < GetEvents < this> [ K ] > ) : Promisify < ReturnType < GetEvents < this> [ K ] > >
27
27
bail < K extends keyof GetEvents < this> > ( name : K , ...args : Parameters < GetEvents < this> [ K ] > ) : ReturnType < GetEvents < this> [ K ] >
28
- bail < K extends keyof GetEvents < this> > ( thisArg : ThisType < GetEvents < this> [ K ] > , name : K , ...args : Parameters < GetEvents < this> [ K ] > ) : ReturnType < GetEvents < this> [ K ] >
28
+ bail < K extends keyof GetEvents < this> > ( thisArg : NoInfer < ThisType < GetEvents < this> [ K ] > > , name : K , ...args : Parameters < GetEvents < this> [ K ] > ) : ReturnType < GetEvents < this> [ K ] >
29
29
waterfall < K extends keyof GetEvents < this> > ( name : K , ...args : Parameters < GetEvents < this> [ K ] > ) : ReturnType < GetEvents < this> [ K ] >
30
- waterfall < K extends keyof GetEvents < this> > ( thisArg : ThisType < GetEvents < this> [ K ] > , name : K , ...args : Parameters < GetEvents < this> [ K ] > ) : ReturnType < GetEvents < this> [ K ] >
30
+ waterfall < K extends keyof GetEvents < this> > ( thisArg : NoInfer < ThisType < GetEvents < this> [ K ] > > , name : K , ...args : Parameters < GetEvents < this> [ K ] > ) : ReturnType < GetEvents < this> [ K ] >
31
31
on < K extends keyof GetEvents < this> > ( name : K , listener : GetEvents < this> [ K ] , options ?: boolean | EventOptions ) : ( ) => boolean
32
32
once < K extends keyof GetEvents < this> > ( name : K , listener : GetEvents < this> [ K ] , options ?: boolean | EventOptions ) : ( ) => boolean
33
33
/* eslint-enable max-len */
@@ -53,16 +53,11 @@ export class EventsService<C extends Context = Context> {
53
53
noShadow : true ,
54
54
} )
55
55
56
- // TODO: deprecate these events
57
56
this . on ( 'internal/listener' , function ( this : Context , name , listener , options : EventOptions ) {
58
- if ( name === 'ready' ) {
59
- Promise . resolve ( ) . then ( listener )
60
- return ( ) => false
61
- } else if ( name === 'dispose' ) {
62
- defineProperty ( listener , 'name' , 'event <dispose>' )
63
- return this . fiber . _disposables . push ( listener )
64
- } else if ( name === 'internal/update' && ! options . global ) {
65
- return this . fiber . acceptors . push ( listener )
57
+ if ( name === 'internal/update' && ! options . global ) {
58
+ const hooks = this . fiber . _hooks [ 'internal/update' ] ??= new DisposableList ( )
59
+ const method = options . prepend ? 'unshift' : 'push'
60
+ return hooks [ method ] ( listener )
66
61
}
67
62
} )
68
63
@@ -74,12 +69,14 @@ export class EventsService<C extends Context = Context> {
74
69
} )
75
70
}
76
71
77
- this . on ( 'internal/update' , ( fiber : Fiber , config ) => {
78
- for ( const acceptor of fiber . acceptors ) {
79
- if ( acceptor . call ( fiber , config ) ) return true
72
+ this . on ( 'internal/update' , function ( config , _next ) {
73
+ const cbs = [ ...this . _hooks [ 'internal/update' ] || [ ] ]
74
+ const next = ( ) => {
75
+ const cb = cbs . shift ( ) ?? _next
76
+ return cb . call ( this , config , next )
80
77
}
81
- return deepEqual ( fiber . config , config )
82
- } , { global : true } )
78
+ return next ( )
79
+ } , { global : true , prepend : true } )
83
80
}
84
81
85
82
dispatch ( type : string , args : any [ ] ) {
@@ -175,7 +172,7 @@ export interface Events<in C extends Context = Context> {
175
172
'internal/error' ( this : C , format : any , ...param : any [ ] ) : void
176
173
'internal/warn' ( this : C , format : any , ...param : any [ ] ) : void
177
174
'internal/service' ( this : C , name : string , value : any ) : void
178
- 'internal/update' ( fiber : Fiber < C > , config : any ) : boolean | void
175
+ 'internal/update' ( this : Fiber < C > , config : any , next : ( ) => void ) : void
179
176
'internal/get' ( ctx : C , name : string , error : Error , next : ( ) => any ) : any
180
177
'internal/set' ( ctx : C , name : string , value : any , error : Error , next : ( ) => boolean ) : boolean
181
178
'internal/listener' ( this : C , name : string , listener : any , prepend : boolean ) : void
0 commit comments