Socket
Book a DemoInstallSign in
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.0.0
Source
npmnpm
Version published
Maintainers
1
Created
Source

@dekkai/event-emitter
CI 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 {
    // ...
}

// 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

event-emitter

FAQs

Package last updated on 18 Aug 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

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.