event-bind
v0.1.4
Published
An event binder utility for event binding and unbinding easier
Downloads
2
Readme
event-bind
An event binder utility for event binding and unbinding easier
Usage
socket.ts
// socket.ts
import {AnyFunction} from 'event-bind';
// 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
one listener
import {bind} from 'event-bind';
// any event emitter
const socket = new Socket();
const unbind = bind(socket, 'data', data => {});
// after all done
unbind();
Binder
for multiple listeners
import {Binder} from 'event-bind';
// any event emitter
const socket = new Socket();
const binder = Binder.for(socket)
.bind('data', data => {})
.bind('close', () => {});
// after all done
// unbind all
binder.unbind();
// unbind exact properties
binder.unbind('data', 'close');