2.0.5 • Published 6 years ago

@amphibian/emitter v2.0.5

Weekly downloads
5
License
ISC
Repository
gitlab
Last release
6 years ago

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.

2.0.5

6 years ago

2.0.4

6 years ago

2.0.3

6 years ago

2.0.2

6 years ago

2.0.1

6 years ago

2.0.0

6 years ago

1.1.9

7 years ago

1.1.7

8 years ago

1.1.8

8 years ago

1.1.6

8 years ago

1.1.5

8 years ago

1.1.4

8 years ago

1.1.3

8 years ago

1.1.2

8 years ago

1.1.1

8 years ago

1.1.0

8 years ago

1.0.1

8 years ago

1.0.0

8 years ago