Action emitter based on fbemitter. Instead of string event types we use classes (functions). The package is most useful when used with TypeScript.
$ npm install action-emitter --save-dev
First import the action-emitter
package and then create a new emitter instance.
import { ActionEmitter } from "action-emitter";
const Emitter = new ActionEmitter();
Create a new emitter instance.
const Emitter = new ActionEmitter();
Register a specific callback to be called on a particular action event. A subscription is returned that can be called to remove the listener.
Argument | Type | Description |
---|---|---|
actionClass |
Function |
Action class function. |
callback |
(action: TAction) => void |
Listener callback function. |
class MyAction {
constructor(private value: string) { }
public get Value() {
return this.value;
}
}
let subsciption = Emitter.addListener<MyAction>(MyAction, action => {
console.log(action.Value);
});
Similar to addListener()
but the callback is removed after it is invoked once. A subscription is returned that can be called to remove the listener.
Argument | Type | Description |
---|---|---|
actionClass |
Function |
Action class function. |
callback |
(action: TAction) => void |
Listener callback function. |
class MyAction {
constructor(private value: string) { }
public get Value() {
return this.value;
}
}
let subsciption = Emitter.once<MyAction>(MyAction, action => {
console.log(action.Value);
});
Removes all of the registered listeners. If provide actionClass
, only listeners for that action class are removed.
Argument | Type | Description |
---|---|---|
actionClass [*] |
Function |
Action class function. |
[*] - optional.
class MyAction {
constructor(private value: string) { }
public get Value() {
return this.value;
}
}
Emitter.removeAllListeners(MyAction);
// Or
Emitter.removeAllListeners();
Returns an array of listeners that are currently registered for the given action class.
Argument | Type | Description |
---|---|---|
actionClass |
Function |
Action class function. |
let listenersList = Emitter.listeners();
Return listeners count that are currently registered for the given action class. If action class is not specified, method will return all registered action listeners count.
Argument | Type | Description |
---|---|---|
actionClass [*] |
Function |
Action class function. |
[*] - optional.
class MyAction {
constructor(private value: string) { }
public get Value() {
return this.value;
}
}
let globalListenersCount = Emitter.listenersCount();
// or
let listenersCount = Emitter.listenersCount(MyAction);
Emits an action event with the given data. All callbacks that are listening to the particular action event will be notified.
Argument | Type | Description |
---|---|---|
action |
TAction |
Action class instance. |
class MyAction {
constructor(private value: string) { }
public get Value() {
return this.value;
}
}
Emitter.emit<MyAction>(new MyAction("value"));
//or
Emitter.emit(new MyAction("value"));
You can listen to all actions with AnyAction
class.
import { AnyAction } from "action-emitter";
Emitter.addListener(AnyAction, anyAction => {
let actionInstance = anyAction.Action;
console.log(actionInstance);
});
Released under the MIT license.