33 Changed
44}
55
6- export interface ChangeData {
6+ export interface EventData {
77 eventName : string ;
88 sender : Observable ;
9+ }
10+
11+ export interface ChangeData extends EventData {
912 phase ?: ChangePhase ;
1013}
1114
@@ -19,29 +22,46 @@ export class Observable {
1922 public static propertyChangeEvent = "propertyChange" ;
2023 private _observers = { } ;
2124
25+ public on : ( eventNames : string , callback : ( data : EventData ) => void ) => void ;
26+ public off : ( eventNames : string , callback ?: any ) => void ;
27+ public addListener : ( eventNames : string , callback : ( data : EventData ) => void ) => void ;
28+ public removeListener : ( eventNames : string , callback ?: any ) => void ;
29+
2230 // true to track the Changing phase, false otherwise
2331 private _trackChanging = false ;
2432
2533 constructor ( body ?: any ) {
26- // TODO: Not implemented
34+ this . on = this . addListener = this . addObserver ;
35+ this . off = this . removeListener = this . removeObserver ;
2736 }
2837
29- public addObserver ( eventName : string , callback : ( data : ChangeData ) => void ) {
30- this . verifyCallback ( callback ) ;
31- var list = this . getEventList ( eventName , true ) ;
32- list . push ( callback ) ;
38+ public addObserver ( eventNames : string , callback : ( data : EventData ) => void ) {
39+ Observable . verifyCallback ( callback ) ;
40+ var events : Array < string > = eventNames . split ( "," ) ;
41+ var that = this ;
42+ events . forEach ( function ( event : string ) {
43+ var list = that . getEventList ( event . trim ( ) , true ) ;
44+ list . push ( callback ) ;
45+ } ) ;
3346 }
3447
35- public removeObserver ( eventName : string , callback : any ) {
36- var list = this . getEventList ( eventName , false ) ;
37- if ( ! list ) {
38- return ;
39- }
40-
41- var index = list . indexOf ( callback ) ;
42- if ( index >= 0 ) {
43- list . splice ( index , 1 ) ;
44- }
48+ public removeObserver ( eventNames : string , callback ?: any ) {
49+ var events : Array < string > = eventNames . split ( "," ) ;
50+ var that = this ;
51+ events . forEach ( function ( event : string ) {
52+ if ( callback ) {
53+ var list = that . getEventList ( event . trim ( ) , false ) ;
54+ if ( list ) {
55+ var index = list . indexOf ( callback ) ;
56+ if ( index >= 0 ) {
57+ list . splice ( index , 1 ) ;
58+ }
59+ }
60+ }
61+ else {
62+ that . _observers [ event . trim ( ) ] = undefined ;
63+ }
64+ } ) ;
4565 }
4666
4767 public setProperty ( name : string , value : any ) {
@@ -77,7 +97,7 @@ export class Observable {
7797 }
7898
7999 // The method will return true if the change is accepted, false otherwise
80- public notify ( data : ChangeData ) {
100+ public notify ( data : EventData ) {
81101 var observers = this . getEventList ( data . eventName ) ;
82102 if ( ! observers ) {
83103 return ;
@@ -105,23 +125,32 @@ export class Observable {
105125 } ;
106126 }
107127
108- private getEventList ( eventName : string , createIfNeeded ?: boolean ) : Array < ( data : ChangeData ) => void > {
128+ private getEventList ( eventName : string , createIfNeeded ?: boolean ) : Array < ( data : EventData ) => void > {
109129 if ( ! eventName ) {
110130 throw new TypeError ( "EventName must be valid string." ) ;
111131 }
112132
113- var list = < Array < ( data : ChangeData ) => void > > this . _observers [ eventName ] ;
133+ var list = < Array < ( data : EventData ) => void > > this . _observers [ eventName ] ;
114134 if ( ! list && createIfNeeded ) {
115- list = new Array < ( data : ChangeData ) => void > ( ) ;
135+ list = [ ] ;
116136 this . _observers [ eventName ] = list ;
117137 }
118138
119139 return list ;
120140 }
121141
122- private verifyCallback ( callback : any ) {
142+ private static verifyCallback ( callback : any ) {
123143 if ( ! callback || typeof callback !== "function" ) {
124144 throw new TypeError ( "Callback must be a valid function." ) ;
125145 }
126146 }
147+
148+ public emit ( eventNames : string ) {
149+ var events : Array < string > = eventNames . split ( "," ) ;
150+ var that = this ;
151+ events . forEach ( function ( event : string ) {
152+ that . notify ( { eventName : event . trim ( ) , sender : this } ) ;
153+ } ) ;
154+ }
155+
127156}
0 commit comments