0.0.0 • Published 7 years ago

@hscmap/event-emitter v0.0.0

Weekly downloads
7
License
-
Repository
github
Last release
7 years ago

Event Emitter

EventEmitter for TypeScript

Install

npm install --save @hscmap/event-emitter

Example

import { EventEmitter } from '@hscmap/event-emitter'


class MyEvent1 {
    constructor(readonly message: string) { }
}

class MyEvent2 {
    constructor(readonly message: string) { }
}


const ee = new EventEmitter()


const off1 = ee.on(MyEvent1, e => {
    throw e
})


ee.on(MyEvent2, e => {
    throw e
})


try {
    ee.emit(new MyEvent2('hello'))
    throw new Error()
}
catch (e) {
    console.assert(e instanceof MyEvent2)
    console.assert(e.message == 'hello')
}


try {
    ee.emit(new MyEvent1('world'))
    throw new Error()
}
catch (e) {
    console.assert(e instanceof MyEvent1)
    console.assert(e.message == 'world')
}


off1()


try {
    ee.emit(new MyEvent1('hello'))
    throw new Error('no handler fired')
}
catch (e) {
    console.assert(e instanceof Error && e.message == 'no handler fired')
}