11import { Injectable } from '@angular/core' ;
22import { BehaviorSubject } from 'rxjs/BehaviorSubject' ;
33import { Observable } from 'rxjs/Observable' ;
4+ import { Subscription } from 'rxjs/Subscription' ;
45
56@Injectable ( )
6- export class PubSubService {
7+ export class PubSubService implements IPubSubService {
78 private events = { } ;
89
910 constructor ( ) { }
1011
11- $sub ( event : string , callback : ( value : any ) => void , error ?: ( error : any ) => void , complete ?: ( ) => void ) {
12+ $sub ( event : string ) : Observable < any > ;
13+ $sub ( event : string , callback : ( value : any ) => void ) : Subscription ;
14+ $sub ( event : string , callback : ( value : any ) => void , error : ( error : any ) => void ) : Subscription ;
15+ $sub ( event : string , callback : ( value : any ) => void , error : ( error : any ) => void , complete : ( ) => void ) : Subscription ;
16+ $sub ( event : string , callback ?: ( value : any ) => void , error ?: ( error : any ) => void , complete ?: ( ) => void ) {
1217 if ( event == undefined ) {
1318 console . warn ( '[PubSub Service] => Subscription method must get event name.' ) ;
1419 return ;
1520 } ;
1621
1722 if ( this . events [ event ] === undefined )
1823 this . events [ event ] = new BehaviorSubject < any > ( 0 ) ;
24+
1925 if ( ! callback || typeof callback !== 'function' )
20- return < Observable < any > > this . events [ event ] . asObservable ( ) ;
26+ return this . events [ event ] . asObservable ( ) ;
2127 else
22- return ( < Observable < any > > this . events [ event ] . asObservable ( ) ) . subscribe ( callback , error , complete ) ;
28+ return this . events [ event ] . asObservable ( ) . subscribe ( callback , error , complete ) ;
2329 }
2430 $pub ( event : string , eventObject ?: any ) {
2531 if ( event == undefined ) {
@@ -31,6 +37,19 @@ export class PubSubService {
3137 return ;
3238 }
3339
34- this . events [ event ] . next ( eventObject ) ;
40+ this . events [ event ] . next ( eventObject ) ;
3541 }
36- }
42+ }
43+
44+ interface IPubSubService {
45+ $pub ( event : string , eventObject ?: any ) ;
46+ $sub : I$sub ;
47+ }
48+
49+ interface I$sub {
50+ ( event : string ) : Observable < any > ;
51+ ( event : string , callback : ( value : any ) => void ) : Subscription ;
52+ ( event : string , callback : ( value : any ) => void , error : ( error : any ) => void ) : Subscription ;
53+ ( event : string , callback : ( value : any ) => void , error : ( error : any ) => void , complete : ( ) => void ) : Subscription ;
54+ }
55+
0 commit comments