event-bind
TypeScript icon, indicating that this package has built-in type declarations

0.1.4 • Public • Published

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');

Readme

Keywords

none

Package Sidebar

Install

npm i event-bind

Weekly Downloads

1

Version

0.1.4

License

MIT

Unpacked Size

30.4 kB

Total Files

19

Last publish

Collaborators

  • towyuan