import { Service } from "./Service" ; import { Manager } from "../Framework" ; description const { ccclass , property } = cc . _decorator ; description const addListeners = Symbol ( "addListeners" ) ; const removeEventListeners = Symbol ( "removeEventListeners" ) ; interface EventArgs { name ? : string ; func ? : ( data : any ) => any ; mainCmd ? : number ; subCmd ? : number ; handleType ? : any ; isQueue ? : boolean ; } @ ccclass export default class EventComponent extends cc . Component { protected _service : Service = null ; protected logTag = `[EventComponent]` ; private _events : EventArgs [ ] = [ ] ; private _getEventArgs ( ) { if ( arguments . length < 2 ) { if ( CC_DEBUG ) cc . error ( `注册事件参数错误` ) ; return null ; } let args = { } ; if ( typeof arguments [ 0 ] == "string" ) { args . name = arguments [ 0 ] ; args . func = arguments [ 1 ] ; } else { args . mainCmd = arguments [ 0 ] ; args . subCmd = arguments [ 1 ] ; args . handleType = null ; args . isQueue = true ; if ( arguments . length >= 3 ) { args . func = arguments [ 2 ] ; } if ( arguments . length >= 4 ) { args . handleType = arguments [ 3 ] ; } if ( arguments . length >= 5 ) { args . isQueue = arguments [ 4 ] ; } } return args ; } description param manCmd param subCmd param func param handleType param isQueue registerEvent ( manCmd , subCmd , func , handleType ? , isQueue ? ) ; param eventName param func registerEvent ( eventName , func ) ; registerEvent ( ) { let args = this . _getEventArgs . apply ( this , arguments ) ; if ( args ) { this . _events . push ( args ) ; } } description param manCmd param subCmd param func param handleType param isQueue addEvent ( manCmd , subCmd , func , handleType ? , isQueue ? ) ; param eventName param func addEvent ( eventName , func ) ; addEvent ( ) { let event = this . _getEventArgs . apply ( this , arguments ) ; if ( event ) { this . _events . push ( event ) ; if ( event . name ) { Manager . eventDispatcher . addEventListener ( event . name , event . func , this ) ; } else { if ( this . _service ) { if ( event . mainCmd && event . subCmd ) { this . _service . addListener ( event . mainCmd , event . subCmd , event . handleType , event . func , event . isQueue , this ) ; } else { cc . error ( this . logTag , `注册的网络回调有误 class : ${ cc . js . getClassName ( this ) } manCmd : ${ event . mainCmd } subCmd : ${ event . subCmd }` ) ; } } } } } description param manCmd param subCmd removeEvent ( manCmd , subCmd ) ; description param eventName removeEvent ( eventName ) ; removeEvent ( ) { if ( arguments . length < 1 ) { if ( CC_DEBUG ) cc . error ( `参数有误` ) ; return ; } if ( arguments . length == 1 ) { Manager . eventDispatcher . removeEventListener ( arguments [ 0 ] , this ) ; let i = this . _events . length ; while ( i -- ) { if ( this . _events [ i ] . name == arguments [ 0 ] ) { this . _events . splice ( i , 1 ) ; } } } else { let mainCmd = arguments [ 0 ] ; let subCmd = arguments [ 1 ] ; if ( this . _service && typeof mainCmd == "number" && typeof subCmd == "number" ) { this . _service . removeListeners ( this , mainCmd , subCmd ) ; } let i = this . _events . length ; while ( i -- ) { if ( this . _events [ i ] . mainCmd == mainCmd && this . _events [ i ] . subCmd == subCmd ) { this . _events . splice ( i , 1 ) ; } } } } protected bindingEvents ( ) { } onLoad ( ) { this . bindingEvents ( ) ; this [ addListeners ] ( ) ; } onDestroy ( ) { this [ removeEventListeners ] ( ) ; } [ addListeners ] ( ) { for ( let i = 0 ; i < this . _events . length ; i ++ ) { let event = this . _events [ i ] ; if ( event . name ) { Manager . eventDispatcher . addEventListener ( event . name , event . func , this ) ; } else { if ( this . _service ) { if ( event . mainCmd && event . subCmd ) { this . _service . addListener ( event . mainCmd , event . subCmd , event . handleType , event . func , event . isQueue , this ) ; } else { cc . error ( this . logTag , `注册的网络回调有误 class : ${ cc . js . getClassName ( this ) } manCmd : ${ event . mainCmd } subCmd : ${ event . subCmd }` ) ; } } } } } [ removeEventListeners ] ( ) { for ( let i = 0 ; i < this . _events . length ; i ++ ) { let event = this . _events [ i ] ; if ( event . name ) { Manager . eventDispatcher . removeEventListener ( event . name , this ) ; } } if ( this . _service ) { this . _service . removeListeners ( this ) ; this . _service = null ; } } }