/**
 * Event handler function with a generic sender and a generic argument.
 */ export interface IEventHandler < TSender , TArgs > { sender args ( sender , args ) ; } /**
 * Event handler function with a generic argument
 */ export interface ISimpleEventHandler < TArgs > { args ( args ) ; } /**
 * Event handler function without arguments
 */ export interface ISignalHandler { ( ) ; } /**
 * Indicates the object implements generic subscriptions.
 */ export interface ISubscribable < THandlerType > { param fn subscribe ( fn , scope ? ) : void ; param fn sub ( fn , scope ? ) : void ; param fn unsubscribe ( fn ) : void ; param fn unsub ( fn ) : void ; param fn one ( fn ) : void ; param fn has ( fn ) : boolean ; /**
     * Clears all the subscriptions.
     */ clear ( ) : void ; } /**
 * Models an event with a generic sender and generic argument.
 */ export interface IEvent < TSender , TArgs > extends ISubscribable < IEventHandler < TSender , TArgs > > { } /**
 * Models a simple event with a generic argument.
 */ export interface ISimpleEvent < TArgs > extends ISubscribable < ISimpleEventHandler < TArgs > > { } interface extends ISubscribable < ISignalHandler > export interface ISignal extends ISubscribable < ISignalHandler > { } /**
 * Base interface for event handling.
 */ export interface IBaseEventHandling < TEventHandler > { param name param fn subscribe ( name , fn ) : void ; param name param fn sub ( name , fn ) : void ; param name param fn unsubscribe ( name , fn ) : void ; param name param fn unsub ( name , fn ) : void ; param name param fn one ( name , fn ) : void ; param name param fn has ( name , fn ) : boolean ; } /**
 * Indicates the object is capable of handling named events.
 */ export interface IEventHandling < TSender , TArgs > extends IBaseEventHandling < IEventHandler < TSender , TArgs > > { } /**
 * Indicates the object is capable of handling named simple events.
 */ export interface ISimpleEventHandling < TArgs > extends IBaseEventHandling < ISimpleEventHandler < TArgs > > { } /**
 * Indicates the object is capable of handling named signals.
 */ export interface ISignalHandling extends IBaseEventHandling < ISignalHandler > { } class template TEventHandler export class Subscription < TEventHandler > { /**
     * Indicates if the subscription has been executed before.
     */ public isExecuted = false ; public scope : any ; param { TEventHandler } handler param { boolean } isOnce constructor ( public handler , public isOnce ) { } param { boolean } executeAsync param { * } The param { IArguments } args public execute ( executeAsync , scope , args ) { if ( ! this . isOnce || ! this . isExecuted ) { this . isExecuted = true ; var fn = this . handler ; if ( executeAsync ) { setTimeout ( ( ) => { fn . apply ( scope , args ) ; } , 1 ) ; } else { fn . apply ( scope , args ) ; } } } } /**
 * Base class for implementation of the dispatcher. It facilitates the subscribe
 * and unsubscribe methods based on generic handlers. The TEventType specifies
 * the type of event that should be exposed. Use the asEvent to expose the
 * dispatcher as event.
 */ export abstract class DispatcherBase < TEventHandler > implements ISubscribable < TEventHandler > { private _wrap = new DispatcherWrapper ( this ) ; private _subscriptions = new Array < Subscription < TEventHandler > > ( ) ; protected parent : any ; constructor ( incomingParent ) { this . parent = incomingParent ; } param fn public subscribe ( fn , scope ? ) { if ( fn ) { let sub = new Subscription < TEventHandler > ( fn , false ) ; sub . scope = scope ; this . _subscriptions . push ( sub ) ; } } param fn public sub ( fn , scope ? ) { this . subscribe ( fn , scope ) ; } param fn public one ( fn ) { if ( fn ) { this . _subscriptions . push ( new Subscription < TEventHandler > ( fn , true ) ) ; } } param fn public has ( fn ) { if ( fn ) { for ( let sub of this . _subscriptions ) { if ( sub . handler == fn ) { return true ; } } } return false ; } param fn public unsubscribe ( fn ) { if ( fn ) { for ( let i = 0 ; i < this . _subscriptions . length ; i ++ ) { let sub = this . _subscriptions [ i ] ; if ( sub . handler == fn ) { this . _subscriptions . splice ( i , 1 ) ; break ; } } } } param fn public unsub ( fn ) { this . unsubscribe ( fn ) ; } protected param { boolean } executeAsync param { * } The param { IArguments } args protected _dispatch ( executeAsync , scope , args ) { for ( let i = 0 ; i < this . _subscriptions . length ; i ++ ) { let sub = this . _subscriptions [ i ] ; if ( sub . isOnce ) { if ( sub . isExecuted === true ) { continue ; } this . _subscriptions . splice ( i , 1 ) ; i -- ; } sub . execute ( executeAsync , sub . scope ? sub . scope : scope , args ) ; } } /**
     * Creates an event from the dispatcher. Will return the dispatcher
     * in a wrapper. This will prevent exposure of any dispatcher methods.
     */ public asEvent ( ) { return this . _wrap ; } /**
     * Clears all the subscriptions.
     */ public clear ( ) { this . _subscriptions . splice ( 0 , this . _subscriptions . length ) ; } } /**
 * Dispatcher implementation for events. Can be used to subscribe, unsubscribe
 * or dispatch events. Use the ToEvent() method to expose the event.
 */ export class EventDispatcher < TSender , TArgs > extends DispatcherBase < IEventHandler < TSender , TArgs > > implements IEvent < TSender , TArgs > { /**
     * Creates a new EventDispatcher instance.
     */ constructor ( incomingParent ) { super ( incomingParent ) ; } param sender param args public dispatch ( sender , args ) { this . _dispatch ( false , this . parent , arguments ) ; } param sender param args public dispatchAsync ( sender , args ) { this . _dispatch ( true , this . parent , arguments ) ; } /**
     * Creates an event from the dispatcher. Will return the dispatcher
     * in a wrapper. This will prevent exposure of any dispatcher methods.
     */ public asEvent ( ) { return super . asEvent ( ) ; } } /**
 * The dispatcher handles the storage of subsciptions and facilitates
 * subscription, unsubscription and dispatching of a simple event
 */ export class SimpleEventDispatcher < TArgs > extends DispatcherBase < ISimpleEventHandler < TArgs > > implements ISimpleEvent < TArgs > { /**
     * Creates a new SimpleEventDispatcher instance.
     */ constructor ( incomingParent ) { super ( incomingParent ) ; } param args public dispatch ( args ) { this . _dispatch ( false , this . parent , arguments ) ; } param args public dispatchAsync ( args ) { this . _dispatch ( true , this . parent , arguments ) ; } /**
     * Creates an event from the dispatcher. Will return the dispatcher
     * in a wrapper. This will prevent exposure of any dispatcher methods.
     */ public asEvent ( ) { return super . asEvent ( ) ; } } /**
 * The dispatcher handles the storage of subsciptions and facilitates
 * subscription, unsubscription and dispatching of a signal event.
 */ export class SignalDispatcher extends DispatcherBase < ISignalHandler > implements ISignal { /**
     * Creates a new SignalDispatcher instance.
     */ constructor ( incomingParent ) { super ( incomingParent ) ; } /**
     * Dispatches the signal.
     */ public dispatch ( ) { this . _dispatch ( false , this . parent , arguments ) ; } /**
     * Dispatches the signal threaded.
     */ public dispatchAsync ( ) { this . _dispatch ( true , this . parent , arguments ) ; } /**
     * Creates an event from the dispatcher. Will return the dispatcher
     * in a wrapper. This will prevent exposure of any dispatcher methods.
     */ public asEvent ( ) { return super . asEvent ( ) ; } } /**
 * Hides the implementation of the event dispatcher. Will expose methods that
 * are relevent to the event.
 */ export class DispatcherWrapper < THandler > implements ISubscribable < THandler > { private _subscribe : ( fn : THandler , scope ? : any ) => void ; private _unsubscribe : ( fn : THandler ) => void ; private _one : ( fn : THandler ) => void ; private _has : ( fn : THandler ) => boolean ; private _clear : ( ) => void ; param dispatcher constructor ( dispatcher ) { this . _subscribe = ( fn , scope ? ) => dispatcher . subscribe ( fn , scope ) ; this . _unsubscribe = ( fn ) => dispatcher . unsubscribe ( fn ) ; this . _one = ( fn ) => dispatcher . one ( fn ) ; this . _has = ( fn ) => dispatcher . has ( fn ) ; this . _clear = ( ) => dispatcher . clear ( ) ; } param fn public subscribe ( fn , scope ? ) { this . _subscribe ( fn , scope ) ; } param fn public sub ( fn , scope ? ) { this . subscribe ( fn , scope ) ; } param fn public unsubscribe ( fn ) { this . _unsubscribe ( fn ) ; } param fn public unsub ( fn ) { this . unsubscribe ( fn ) ; } param fn public one ( fn ) { this . _one ( fn ) ; } param fn public has ( fn ) { return this . _has ( fn ) ; } /**
     * Clears all the subscriptions.
     */ public clear ( ) { this . _clear ( ) ; } } /**
 * Base class for event lists classes. Implements the get and remove.
 */ export abstract class EventListBase < TEventDispatcher > { private _events : { [ name ] : TEventDispatcher ; } = { } ; param name public get ( name ) { let event = this . _events [ name ] ; if ( event ) { return event ; } event = this . createDispatcher ( ) ; this . _events [ name ] = event ; return event ; } param name public remove ( name ) { this . _events [ name ] = null ; } /**
     * Creates a new dispatcher instance.
     */ protected abstract createDispatcher ( ) ; } /**
 * Storage class for multiple events that are accessible by name.
 * Events dispatchers are automatically created.
 */ export class EventList < TSender , TArgs > extends EventListBase < EventDispatcher < TSender , TArgs > > { /**
     * Creates a new EventList instance.
     */ constructor ( ) { super ( ) ; } /**
     * Creates a new dispatcher instance.
     */ protected createDispatcher ( ) { return new EventDispatcher < TSender , TArgs > ( this ) ; } } /**
 * Storage class for multiple simple events that are accessible by name.
 * Events dispatchers are automatically created.
 */ export class SimpleEventList < TArgs > extends EventListBase < SimpleEventDispatcher < TArgs > > { /**
     * Creates a new SimpleEventList instance.
     */ constructor ( ) { super ( ) ; } /**
     * Creates a new dispatcher instance.
     */ protected createDispatcher ( ) { return new SimpleEventDispatcher < TArgs > ( this ) ; } } /**
 * Storage class for multiple signal events that are accessible by name.
 * Events dispatchers are automatically created.
 */ export class SignalList extends EventListBase < SignalDispatcher > { /**
     * Creates a new SignalList instance.
     */ constructor ( ) { super ( ) ; } /**
     * Creates a new dispatcher instance.
     */ protected createDispatcher ( ) { return new SignalDispatcher ( this ) ; } } /**
 * Extends objects with event handling capabilities.
 */ export abstract class EventHandlingBase < TSender , TArgs > implements IEventHandling < TSender , TArgs > { private _events = new EventList < TSender , TArgs > ( ) ; /**
     * Gets the list with all the event dispatchers.
     */ protected get events ( ) : EventList < TSender , TArgs > { return this . _events ; } param name param fn public subscribe ( name , fn ) { this . _events . get ( name ) . subscribe ( fn ) ; } param name param fn public sub ( name , fn ) { this . subscribe ( name , fn ) ; } param name param fn public unsubscribe ( name , fn ) { this . _events . get ( name ) . unsubscribe ( fn ) ; } param name param fn public unsub ( name , fn ) { this . unsubscribe ( name , fn ) ; } param name param fn public one ( name , fn ) { this . _events . get ( name ) . one ( fn ) ; } param name param fn public has ( name , fn ) { return this . _events . get ( name ) . has ( fn ) ; } } /**
 * Extends objects with simple event handling capabilities.
 */ export abstract class SimpleEventHandlingBase < TArgs > implements ISimpleEventHandling < TArgs > { private _events = new SimpleEventList < TArgs > ( ) ; protected get events ( ) : SimpleEventList < TArgs > { return this . _events ; } param name param fn public subscribe ( name , fn ) { this . _events . get ( name ) . subscribe ( fn ) ; } param name param fn public sub ( name , fn ) { this . subscribe ( name , fn ) ; } param name param fn public one ( name , fn ) { this . _events . get ( name ) . one ( fn ) ; } param name param fn public has ( name , fn ) { return this . _events . get ( name ) . has ( fn ) ; } param name param fn public unsubscribe ( name , fn ) { this . _events . get ( name ) . unsubscribe ( fn ) ; } param name param fn public unsub ( name , fn ) { this . unsubscribe ( name , fn ) ; } } /**
 * Extends objects with signal event handling capabilities.
 */ export abstract class SignalHandlingBase implements ISignalHandling { private _events = new SignalList ( ) ; protected get events ( ) : SignalList { return this . _events ; } param name param fn public one ( name , fn ) { this . _events . get ( name ) . one ( fn ) ; } param name param fn public has ( name , fn ) { return this . _events . get ( name ) . has ( fn ) ; } param name param fn public subscribe ( name , fn ) { this . _events . get ( name ) . subscribe ( fn ) ; } param name param fn public sub ( name , fn ) { this . subscribe ( name , fn ) ; } param name param fn public unsubscribe ( name , fn ) { this . _events . get ( name ) . unsubscribe ( fn ) ; } param name param fn public unsub ( name , fn ) { this . unsubscribe ( name , fn ) ; } } export function createEventDispatcher < TSender , TArgs > ( ) { return new EventDispatcher < TSender , TArgs > ( this ) ; } export function createEventList < TSender , TArgs > ( ) { return new EventList < TSender , TArgs > ( ) ; } export function createSimpleEventDispatcher < TArgs > ( ) { return new SimpleEventDispatcher < TArgs > ( this ) ; } export function createSimpleEventList < TArgs > ( ) { return new SimpleEventList < TArgs > ( ) ; } export function createSignalDispatcher ( ) { return new SignalDispatcher ( this ) ; } export function createSignalList ( ) { return new SignalList ( ) ; } var StronglyTypedEventsStatic = { EventList : EventList , SimpleEventList : SimpleEventList , SignalList : SignalList , createEventList : createEventList , createSimpleEventList : createSimpleEventList , createSignalList : createSignalList , EventDispatcher : EventDispatcher , SimpleEventDispatcher : SimpleEventDispatcher , SignalDispatcher : SignalDispatcher , EventHandlingBase : EventHandlingBase , SimpleEventHandlingBase : SimpleEventHandlingBase , SignalHandlingBase : SignalHandlingBase , createEventDispatcher : createEventDispatcher , createSimpleEventDispatcher : createSimpleEventDispatcher , createSignalDispatcher : createSignalDispatcher , EventListBase : EventListBase , DispatcherBase : DispatcherBase , DispatcherWrapper : DispatcherWrapper } ; export var IStronglyTypedEvents = StronglyTypedEventsStatic ; export default StronglyTypedEventsStatic ;