module entitas . utils { "use strict" ; import Bag = entitas . utils . Bag ; export interface ISignal < T > { dispatch ( ... args ) : void ; add ( listener ) : void ; clear ( ) : void ; remove ( listener ) : void ; } export class Signal < T > implements ISignal < T > { public _listeners : Bag < T > ; private _context ; private _alloc : number ; public active : boolean ; constructor param context param alloc constructor ( context , alloc = 16 ) { this . _listeners = new Bag < T > ( ) ; this . _context = context ; this . _alloc = alloc ; this . active = false ; } param args dispatch ( ... args ) { const listeners = this . _listeners ; const size = listeners . size ( ) ; if ( size <= 0 ) return ; const context = this . _context ; for ( let i = 0 ; i < size ; i ++ ) { listeners [ i ] . apply ( context , args ) ; } } param listener add ( listener ) { this . _listeners . add ( listener ) ; this . active = true ; } param listener remove ( listener ) { const listeners = this . _listeners ; listeners . remove ( listener ) ; this . active = listeners . size ( ) > 0 ; } /**
         * Clear and reset to original alloc
         */ clear ( ) { this . _listeners . clear ( ) ; this . active = false ; } } }