Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

composite-disposable

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

composite-disposable

Simple library for implementing and consuming evented APIs

  • 1.0.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
1
decreased by-66.67%
Maintainers
1
Weekly downloads
 
Created
Source

composite-disposable

This is a simple library for implementing event subscription APIs. Inspired https://github.com/atom/event-kit. Use default event handler in Node.js.

Implementing Event Composite-Disposable Subscription APIs

const { EventEmitter } = require('events');
const CompositeDisposable = require('composite-disposable');


class User {
  constructor() {
    this.emitter = new EventEmitter();
    this.subscriptions = new CompositeDisposable();
  }

  onDidChangeName(callback) {
    this.subscriptions.add(this.emitter.on('change-name', callback));
  }

  setName(name) {
    if (this.name !== name) {
      this.name = name;
      this.emitter.emit('change-name', this.name);
    }

    return this.name;
  }

  destroy() {
    this.subscriptions.dispose();
  }
}

In the example above, we implement ::onDidChangeName on the user object, which will register callbacks to be invoked whenever the user's name changes. To do so, we make use of an internal EventEmitter instance. We use ::on to subscribe the given callback in ::onDidChangeName, and ::emit in ::setName to notify subscribers. Finally, when the User instance is destroyed we call ::dispose on the emitter to unsubscribe all subscribers.

Keywords

FAQs

Package last updated on 24 Dec 2017

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc