listenerjs
v1.0.1
Published
Event subscription made simple for JavaScript
Downloads
4
Maintainers
Readme
listenerjs
Hey dude! Help me out for a couple of 🍻! It will provide inspiration to keep this event subscription lib sober. You can always request for new functionaries.
Installation
npm install -D listenerjs
Usage
// someModule module definition
import { createEvent } from 'listenerjs';
const event1 = createEvent();
const event2 = createEvent();
function fakeEvent1(event) {
setTimeout(() => event.dispatch("5000 after"), 5000);
setTimeout(() => event.dispatch("10000 after"), 10000);
}
export default = {
onSomeEvent: (callback) {
fakeEvent1(event1);
return event1.addListener(callback);
},
onceSomeEvent: (callback) {
fakeEvent1(event1);
return event1.once(callback);
},
onSomeOtherEvent: event2.addListener
...,
}
// Usage
import { onSomeEvent, onceSomeEvent, onSomeOtherEvent } from 'someModule';
// Will log "5000 after" and "10000 after"
onSomeEvent(payload => console.log(payload)));
// Will log "5000 after" only
onceSomeEvent(payload => console.log(payload)));
// There is no specification for the dispatch of this event...
const subscription = onSomeOtherEvent(payload => console.log(payload)));
// remove subscription
subscription2.remove();
API
When created an event, it will expose:
Property | Type | Parameters | Returns | | --------- | -------- |-------------------- | ------------ | | addListener | function | function[, context] | { remove } | | dispatch | function | any | null | | once | function | function[, context] | { remove } |