@md5crypt/listener
TypeScript icon, indicating that this package has built-in type declarations

1.1.1 • Public • Published

Event listener

Because everyone has to have their own event listener implementation for some reason.

Usage

import Listener from `@thunderbite/event-listener`

class FooBar {
    public readonly onFoo = new Listener()
    public readonly onBar = new Listener<(foo: string, bar: number) => void>()
    public foo() {
        this.onFoo.invoke()
    }
    public bar() {
        this.onBar.invoke("foo", 0xba6)
    }
}

const fooBar = new FooBar()

fooBar.onFoo.add(() => console.log("foo"))
fooBar.onFoo.add(() => {
    console.log("this will be executed only once")
    return Listener.REMOVE
})
fooBar.onBar.add((foo, bar) => console.log(foo + bar))

fooBar.foo()
fooBar.bar()

Instance methods

name signature description
add (listener: T) => T add a callback, returns the callback that was passed to it
remove (listener: T) => boolean remove a callback, returns true if the callback was found
clear () => void remove all callbacks
invoke (...args: Parameters<T>) => T invoke all callbacks

Dependencies (0)

    Dev Dependencies (3)

    Package Sidebar

    Install

    npm i @md5crypt/listener

    Weekly Downloads

    2

    Version

    1.1.1

    License

    MIT

    Unpacked Size

    5.72 kB

    Total Files

    5

    Last publish

    Collaborators

    • md5crypt