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

callback-registry

Package Overview
Dependencies
Maintainers
1
Versions
22
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

callback-registry

Registry for callbacks

  • 2.3.3
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

Intro

A simple registry for callbacks that allows you to add one or more callbacks under some key and then execute all callbacks under some key.

Example:

const registryFactory = require('callback-registry');

const registry = registryFactory();

// add a new callback for that event key
registry.add('event-key', function(){
    console.log('the event occurred')
});

// execute all callbacks that were registred for that key
registry.execute('event-key');

Passing arguments

You can pass any arguments to the callbacks when you execute them

// execute all callbacks that were registred for that key
registry.execute('event-key', arg1, arg2, arg3);

Returning results

The execute method returns an array with the results returned from the callbacks.

Removing a callback

When you add a new callback a function is returned that can be used to unsubscribe


// A callback that will be called just the first time
var unsubscribe = registry.add('event-key', function(){
    console.log('the event occurred');
    unsubscribe();
});

Change log

  • 2.3.2
    • fix case where unsubscribe function removes all subscriptions with the same callback reference
  • 2.1.1
    • return empty array as result if no subscribers
    • catch errors in user callbacks (returns undefined in the result if error)

Keywords

FAQs

Package last updated on 24 Aug 2018

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