listenerjs

1.0.1 • Public • Published

listenerjs

Beerpay Beerpay Build Status npm package coverage

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 }

License

MIT

Dependencies (0)

    Dev Dependencies (8)

    Package Sidebar

    Install

    npm i listenerjs

    Weekly Downloads

    2

    Version

    1.0.1

    License

    MIT

    Unpacked Size

    12.6 kB

    Total Files

    14

    Last publish

    Collaborators

    • saribe