From 98892ecad830b93bd5f5749f9b924e894c717f64 Mon Sep 17 00:00:00 2001 From: Erik Date: Mon, 10 Aug 2020 21:52:05 -0400 Subject: [PATCH] Added ability to pass arbitrary arguments to publisher --- actions/ActionBus.ts | 15 +++++++++------ package.json | 2 +- 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/actions/ActionBus.ts b/actions/ActionBus.ts index 107b5ee..7f19d23 100644 --- a/actions/ActionBus.ts +++ b/actions/ActionBus.ts @@ -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 { @@ -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. */ @@ -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) } } } diff --git a/package.json b/package.json index 8a368a9..f972e36 100644 --- a/package.json +++ b/package.json @@ -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",