1- import { log , warn , isFunction , isString , getClassName } from '../utils' ;
2- import { fxData , _require } from '../core' ;
1+ import {
2+ log ,
3+ warn ,
4+ isFunction ,
5+ isString ,
6+ getClassName
7+ } from '../utils' ;
8+ import {
9+ fxData ,
10+ _require
11+ } from '../core' ;
312import i18n from '../i18n' ;
413import Dispatcher from './dispatcher' ;
5- import { getDecoratorToken as getCommandToken } from '../command/decorator' ;
6- import { getDecoratorToken as getEventToken } from '../event/decorator' ;
14+ import {
15+ getDecoratorToken as getCommandToken
16+ } from '../command/decorator' ;
17+ import {
18+ getDecoratorToken as getEventToken
19+ } from '../event/decorator' ;
720import assert from 'assert' ;
821
922export default class MessageDispatcher extends Dispatcher {
@@ -31,12 +44,12 @@ export default class MessageDispatcher extends Dispatcher {
3144
3245 registerHandler ( handlerType ) {
3346 assert ( handlerType ) ;
34- let ctoken = this . type === 'event'
35- ? getEventToken ( handlerType )
36- : getCommandToken ( handlerType ) ;
47+ let ctoken = this . type === 'event' ?
48+ getEventToken ( handlerType ) :
49+ getCommandToken ( handlerType ) ;
3750 if ( ! ctoken . name && ! ctoken . module ) {
3851 if ( ! handlerType . prototype ) {
39- log ( i18n . t ( '不支持的handler类型' ) ) ;
52+ warn ( i18n . t ( '不支持的handler类型' ) ) ;
4053 }
4154 ctoken = {
4255 module : handlerType . prototype . __module ,
@@ -52,18 +65,23 @@ export default class MessageDispatcher extends Dispatcher {
5265 }
5366 let {
5467 module = ctoken . module ,
55- name = p
56- } = this . type === 'event'
57- ? getEventToken ( handlerType . prototype [ p ] )
58- : getCommandToken ( handlerType . prototype [ p ] ) ;
68+ name = p
69+ } = this . type === 'event' ?
70+ getEventToken ( handlerType . prototype [ p ] ) :
71+ getCommandToken ( handlerType . prototype [ p ] ) ;
5972 if ( module && name ) {
6073 let items = this . _handlers [ `${ module } /${ name } ` ] ;
6174 if ( ! items ) {
6275 this . _handlers [ `${ module } /${ name } ` ] = items = [ ] ;
6376 }
64- if ( ! items . find ( item => item . CLS . name == handlerType . name && item . method == p ) ) {
65- items . push ( { CLS : handlerType , method : p } ) ;
77+ if ( ! items . find ( item => item . CLS == handlerType && item . method == p ) ) {
78+ items . push ( {
79+ CLS : handlerType ,
80+ method : p
81+ } ) ;
6682 log ( i18n . t ( '注册' ) , getClassName ( handlerType ) + '.' + p ) ;
83+ } else {
84+ log ( i18n . t ( '跳过' ) , getClassName ( handlerType ) + '.' + p )
6785 }
6886 } else {
6987 warn ( i18n . t ( '注册失败' ) , getClassName ( handlerType ) + '.' + p )
@@ -72,16 +90,16 @@ export default class MessageDispatcher extends Dispatcher {
7290 }
7391
7492 unregisterHandler ( handler ) {
75- const ctoken = this . type === 'event'
76- ? getEventToken ( handlerType )
77- : getCommandToken ( handlerType ) ;
93+ const ctoken = this . type === 'event' ?
94+ getEventToken ( handlerType ) :
95+ getCommandToken ( handlerType ) ;
7896 for ( const p in handlerType . prototype ) {
7997 const {
8098 module = ctoken . module ,
81- name
82- } = this . type === 'event'
83- ? getEventToken ( handlerType . prototype [ p ] )
84- : getCommandToken ( handlerType . prototype [ p ] ) ;
99+ name
100+ } = this . type === 'event' ?
101+ getEventToken ( handlerType . prototype [ p ] ) :
102+ getCommandToken ( handlerType . prototype [ p ] ) ;
85103 if ( module && name ) {
86104 let items = this . _handlers [ `${ module } /${ name } ` ] ;
87105 if ( ! items ) {
@@ -100,59 +118,61 @@ export default class MessageDispatcher extends Dispatcher {
100118 async dispatch ( message ) {
101119 if ( ! isString ( message . id ) ) {
102120 this . _onDispatchFaild ( message , 'nomessage' ) ;
103- log ( i18n . t ( '消息id无效' ) ) ;
121+ warn ( i18n . t ( '消息id无效' ) ) ;
104122 return ;
105123 }
106124 if ( ! isString ( message . name ) ) {
107125 this . _onDispatchFaild ( message , 'nomessage' ) ;
108- log ( i18n . t ( '消息name无效' ) ) ;
126+ warn ( i18n . t ( '消息name无效' ) ) ;
109127 return ;
110128 }
111129 if ( message . type !== 'event' && message . type !== 'command' ) {
112130 this . _onDispatchFaild ( message , 'nomessage' ) ;
113- log ( i18n . t ( '消息type无效' ) ) ;
131+ warn ( i18n . t ( '消息type无效' ) ) ;
114132 return ;
115133 }
116134 if ( message . type !== this . type ) {
117135 this . _onDispatchFaild ( message , 'notsupport' ) ;
118- log ( message . type + i18n . t ( '消息无法分发' ) ) ;
136+ warn ( message . type + i18n . t ( '消息无法分发' ) ) ;
119137 return ;
120138 }
121139 const id = message . id ;
122140 const module = message . module ;
123141 const name = message . name ;
124142 if ( ! module ) {
125143 this . _onDispatchFaild ( message , 'nomodule' ) ;
126- log ( i18n . t ( '消息module无效' ) ) ;
144+ warn ( i18n . t ( '消息module无效' ) ) ;
127145 return ;
128146 }
129147 const handlers = this . getHandlers ( name , module ) ;
130148 if ( ! handlers || handlers . length <= 0 ) {
131149 this . _onDispatchFaild ( message , 'nohandler' ) ;
132- log ( i18n . t ( '无消息处理器' ) ) ;
150+ warn ( i18n . t ( '无消息处理器' ) ) ;
133151 }
134- log ( i18n . t ( '开始执行' ) + this . type + ' ' + `${ module } /${ name } (${ id } )` ) ;
152+ log ( i18n . t ( '开始执行' ) , this . type , `${ module } /${ name } (${ id } )` ) ;
135153 await this . _onDispatching ( message ) ;
136154 let curHandler ;
137155 try {
138- for ( const { CLS , method}
139- of handlers ) {
156+ for ( const {
157+ CLS ,
158+ method
159+ } of handlers ) {
140160 curHandler = null ;
141161 var handler = new CLS ( ) ;
142162 curHandler = handler ;
143163 if ( ! isFunction ( handler [ method ] ) ) {
144- log ( i18n . t ( '处理器无法执行命令' ) ) ;
164+ warn ( i18n . t ( '处理器无法执行命令' ) ) ;
145165 continue ;
146166 }
147167 await handler [ method ] . bind ( handler ) ( message . data || { } ) ;
148168 }
149169 await this . _onDispatched ( message ) ;
150- log ( i18n . t ( '完成执行' ) + this . type + ' ' + `${ module } /${ name } (${ id } )` ) ;
170+ log ( i18n . t ( '完成执行' ) , this . type , `${ module } /${ name } (${ id } )` ) ;
151171 return true ;
152172 } catch ( err ) {
153- warn ( i18n . t ( '失败执行' ) + this . type + ' ' + `${ module } /${ name } (${ id } )` ) ;
154- console . warn ( err , err . stack ) ;
173+ warn ( i18n . t ( '失败执行' ) , this . type , `${ module } /${ name } (${ id } )` , err ) ;
155174 await this . _onDispatchFaild ( message , 'error' , err , curHandler ) ;
175+ return false ;
156176 }
157177 }
158178
@@ -169,7 +189,7 @@ export default class MessageDispatcher extends Dispatcher {
169189 this . _dispatchedListeners . push ( dispatchedListener ) ;
170190 if ( isFunction ( dispatchFailedListener ) )
171191 this . _dispatchFailedListeners . push ( dispatchFailedListener ) ;
172- }
192+ }
173193
174194 removeListener ( dispatchingListener , dispatchedListener , dispatchFailedListener ) {
175195 if ( isFunction ( dispatchingListener ) )
@@ -178,14 +198,14 @@ export default class MessageDispatcher extends Dispatcher {
178198 this . _dispatchedListeners . splice ( this . _dispatchedListeners . indexOf ( dispatchedListener ) , 1 ) ;
179199 if ( isFunction ( dispatchFailedListener ) )
180200 this . _dispatchFailedListeners . splice ( this . _dispatchFailedListeners . indexOf ( dispatchFailedListener ) , 1 ) ;
181- }
201+ }
182202
183203 async _onDispatching ( message ) {
184204 for ( const listener of this . _dispatchingListeners ) {
185205 try {
186206 await listener ( message ) ;
187207 } catch ( e ) {
188- log ( e ) ;
208+ warn ( e ) ;
189209 }
190210 }
191211 }
@@ -195,7 +215,7 @@ export default class MessageDispatcher extends Dispatcher {
195215 try {
196216 await listener ( message , code , error ) ;
197217 } catch ( e ) {
198- log ( e ) ;
218+ warn ( e ) ;
199219 }
200220 }
201221 }
@@ -205,7 +225,7 @@ export default class MessageDispatcher extends Dispatcher {
205225 try {
206226 await listener ( message , 'ok' ) ;
207227 } catch ( e ) {
208- log ( e ) ;
228+ warn ( e ) ;
209229 }
210230 }
211231 }
0 commit comments