Norvell, Please Merge

    @bemoje/emitterize

    0.0.1 • Public • Published

    @bemoje/emitterize

    Upgrade a class to become an EventEmitter without extending. Uses the 'eventemitter3' module, but there are some major differences in terms of syntax-logic. EventName strings are out. Instead of emitter.on('end', fn), you just do emitter.onEnd(fn). When the main exported method is called, you provide all the event-names, from which 'onEnd' is created. Internally, a normal UNCHANGED emitter instance is created and used. Handling of eventNames is automatic (and uses symbols which performs faster), and you don't have to do any of it yourself. It is all on your own class's prototype, so it's inheritable. The only thing done on the instance, is setting the (hidden) actual EventEmitter object there. Nothing else is done on your instances. It works the same way with all the eventName-specific methods. onEnd(fn), onceEnd, emitEnd(data, ctx)... etc.

    install

    npm install --save @bemoje/emitterize

    Docs

    export function emitterize(Class: Function): Function
    
    Class.defineEvents = function(...eventNames: Array<string>): Function

    usage

    import emitterize from '@bemoje/emitterize'
    
    // normal class
    let Cool = class {
    	constructor() {
    		this.a = 5
    	}
    	hey() {
    		this.emitStart('now starting')
    	}
    }
    
    // it happens here
    Cool = emitterize(Cool).defineEvents('end', 'start', 'error')
    
    // now test
    const ins = new Cool()
    
    ins.onStart((data) => {
    	console.log(data)
    })
    
    ins.hey()
    //=> event: "now starting"

    Install

    npm i @bemoje/emitterize

    DownloadsWeekly Downloads

    2

    Version

    0.0.1

    License

    MIT

    Unpacked Size

    5.23 kB

    Total Files

    3

    Last publish

    Collaborators

    • bemoje