@@ -3,50 +3,53 @@ import { BehaviorSubject } from 'rxjs/BehaviorSubject';
33import { Observable } from 'rxjs/Observable' ;
44import { Subscription } from 'rxjs/Subscription' ;
55
6+ const ServiceName : string = "PubSub Service" ;
7+
68@Injectable ( )
7- export class PubSubService implements IPubSubService {
8- private events = { } ;
9+ export class PubSubService implements IPubSubService {
10+ private events = { } ;
911
1012 constructor ( ) { }
1113
1214 $sub ( event : string ) : Observable < any > ;
13- $sub ( event : string , callback : ( value : any ) => void ) : Subscription ;
15+ $sub ( event : string , callback : ( value : any ) => void ) : Subscription ;
1416 $sub ( event : string , callback : ( value : any ) => void , error : ( error : any ) => void ) : Subscription ;
1517 $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 ) {
17- if ( event == undefined ) {
18- console . warn ( '[PubSub Service] => Subscription method must get event name.' ) ;
19- return ;
18+ $sub ( event : string , callback ?: ( value : any ) => void , error ?: ( error : any ) => void , complete ?: ( ) => void ) {
19+ if ( event == undefined ) {
20+ throw new Error ( `[${ ServiceName } ] => Subscription method must get event name.` ) ;
2021 } ;
2122
22- if ( this . events [ event ] === undefined )
23+ if ( this . events [ event ] === undefined ) {
2324 this . events [ event ] = new BehaviorSubject < any > ( 0 ) ;
24-
25- if ( ! callback || typeof callback !== 'function' )
25+ }
26+
27+ if ( ! callback || typeof callback !== 'function' ) {
2628 return this . events [ event ] . asObservable ( ) ;
27- else
29+ }
30+ else {
2831 return this . events [ event ] . asObservable ( ) . subscribe ( callback , error , complete ) ;
32+ }
2933 }
34+
3035 $pub ( event : string , eventObject ?: any ) {
3136 if ( event == undefined ) {
32- console . warn ( '[PubSub Service] => Publish method must get event name.' ) ;
33- return ;
37+ throw new Error ( `[${ ServiceName } ] => Publish method must get event name.` ) ;
3438 }
3539 else if ( this . events [ event ] === undefined ) {
36- console . warn ( '[PubSub Service] => No recorded events found for "' + event + '".' ) ;
37- return ;
40+ throw new Error ( `[${ ServiceName } ] => No recorded events found for ${ event } .` ) ;
3841 }
39-
40- this . events [ event ] . next ( eventObject ) ;
41- }
42+
43+ this . events [ event ] . next ( eventObject ) ;
44+ }
4245}
4346
44- interface IPubSubService {
47+ interface IPubSubService {
4548 $pub ( event : string , eventObject ?: any ) ;
4649 $sub : I$sub ;
4750}
4851
49- interface I$sub {
52+ interface I$sub {
5053 ( event : string ) : Observable < any > ;
5154 ( event : string , callback : ( value : any ) => void ) : Subscription ;
5255 ( event : string , callback : ( value : any ) => void , error : ( error : any ) => void ) : Subscription ;
0 commit comments