Introducing Socket Firewall: Free, Proactive Protection for Your Software Supply Chain.Learn More
Socket
Book a DemoInstallSign in
Socket

@amphibian/emitter

Package Overview
Dependencies
Maintainers
0
Versions
18
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@amphibian/emitter

small utility emitter library

latest
Source
npmnpm
Version
2.0.5
Version published
Maintainers
0
Created
Source

emitter

small utility emitter library

npm install @amphibian/emitter
const createEmitter = require('@amphibian/emitter');
const myCustomEmitter = createEmitter();

myCustomEmitter.on((data) => {
    console.log(data);
});

myCustomEmitter.emit('Hello');
// > 'Hello'

Unsubscribe

The following example will only trigger console.log once as the event listener unsubscribes itself the first time it is fired.

const myCustomEmitter = createEmitter();
const unsubscribe = myCustomEmitter.on((data) => {
    unsubscribe();
    console.log(data);
});

myCustomEmitter.emit('Hello');
myCustomEmitter.emit('Hello');
// > 'Hello'

You can also save the function and call off.

const myCustomEmitter = createEmitter();
const eventHandler = (data) => {
    console.log(data);
};

myCustomEmitter.on(eventHandler);
myCustomEmitter.off(eventHandler);

Stop propagation

To prevent subsequent listeners from firing, stop propagation as follows:

const myCustomEmitter = createEmitter();

myCustomEmitter.on(() => {
    myCustomEmitter.stopPropagation();
    console.log('I am number one.');
});

myCustomEmitter.on(() => {
    console.log('I am number two.');
});

myCustomEmitter.emit();
// > 'I am number one.'

Only the first event listener will be called.

FAQs

Package last updated on 02 Aug 2018

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