1+ module Forms
2+ {
3+ /**
4+ * Function signature of an event listener callback
5+ */
6+ interface IEventListener < T >
7+ {
8+ ( parameter :T ) :any ;
9+ }
10+
11+
12+ /**
13+ * Encapsulates a subscription to an event dispatcher, and allows for unsubscribing
14+ */
15+ interface ISubscription < T >
16+ {
17+ listener : IEventListener < T >
18+ priority : number
19+ filter : any
20+
21+ /**
22+ * Remove this subscription from its dispatcher
23+ */
24+ unsubscribe ( ) :void
25+ }
26+
27+
28+ class Subscription < V > implements ISubscription < V >
29+ {
30+ constructor ( public listener :IEventListener < V > , public filter :any , public priority :number , public dispatcher :EventDispatcher < V > ) { }
31+
32+ unsubscribe ( ) :void { }
33+ }
34+
35+
36+ /**
37+ * The main interface of the event system.
38+ * An IEventDispatcher is an object that keeps a list of listeners, and sends dispatches events of a certain type to them.
39+ * This might otherwise be known as a Signal.
40+ */
41+ export interface IEventDispatcher < U >
42+ {
43+ add ( listener :IEventListener < U > , filter ?:any , priority ?:number ) :ISubscription < U > ;
44+ remove ( subscription :ISubscription < U > ) : void ;
45+ dispatch ( parameter :U ) : boolean ;
46+ clear ( ) :void ;
47+ hasListeners ( ) :boolean ;
48+ }
49+
50+
51+ /**
52+ * Implementation of IEventDispatcher
53+ * @see IEventDispatcher
54+ */
55+ export class EventDispatcher < T > implements IEventDispatcher < T >
56+ {
57+ private subscriptions :ISubscription < T > [ ] ;
58+
59+ add ( listener :IEventListener < T > , filter :any = null , priority :number = 0 ) :ISubscription < T > {
60+ return new Subscription < T > ( listener , filter , priority , this ) ;
61+ }
62+
63+ remove ( subscription :ISubscription < T > ) :void { }
64+
65+ dispatch ( event :T ) :boolean {
66+ return false ;
67+ }
68+
69+ clear ( ) :void { }
70+
71+ hasListeners ( ) :boolean {
72+ return false ;
73+ }
74+ }
75+ }
0 commit comments