@@ -67,7 +67,8 @@ import { getWeakRef } from "./utils/getWeakRef";
6767import { getPromiseResults } from "./utils/getPromiseResults" ;
6868import { isPromise } from "./utils/isPromise" ;
6969import { resolveValue } from "./utils/resolveValue" ;
70- import { FastEventHooks } from "./types/FastEventHooks" ;
70+ import { AfterExecuteListenerHook , FastEventHooks } from "./types/FastEventHooks" ;
71+ import { ItemOf } from "../../viewer/src/types" ;
7172
7273/**
7374 * FastEvent 事件发射器类
@@ -181,21 +182,47 @@ export class FastEvent<
181182 }
182183 return this . _hooks ;
183184 }
185+ private _execAfterExecuteListener ( fn : Function , args : Parameters < AfterExecuteListenerHook > ) {
186+ Promise . allSettled ( args [ 1 ] ) . then ( ( r : any [ ] ) => {
187+ args [ 1 ] = getPromiseResults ( r ) ;
188+ fn ! . apply ( this , args as any ) ;
189+ } ) ;
190+ }
184191 /**
185192 * 执行Hook
186193 * @param hookName
187194 * @param args
188195 * @returns
189196 */
190- private _executeHooks ( hookName : keyof FastEventHooks , args : any [ ] ) {
191- if ( ! this . _hooks ) return ;
192- const hooks = this . hooks [ hookName ] ;
193- if ( Array . isArray ( hooks ) ) {
194- Promise . allSettled (
195- hooks . map ( ( hook ) => {
196- return ( hook as any ) . apply ( this , args ) ;
197- } ) ,
198- ) ;
197+ private _executeHooks < T extends keyof FastEventHooks > (
198+ hookName : T ,
199+ args : Parameters < ItemOf < FastEventHooks [ T ] > > ,
200+ onlyAsyncHook : boolean = false ,
201+ ) {
202+ setTimeout ( ( ) => {
203+ if ( ! this . _hooks ) return ;
204+ const hooks = this . hooks [ hookName ] ;
205+ if ( Array . isArray ( hooks ) && hooks . length > 0 ) {
206+ Promise . allSettled (
207+ hooks . map ( ( hook ) => {
208+ if ( hookName === "AfterExecuteListener" ) {
209+ this . _execAfterExecuteListener ( hook , args as any ) ;
210+ } else {
211+ return ( hook as any ) . apply ( this , args ) ;
212+ }
213+ } ) ,
214+ ) ;
215+ }
216+ } ) ;
217+ if ( ! onlyAsyncHook ) {
218+ const hookMethod = this . options [ `on${ hookName } ` ] as Function ;
219+ if ( isFunction ( hookMethod ) ) {
220+ if ( hookName === "AfterExecuteListener" ) {
221+ this . _execAfterExecuteListener ( hookMethod , args as any ) ;
222+ } else {
223+ return hookMethod . apply ( this , args ) ;
224+ }
225+ }
199226 }
200227 }
201228 /**
@@ -302,30 +329,28 @@ export class FastEvent<
302329 const isRemove = item === listener ;
303330 if ( isRemove ) {
304331 this . listenerCount -- ;
305- if ( isFunction ( this . _options . onRemoveListener ) ) {
306- this . _options . onRemoveListener ( path . join ( this . _delimiter ) , listener ) ;
307- }
332+ this . _executeHooks ( "RemoveListener" , [ path . join ( this . _delimiter ) , listener ] ) ;
308333 }
309334 return isRemove ;
310335 } ) ;
311336 }
312- /**
313- * 调用onAddListener HOOK
314- * @param type
315- * @param listener
316- * @param options
317- * @returns
318- */
319- private _onAddListener ( type : string , listener : any , options : any ) {
320- if ( isFunction ( this . _options . onAddListener ) ) {
321- const r = this . _options . onAddListener ( type , listener , options ) ;
322- if ( r === false ) {
323- throw new CancelError ( ) ;
324- } else if ( isSubsctiber ( r ) ) {
325- return r ;
326- }
327- }
328- }
337+ // / **
338+ // * 调用onAddListener HOOK
339+ // * @param type
340+ // * @param listener
341+ // * @param options
342+ // * @returns
343+ // */
344+ // private _onAddListener(type: string, listener: any, options: any) {
345+ // if (isFunction(this._options.onAddListener)) {
346+ // const r = this._options.onAddListener(type, listener, options);
347+ // if (r === false) {
348+ // throw new CancelError();
349+ // } else if (isSubsctiber(r)) {
350+ // return r;
351+ // }
352+ // }
353+ // }
329354 /**
330355 * 注册事件监听器
331356 * @param type - 事件类型,支持以下格式:
@@ -420,18 +445,18 @@ export class FastEvent<
420445 ) as FastEventIteratorOptions ;
421446 const iterator = createAsyncEventIterator < any > ( this as any , type , iteratorOpts ) ;
422447 iterator . create ( finalOptions ) ;
423- this . _onAddListener ( type , iterator . listener , finalOptions ) ;
448+ this . _executeHooks ( "AddListener" , [ type , iterator . listener , finalOptions ] ) ;
424449 return iterator ;
425450 }
426451 // 执行回调
427- if ( isFunction ( this . _options . onAddListener ) ) {
428- const r = this . _options . onAddListener ( type , listener , finalOptions ) ;
429- if ( r === false ) {
430- throw new CancelError ( ) ;
431- } else if ( isSubsctiber ( r ) ) {
432- return r ;
433- }
452+ // if (isFunction(this._options.onAddListener)) {
453+ const r = this . _executeHooks ( "AddListener" , [ type , listener , finalOptions ] ) ;
454+ if ( r === false ) {
455+ throw new CancelError ( ) ;
456+ } else if ( isSubsctiber ( r ) ) {
457+ return r ;
434458 }
459+ // }
435460
436461 const parts = type . split ( this . _delimiter ) ;
437462
@@ -622,20 +647,32 @@ export class FastEvent<
622647 let count = 0 ;
623648 this . _traverseListeners ( this . listeners , parts , ( path , node ) => {
624649 count += node . __listeners . length ;
625- node . __listeners = [ ] ;
650+ try {
651+ node . __listeners . forEach ( ( listener ) => {
652+ this . _executeHooks ( "RemoveListener" , [
653+ path . join ( this . options . delimiter ) ,
654+ listener [ 0 ] ,
655+ ] ) ;
656+ } ) ;
657+ } finally {
658+ node . __listeners = [ ] ;
659+ }
626660 } ) ;
627661 this . listenerCount -= count ;
628662 this . _removeRetainedEvents ( entry ) ;
629663 } else {
630- let count = 0 ;
631- this . _traverseListeners ( this . listeners , [ ] , ( path , node ) => {
632- count += node . __listeners . length ;
633- } ) ;
634- this . listenerCount -= count ;
635- this . retainedMessages . clear ( ) ;
636- this . listeners = { __listeners : [ ] } as unknown as FastListeners ;
664+ try {
665+ let count = 0 ;
666+ this . _traverseListeners ( this . listeners , [ ] , ( path , node ) => {
667+ count += node . __listeners . length ;
668+ } ) ;
669+ this . listenerCount -= count ;
670+ this . retainedMessages . clear ( ) ;
671+ this . listeners = { __listeners : [ ] } as unknown as FastListeners ;
672+ } finally {
673+ this . _executeHooks ( "ClearListeners" , [ ] ) ;
674+ }
637675 }
638- if ( isFunction ( this . _options . onClearListeners ) ) this . _options . onClearListeners . call ( this ) ;
639676 }
640677 /**
641678 * 移除保留的事件
@@ -787,11 +824,7 @@ export class FastEvent<
787824 // @ts -ignore
788825 e . _emitter = `${ listener . name || "anonymous" } :${ message . type } ` ;
789826 }
790- if ( isFunction ( this . _options . onListenerError ) ) {
791- try {
792- this . _options . onListenerError . call ( this , e , listener , message , args ) ;
793- } catch { }
794- }
827+ this . _executeHooks ( "ListenerError" , [ e , listener , message , args ] ) ;
795828 if ( this . _options . ignoreErrors ) {
796829 return e ;
797830 } else {
@@ -862,7 +895,9 @@ export class FastEvent<
862895 }
863896 return result ;
864897 } catch ( e : any ) {
865- return this . _onListenerError ( listenerFn , message , args , e ) ;
898+ const errObj = this . _onListenerError ( listenerFn , message , args , e ) ;
899+ if ( errObj instanceof Error ) listener [ 5 ] = getWeakRef ( errObj ) ;
900+ return errObj ;
866901 }
867902 }
868903 private _getListenerExecutor ( args : FastEventListenerArgs ) : FastListenerExecutor | undefined {
@@ -1084,14 +1119,14 @@ export class FastEvent<
10841119 this . _traverseToPath ( this . listeners , parts , ( node ) => {
10851120 nodes . push ( node ) ;
10861121 } ) ;
1087- if ( isFunction ( this . _options . onBeforeExecuteListener ) ) {
1088- const r = this . _options . onBeforeExecuteListener . call ( this , message , args ) ;
1089- if ( Array . isArray ( r ) ) {
1090- return r ;
1091- } else if ( r === false ) {
1092- throw new AbortError ( message . type ) ;
1093- }
1122+
1123+ const r = this . _executeHooks ( "BeforeExecuteListener" , [ message , args ] ) ;
1124+ if ( Array . isArray ( r ) ) {
1125+ return r ;
1126+ } else if ( r === false ) {
1127+ throw new AbortError ( message . type ) ;
10941128 }
1129+
10951130 // 触发时进行消息转换
10961131 if ( isFunction ( this . _options . transform ) ) {
10971132 message . payload = this . _options . transform . call ( this , message as any ) ;
@@ -1105,13 +1140,7 @@ export class FastEvent<
11051140 if ( this . _options . expandEmitResults ) {
11061141 expandEmitResults ( results ) ;
11071142 }
1108- if ( isFunction ( this . _options . onAfterExecuteListener ) ) {
1109- Promise . allSettled ( results ) . then ( ( r : any [ ] ) => {
1110- const args = [ message , getPromiseResults ( r ) , nodes ] ;
1111- this . _options . onAfterExecuteListener ! . apply ( this , args as any ) ;
1112- this . _executeHooks ( "AfterExecuteListener" , args ) ;
1113- } ) ;
1114- }
1143+ this . _executeHooks ( "AfterExecuteListener" , [ message , results , nodes ] as any ) ;
11151144 return results ;
11161145 }
11171146
0 commit comments