Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

action-emitter

Package Overview
Dependencies
Maintainers
2
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

action-emitter

Action emitter based on fbemitter. Instead of string event types we use classes (functions).

  • 0.2.1
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
71
Maintainers
2
Weekly downloads
 
Created
Source

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.

Build Status NPM version dependencies Status devDependencies Status

Get started

$ npm install action-emitter --save-dev

Usage

First import the action-emitter package and then create a new emitter instance.

import { ActionEmitter } from "action-emitter";
const Emitter = new ActionEmitter();

API

constructor(): void

Create a new emitter instance.

Emitter construction example:
const Emitter = new ActionEmitter();

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
ArgumentTypeDescription
actionClassFunctionAction class function.
callback(action: TAction) => voidListener callback function.
Add listener example:
class MyAction {
    constructor(private value: string) { }
    public get Value() {
        return this.value;
    }
}

let subsciption = Emitter.addListener<MyAction>(MyAction, action => {
    console.log(action.Value);
});

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
ArgumentTypeDescription
actionClassFunctionAction class function.
callback(action: TAction) => voidListener callback function.
Add once listener example:
class MyAction {
    constructor(private value: string) { }
    public get Value() {
        return this.value;
    }
}

let subsciption = Emitter.once<MyAction>(MyAction, action => {
    console.log(action.Value);
});

removeAllListeners(actionClass): void

Removes all of the registered listeners. If provide actionClass, only listeners for that action class are removed.

Arguments
ArgumentTypeDescription
actionClass[*]FunctionAction class function.

[*] - optional.

Remove all listeners example:
class MyAction {
    constructor(private value: string) { }
    public get Value() {
        return this.value;
    }
}

Emitter.removeAllListeners(MyAction);
// Or
Emitter.removeAllListeners();

listeners(actionClass): Function[]

Returns an array of listeners that are currently registered for the given action class.

Arguments
ArgumentTypeDescription
actionClassFunctionAction class function.
Listeners list example:
let listenersList = Emitter.listeners();

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
ArgumentTypeDescription
actionClass[*]FunctionAction class function.

[*] - optional.

Listeners list example:
class MyAction {
    constructor(private value: string) { }
    public get Value() {
        return this.value;
    }
}

let globalListenersCount = Emitter.listenersCount();
// or 
let listenersCount = Emitter.listenersCount(MyAction);

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
ArgumentTypeDescription
actionTActionAction class instance.
Action emit example:
class MyAction {
    constructor(private value: string) { }
    public get Value() {
        return this.value;
    }
}

Emitter.emit<MyAction>(new MyAction("value"));
//or 
Emitter.emit(new MyAction("value"));

Debuging

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);
});

License

Released under the MIT license.

Keywords

FAQs

Package last updated on 10 Apr 2017

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc