Skip to content

Commit

Permalink
Added ability to pass arbitrary arguments to publisher
Browse files Browse the repository at this point in the history
  • Loading branch information
robere2 committed Aug 11, 2020
1 parent 0919c1c commit 98892ec
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 7 deletions.
15 changes: 9 additions & 6 deletions actions/ActionBus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ class ActionBus {
* Subscribe to listen for new Actions of a certain type to come in, at which point the passed function
* will be called. The same Function can be subscribed multiple times.
* @param action {typeof Action|number} The Action type to listen for. Can be an Action class, or an Action ID.
* @param fn {Function} The function to be called when the Action is received. Should be non-null. Should take
* one argument, which is the Action.
* @param fn {Function} The function to be called when the Action is received. Should be non-null. Should take at
* least one argument, which is the Action. Further arbitrary "rest" arguments can be passed to {@link publish},
* which can be received by this function.
* @returns {boolean} True if the subscriber is added successfully, false otherwise.
*/
public subscribe(action: typeof Action|number, fn: Function) : boolean {
Expand All @@ -35,8 +36,9 @@ class ActionBus {
* Unsubscribe to stop listening for a type of Action on a specific Function. Will only unsubscribe the Function
* once, so if the passed Function was subscribed multiple times, you will need to unsubscribe multiple times.
* @param action {typeof Action|number} The Action type to listen for. Can be an Action class, or an Action ID.
* @param fn {Function} The function to be called when the Action is received. Should be non-null. Should take
* one argument, which is the Action.
* @param fn {Function} The function to be called when the Action is received. Should be non-null. Should take at
* least one argument, which is the Action. Further arbitrary "rest" arguments can be passed to {@link publish},
* which can be received by this function.
* @returns {boolean} True if the subscriber is added successfully, false otherwise. Returns true if the
* passed Function was never subscribed to the passed Action in the first place.
*/
Expand All @@ -61,13 +63,14 @@ class ActionBus {
/**
* Publish a new Action to this ActionBus, notifying all Functions subscribed to the type of Action published.
* @param action {Action} The action to publish. Should be non-null.
* @param args {Object[]} Arbitrary arguments to pass to the subscriber
*/
public publish(action: Action) : void {
public publish(action: Action, ... args: Object[]) : void {
if(action == null || this.subscribers[action.id] == null) {
return
}
for(let i = 0; i < this.subscribers[action.id].length; i++) {
this.subscribers[action.id][i](action)
this.subscribers[action.id][i](action, ...args)
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@quickplaymod/quickplay-actions-js",
"version": "1.0.0",
"version": "1.0.1",
"description": "Quickplay's central Action controller, written in TypeScript.",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down

0 comments on commit 98892ec

Please sign in to comment.