action-emitter
Action emitter based on fbemitter. Instead of string event types we use classes (functions). The package is most useful when used with TypeScript.
Get started
$ npm install action-emitter --save-dev
Usage
First import the action-emitter
package and then create a new emitter instance.
;;
API
constructor(): void
Create a new emitter instance.
Emitter construction example:
;
addListener(actionClass, callback): EventSubscription
Register a specific callback to be called on a particular action event. A subscription is returned that can be called to remove the listener.
Arguments
Argument | Type | Description |
---|---|---|
actionClass |
Function |
Action class function. |
callback |
(action: TAction) => void |
Listener callback function. |
Add listener example:
;
once(actionClass, callback): EventSubscription
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.
Arguments
Argument | Type | Description |
---|---|---|
actionClass |
Function |
Action class function. |
callback |
(action: TAction) => void |
Listener callback function. |
Add once listener example:
;
removeAllListeners(actionClass): void
Removes all of the registered listeners. If provide actionClass
, only listeners for that action class are removed.
Arguments
Argument | Type | Description |
---|---|---|
actionClass [*] |
Function |
Action class function. |
[*] - optional.
Remove all listeners example:
Emitter.removeAllListenersMyAction;// OrEmitter.removeAllListeners;
listeners(actionClass): Function[]
Returns an array of listeners that are currently registered for the given action class.
Arguments
Argument | Type | Description |
---|---|---|
actionClass |
Function |
Action class function. |
Listeners list example:
;
listenersCount(actionClass): number
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.
Arguments
Argument | Type | Description |
---|---|---|
actionClass [*] |
Function |
Action class function. |
[*] - optional.
Listeners list example:
;// or ;
emit(action): void
Emits an action event with the given data. All callbacks that are listening to the particular action event will be notified.
Arguments
Argument | Type | Description |
---|---|---|
action |
TAction |
Action class instance. |
Action emit example:
Emitter.emitnew MyAction"value";//or Emitter.emitnew MyAction"value";
Debuging
You can listen to all actions with AnyAction
class.
;Emitter.addListenerAnyAction, ;
License
Released under the MIT license.