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

2.0.0 • Public • Published

Disposable-Event

Greenkeeper badge

disposable-event is an npm module built to normalize event registration and disposal. We have often dealt with situations where there are inconsistent ways to add or remove event listeners in different APIs, disposable-event normalizes them all and gives authors a beautiful API

Installation

npm install --save disposable-event

API

function disposableEvent(target, eventName, callback, options): Disposable

Usage

import disposableEvent from 'disposable-event'
import {Server} from 'ws'

// Adding listeners
const eventListeners = []
eventListeners.push(disposableEvent(document, 'click', function() {
  console.log('document was clicked')
}))
eventListeners.push(disposableEvent(document, 'keydown', function() {
  console.log('key was pressed')
}))

const server = new Server()
eventListeners.push(disposableEvent(server, 'error', function(error) {
  console.log('error occured', error)
}))

// Disposing listeners
eventListeners.forEach(disposable => disposable.dispose())

License

This project is licensed under the terms of MIT License, see the LICENSE file for more info

Readme

Keywords

Package Sidebar

Install

npm i disposable-event

Weekly Downloads

98

Version

2.0.0

License

MIT

Unpacked Size

5.88 kB

Total Files

7

Last publish

Collaborators

  • steelbrain