event-binder
v0.1.1
Published
An event binder utility for event binding and unbinding easier
Downloads
1
Readme
event-binder
An event binder utility for event binding and unbinding easier
Usage
Sample event emitter
// socket.ts
import {AnyFunction} from 'event-binder';
// A event aware class
export class Socket extends EventEmitter {
on(event: 'close', listener: AnyFunction): this;
on(event: 'data', listener: (data: Buffer | string) => void): this;
on(event: string, listener: AnyFunction): this {
return super.on(event, listener);
}
}
bind
for binding one listener
import {bind} from 'event-binder';
// any event emitter
const socket = new Socket();
const unbind = bind(socket, 'data', data => {});
// after all done
unbind();
Binder
for binding multiple listeners
import {Binder} from 'event-binder';
// any event emitter
const socket = new Socket();
const binder = Binder.for(socket)
.bind('data', data => {})
.bind('close', () => {});
// after all done
binder.unbind();