New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

capillaries

Package Overview
Dependencies
Maintainers
0
Versions
22
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

capillaries

Javascript Events

  • 5.0.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
10
decreased by-44.44%
Maintainers
0
Weekly downloads
 
Created
Source

Capillaries Tests

Javascript Events and Hooks

Getting Started

Installation

Installation can be done via package managers such as npm or yarn

% npm install capillaries

# or

% yarn add capillaries

Events

import { Events } from 'capillaries';

const event = new Events();

const listener = function (payload) {
  console.log('Event Received:', payload);
};

// create a event listeners
event.on('connecting', listener);
event.on('connected', listener, this); // optionally bind context to the listener when invoked

// listen to all events
event.on('*', (type, payload) => {});

// dispatch events
event.emit('connected', 'paylod');

// remove a event listener
const unsubscribe = event.on('connected', listener);
unsubscribe();

// remove all listeners for given event
event.unbindAll('connected');

// unbind all event listeners
event.unbindAll();

AsyncEvents

import { AsyncEvents } from 'capillaries';

const event = new AsyncEvents();

const handler = async function (payload) {
  console.log('Event Received:', payload);
};

// create a event handler
event.on('connected', handler);

// call the event
await event.call('connected', 'paylod');

// remove a event listener
const unsubscribe = event.on('connected', handler);
unsubscribe();

// unbind/remove all events
event.unbindAll();

Only one event handler can be attached per event. Attaching more than one event will throw an error.

Hooks

import { Hooks } from 'capillaries';

const hooks = new Hooks();

// create a tap
hooks.tap('Hook', () => {
  return 'Hello World!';
});

hooks.tap('AsyncHook', async () => {
  return 'Hello World!';
});

// Call the taps
hooks.call('Hook', payload); //-> returns undefined
hooks.callWaterFall('Hook', payload); //-> returns 'Hello World!'
hooks.callAsync('AsyncHook', payload); // awaits on taps, returns undefined
hooks.callAsyncWaterFall('AsyncHook', payload); // awaits on taps, returns 'Hello World!'

// remove all hooks
hooks.clear();

Hooks are executed in order. The calling waterfall hook passes a return value from each function to the next function and returns data from the last tap

Browser compatibility

  • Chrome 38+
  • Edge 12+
  • Firefox 13+
  • Opera 25+
  • Safari 8+
  • Internet Explorer 11

Keywords

FAQs

Package last updated on 22 Jun 2024

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