import { IEventDispatcher } from "../../events/api/IEventDispatcher" ; import { ILifecycle } from "../api/ILifecycle" ; import { LifecycleError } from "../api/LifecycleError" ; import { LifecycleEvent } from "../api/LifecycleEvent" ; import { LifecycleState } from "../api/LifecycleState" ; import { LifecycleTransition } from "./LifecycleTransition" ; private export class Lifecycle implements ILifecycle { private _state : string = LifecycleState . UNINITIALIZED ; inheritDoc public get state ( ) : string { return this . _state ; } private _target : IEventDispatcher ; inheritDoc public get target ( ) : IEventDispatcher { return this . _target ; } inheritDoc public get uninitialized ( ) : boolean { return this . _state === LifecycleState . UNINITIALIZED ; } inheritDoc public get initialized ( ) : boolean { return this . _state !== LifecycleState . UNINITIALIZED && this . _state !== LifecycleState . INITIALIZING ; } inheritDoc public get active ( ) : boolean { return this . _state === LifecycleState . ACTIVE ; } inheritDoc public get suspended ( ) : boolean { return this . _state === LifecycleState . SUSPENDED ; } inheritDoc public get destroyed ( ) : boolean { return this . _state === LifecycleState . DESTROYED ; } private _reversedEventTypes : Map < any , boolean > = new Map < any , boolean > ( ) ; private _reversePriority : number = 0 ; private _initialize : LifecycleTransition ; private _suspend : LifecycleTransition ; private _resume : LifecycleTransition ; private _destroy : LifecycleTransition ; private _dispatcher : IEventDispatcher ; param target constructor ( target ) { this . _target = target ; this . _dispatcher = target ; this . configureTransitions ( ) ; } inheritDoc public initialize ( callback ? ) { this . _initialize . enter ( callback ) ; } inheritDoc public suspend ( callback ? ) { this . _suspend . enter ( callback ) ; } inheritDoc public resume ( callback ? ) { this . _resume . enter ( callback ) ; } inheritDoc public destroy ( callback ? ) { this . _destroy . enter ( callback ) ; } inheritDoc public beforeInitializing ( handler ) { if ( ! this . uninitialized ) { this . reportError ( LifecycleError . LATE_HANDLER_ERROR_MESSAGE ) ; } this . _initialize . addBeforeHandler ( handler ) ; return this ; } inheritDoc public whenInitializing ( handler ) { if ( this . initialized ) { this . reportError ( LifecycleError . LATE_HANDLER_ERROR_MESSAGE ) ; } this . addEventListener ( LifecycleEvent . INITIALIZE , this . createSyncLifecycleListener ( handler , true ) ) ; return this ; } inheritDoc public afterInitializing ( handler ) { if ( this . initialized ) { this . reportError ( LifecycleError . LATE_HANDLER_ERROR_MESSAGE ) ; } this . addEventListener ( LifecycleEvent . POST_INITIALIZE , this . createSyncLifecycleListener ( handler , true ) ) ; return this ; } inheritDoc public beforeSuspending ( handler ) { this . _suspend . addBeforeHandler ( handler ) ; return this ; } inheritDoc public whenSuspending ( handler ) { this . addEventListener ( LifecycleEvent . SUSPEND , this . createSyncLifecycleListener ( handler ) ) ; return this ; } inheritDoc public afterSuspending ( handler ) { this . addEventListener ( LifecycleEvent . POST_SUSPEND , this . createSyncLifecycleListener ( handler ) ) ; return this ; } inheritDoc public beforeResuming ( handler ) { this . _resume . addBeforeHandler ( handler ) ; return this ; } inheritDoc public whenResuming ( handler ) { this . addEventListener ( LifecycleEvent . RESUME , this . createSyncLifecycleListener ( handler ) ) ; return this ; } inheritDoc public afterResuming ( handler ) { this . addEventListener ( LifecycleEvent . POST_RESUME , this . createSyncLifecycleListener ( handler ) ) ; return this ; } inheritDoc public beforeDestroying ( handler ) { this . _destroy . addBeforeHandler ( handler ) ; return this ; } inheritDoc public whenDestroying ( handler ) { this . addEventListener ( LifecycleEvent . DESTROY , this . createSyncLifecycleListener ( handler , true ) ) ; return this ; } inheritDoc public afterDestroying ( handler ) { this . addEventListener ( LifecycleEvent . POST_DESTROY , this . createSyncLifecycleListener ( handler , true ) ) ; return this ; } inheritDoc public addEventListener ( type , listener , useCapture = false , priority = 0 , useWeakReference = false ) { priority = this . flipPriority ( type , priority ) ; this . _dispatcher . addEventListener ( type , listener , undefined , useCapture , priority ) ; } inheritDoc public removeEventListener ( type , listener , useCapture = false ) { this . _dispatcher . removeEventListener ( type , listener ) ; } inheritDoc public dispatchEvent ( event ) { return this . _dispatcher . dispatchEvent ( event ) ; } inheritDoc public hasEventListener ( type ) { return this . _dispatcher . hasEventListener ( type ) ; } inheritDoc public willTrigger ( type ) { return this . _dispatcher . willTrigger ( type ) ; } public setCurrentState ( state ) { if ( this . _state !== state ) { this . _state = state ; this . dispatchEvent ( new LifecycleEvent ( LifecycleEvent . STATE_CHANGE ) ) ; } } public addReversedEventTypes ( ... types ) { types . forEach ( ( type ) => { this . _reversedEventTypes . set ( type , true ) ; } ) ; } private configureTransitions ( ) { this . _initialize = new LifecycleTransition ( LifecycleEvent . PRE_INITIALIZE , this ) . fromStates ( LifecycleState . UNINITIALIZED ) . toStates ( LifecycleState . INITIALIZING , LifecycleState . ACTIVE ) . withEvents ( LifecycleEvent . PRE_INITIALIZE , LifecycleEvent . INITIALIZE , LifecycleEvent . POST_INITIALIZE ) ; this . _suspend = new LifecycleTransition ( LifecycleEvent . PRE_SUSPEND , this ) . fromStates ( LifecycleState . ACTIVE ) . toStates ( LifecycleState . SUSPENDING , LifecycleState . SUSPENDED ) . withEvents ( LifecycleEvent . PRE_SUSPEND , LifecycleEvent . SUSPEND , LifecycleEvent . POST_SUSPEND ) . inReverse ( ) ; this . _resume = new LifecycleTransition ( LifecycleEvent . PRE_RESUME , this ) . fromStates ( LifecycleState . SUSPENDED ) . toStates ( LifecycleState . RESUMING , LifecycleState . ACTIVE ) . withEvents ( LifecycleEvent . PRE_RESUME , LifecycleEvent . RESUME , LifecycleEvent . POST_RESUME ) ; this . _destroy = new LifecycleTransition ( LifecycleEvent . PRE_DESTROY , this ) . fromStates ( LifecycleState . SUSPENDED , LifecycleState . ACTIVE ) . toStates ( LifecycleState . DESTROYING , LifecycleState . DESTROYED ) . withEvents ( LifecycleEvent . PRE_DESTROY , LifecycleEvent . DESTROY , LifecycleEvent . POST_DESTROY ) . inReverse ( ) ; } private flipPriority ( type , priority ) { return priority === 0 && this . _reversedEventTypes . get ( type ) ? this . _reversePriority ++ : priority ; } private createSyncLifecycleListener ( handler , once = false ) { if ( handler . length > 1 ) { throw new LifecycleError ( LifecycleError . SYNC_HANDLER_ARG_MISMATCH ) ; } if ( handler . length === 1 ) { return function ( event ) : void { if ( once ) { ( < IEventDispatcher > event . target ) . removeEventListener ( event . type , handler ) ; } handler ( event . type ) ; } ; } return function ( event ) : void { if ( once ) { ( < IEventDispatcher > event . target ) . removeEventListener ( event . type , handler ) ; } handler ( ) ; } ; } private reportError ( message ) { let error = new LifecycleError ( message ) ; if ( this . hasEventListener ( LifecycleEvent . ERROR ) ) { let event = new LifecycleEvent ( LifecycleEvent . ERROR , error ) ; this . dispatchEvent ( event ) ; } else { throw error ; } } }