Wondering what’s next for npm?Check out our public roadmap! »

    @amphibian/emitter

    2.0.5 • Public • Published

    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.

    Keywords

    none

    Install

    npm i @amphibian/emitter

    DownloadsWeekly Downloads

    13

    Version

    2.0.5

    License

    ISC

    Unpacked Size

    209 kB

    Total Files

    6

    Last publish

    Collaborators

    • avatar