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

@dekkai/event-emitter

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@dekkai/event-emitter

An event emitter base class that supports omni-listeners. Supports browsers, node and deno.

  • 1.1.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
43
increased by72%
Maintainers
1
Weekly downloads
 
Created
Source

@dekkai/event-emitter
browser node deno opinion

@dekkai/event-emitter

An event emitter class, supports omni-listeners and symbol events. Tested on browsers, node.js and deno.

Check out the full API Documentation

Installation

Browser/NodeJS

$ yarn add @dekkai/event-emitter

Deno

// import from directly from a CDN, like unpkg.com
import {EventEmitter} from 'https://unpkg.com/@dekkai/event-emitter';

Usage

// import EventEmitter
import {EventEmitter} from '@dekkai/event-emitter';

// can be extended
class MyEmitter extends EventEmitter {
    // ...
}

// used as a mixin
const MyEmitter = EventEmitter.mixin(BaseClass);

// or injected in the inheritance chain
class MyEmitter extends EventEmitter.mixin(BaseClass) {
    // ...
}

// instantiate the emitter
const emitter = new MyEmitter();

// create an event handler
const handler = (evt, num0, num1) => console.log(`Event [${evt.toString()}] result: ${num0 + num1}`);

// register the handler
emitter.on('add', handler);

// events can also be symbols
const symbolEvent = Symbol('add-symbol');
emitter.on(symbolEvent, handler);

// emit events
emitter.emit('add', 4, 6); // Event [add] result: 10
emitter.emit(symbolEvent, 12, 8); // Event [Symbol(add-symbol)] result: 20

// remove previously added listeners
emitter.off('add', handler);
emitter.off(symbolEvent, handler);

// use omni-listeners to catch all events, register them using the `omniEvent` static property
const omniHandler = evt => console.log(evt.toString());
emitter.on(MyEmitter.omniEvent, omniHandler);

// the omni-listener will catch all events
emitter.emit('hello'); // hello
emitter.emit(Symbol('random symbol')); // Symbol(random symbol)

// removing omni-listeners works the same way as with regular listeners, but using the `omniEvent`
emitter.off(MyEmitter.omniEvent, omniHandler);

Check out the full API Documentation

Keywords

FAQs

Package last updated on 05 Nov 2020

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