Neverending Perpetual Motion
    Have ideas to improve npm?Join in the discussion! »

    @lume/eventful
    TypeScript icon, indicating that this package has built-in type declarations

    0.1.1 • Public • Published

    @lume/eventful

    Emit and subscribe to events.

    This is a standard event listener pattern, similar to EventEmitter in Node where objects have a .on('event-name') method, or EventTarget in the web where HTML elements have a .addEventListener('event-name') method.

    npm install @lume/eventful

    Usage

    @lume/eventful provides a class (or mixin) called Eventful that your objects can extend from so that they can emit events and other code can subscribe to those events.

    import {Eventful} from '@lume/eventful'
    
    class Dog extends Eventful {
    	makeHungry() {
    		// An instance of Dog emits a "hungry" event at some point.
    		this.emit('hungry')
    	}
    
    	eat(food) {
    		// ... dog eats food ...
    	}
    }
    
    const dog = new Dog()
    
    // When the dog emits the "hungry" event, let's feed it.
    dog.on('hungry', () => {
    	dog.feed('chow')
    })
    
    // This triggers the "hungry" event, which causes the callback passed to
    // `dog.on('hungry')` to fire.
    dog.makeHungry()

    That's basically it. See also Eventful.test.ts to get more of an idea.

    Install

    npm i @lume/eventful

    DownloadsWeekly Downloads

    4

    Version

    0.1.1

    License

    MIT

    Unpacked Size

    167 kB

    Total Files

    19

    Last publish

    Collaborators

    • avatar