11import  Action  from  "./Action" ; 
2+ import  Subscriber  from  "./Subscriber" ; 
23
34/** 
45 * The ActionBus is the bus to allow different elements of your program to be notified when a new Action is published, 
@@ -7,51 +8,47 @@ import Action from "./Action";
78class  ActionBus  { 
89
910    /** 
10-      * The subscribed functions . Map of Action IDs to all the subscribed Functions  for that Action ID. 
11+      * The subscribed Subscribers . Map of Action IDs to all the Subscribers  for that Action ID. 
1112     */ 
12-     private  subscribers  : Record < number ,  Function [ ] >  =  { } 
13+     private  subscribers  : Record < number ,  Subscriber [ ] >  =  { } 
1314
1415    /** 
15-      * Subscribe to listen for new Actions of a certain type to come in, at which point the passed function  
16-      * will be called . The same Function  can be subscribed multiple times. 
16+      * Subscribe to listen for new Actions of a certain type to come in, at which point the passed Subscriber  
17+      * will be notified . The same Subscriber  can be subscribed multiple times. 
1718     * @param  action {typeof Action|number} The Action type to listen for. Can be an Action class, or an Action ID. 
18-      * @param  fn {Function} The function to be called when the Action is received. Should be non-null. Should take at 
19-      * least one argument, which is the Action. Further arbitrary "rest" arguments can be passed to {@link  publish}, 
20-      * which can be received by this function. 
19+      * @param  sub {Subscriber} The Subscriber to be notified when the specified Action is published. 
2120     * @returns  {boolean } True if the subscriber is added successfully, false otherwise. 
2221     */ 
23-     public  subscribe ( action : typeof  Action | number ,  fn :  Function )  : boolean  { 
24-         if ( action  ==  null  ||  fn  ==  null )  { 
22+     public  subscribe ( action : typeof  Action | number ,  sub :  Subscriber )  : boolean  { 
23+         if ( action  ==  null  ||  sub  ==  null )  { 
2524            return  false 
2625        } 
2726        const  actionId  =  typeof  action  ==  "number"  ? action  : action . id 
2827        if ( this . subscribers [ actionId ]  ==  null )  { 
2928            this . subscribers [ actionId ]  =  [ ] 
3029        } 
31-         this . subscribers [ actionId ] . push ( fn ) 
30+         this . subscribers [ actionId ] . push ( sub ) 
3231        return  true 
3332    } 
3433
3534    /** 
36-      * Unsubscribe to stop listening for a type of Action on a specific Function . Will only unsubscribe the Function  
37-      * once, so if the passed Function  was subscribed multiple times, you will need to unsubscribe multiple times. 
35+      * Unsubscribe to stop listening for a type of Action on a specific Subscriber . Will only unsubscribe the Subscriber  
36+      * once, so if the passed Subscriber  was subscribed multiple times, you will need to unsubscribe multiple times. 
3837     * @param  action {typeof Action|number} The Action type to listen for. Can be an Action class, or an Action ID. 
39-      * @param  fn {Function} The function to be called when the Action is received. Should be non-null. Should take at 
40-      * least one argument, which is the Action. Further arbitrary "rest" arguments can be passed to {@link  publish}, 
41-      * which can be received by this function. 
38+      * @param  sub {Subscriber} The Subscriber to be notified when the specified Action is published. 
4239     * @returns  {boolean } True if the subscriber is added successfully, false otherwise. Returns true if the 
4340     * passed Function was never subscribed to the passed Action in the first place. 
4441     */ 
45-     public  unsubscribe ( action : typeof  Action | number ,  fn :  Function )  : boolean  { 
46-         if ( action  ==  null  ||  fn  ==  null )  { 
42+     public  unsubscribe ( action : typeof  Action | number ,  sub :  Subscriber )  : boolean  { 
43+         if ( action  ==  null  ||  sub  ==  null )  { 
4744            return  true 
4845        } 
4946
5047        const  actionId  =  typeof  action  ==  "number"  ? action  : action . id 
5148        if ( this . subscribers [ actionId ]  ==  null )  { 
5249            return  true 
5350        } 
54-         const  index  =  this . subscribers [ actionId ] . indexOf ( fn ) 
51+         const  index  =  this . subscribers [ actionId ] . indexOf ( sub ) 
5552        if ( index  <  0  ||  index  >=  this . subscribers [ actionId ] . length )  { 
5653            return  true 
5754        } 
@@ -61,16 +58,16 @@ class ActionBus {
6158    } 
6259
6360    /** 
64-      * Publish a new Action to this ActionBus, notifying all Functions  subscribed to the type of Action published. 
61+      * Publish a new Action to this ActionBus, notifying all Subscribers  subscribed to the type of Action published. 
6562     * @param  action {Action} The action to publish. Should be non-null. 
66-      * @param  args {Object[]} Arbitrary arguments to pass to the subscriber 
63+      * @param  args {Object[]} Arbitrary arguments to pass to the subscriber.  
6764     */ 
68-     public  publish ( action : Action ,  ... args : Object [ ] )  : void { 
65+     public  async   publish ( action : Action ,  ... args : Object [ ] )  : Promise < void >  { 
6966        if ( action  ==  null  ||  this . subscribers [ action . id ]  ==  null )  { 
7067            return 
7168        } 
7269        for ( let  i  =  0 ;  i  <  this . subscribers [ action . id ] . length ;  i ++ )  { 
73-             this . subscribers [ action . id ] [ i ] ( action ,  ...args ) 
70+             await   this . subscribers [ action . id ] [ i ] . run ( action ,  ...args ) 
7471        } 
7572    } 
7673} 
0 commit comments