Socket
Socket
Sign inDemoInstall

tiny-emitter

Package Overview
Dependencies
0
Maintainers
1
Versions
22
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    tiny-emitter

A tiny (less than 1k) event emitter library


Version published
Weekly downloads
3.1M
increased by5.49%
Maintainers
1
Install size
76.2 kB
Created
Weekly downloads
 

Package description

What is tiny-emitter?

The tiny-emitter package is a lightweight event emitter and listener library. It allows you to create an object that can emit events and have listeners that respond to those events. This is useful for creating a simple pub-sub or observer pattern implementation without the overhead of larger libraries.

What are tiny-emitter's main functionalities?

Event Emission

Emitting events with optional data. Listeners for that event will be invoked.

const Emitter = require('tiny-emitter');
let emitter = new Emitter();
emitter.emit('event', 'some data');

Event Listening

Adding a listener for an event. The listener will be called whenever the specified event is emitted.

const Emitter = require('tiny-emitter');
let emitter = new Emitter();
emitter.on('event', function(data) {
  console.log(data);
});

One-time Event Listening

Adding a one-time listener for an event. The listener will be invoked only the first time the event is emitted.

const Emitter = require('tiny-emitter');
let emitter = new Emitter();
emitter.once('event', function(data) {
  console.log(data);
});

Removing Event Listeners

Removing a specific listener from an event.

const Emitter = require('tiny-emitter');
let emitter = new Emitter();
function listener(data) {
  console.log(data);
}
emitter.on('event', listener);
emitter.off('event', listener);

Other packages similar to tiny-emitter

Readme

Source

tiny-emitter

A tiny (less than 1k) event emitter library.

Install

npm

npm install tiny-emitter --save

Usage

var Emitter = require('tiny-emitter');
var emitter = new Emitter();

emitter.on('some-event', function (arg1, arg2, arg3) {
 //
});

emitter.emit('some-event', 'arg1 value', 'arg2 value', 'arg3 value');

Alternatively, you can skip the initialization step by requiring tiny-emitter/instance instead. This pulls in an already initialized emitter.

var emitter = require('tiny-emitter/instance');

emitter.on('some-event', function (arg1, arg2, arg3) {
 //
});

emitter.emit('some-event', 'arg1 value', 'arg2 value', 'arg3 value');

Instance Methods

on(event, callback[, context])

Subscribe to an event

  • event - the name of the event to subscribe to
  • callback - the function to call when event is emitted
  • context - (OPTIONAL) - the context to bind the event callback to

once(event, callback[, context])

Subscribe to an event only once

  • event - the name of the event to subscribe to
  • callback - the function to call when event is emitted
  • context - (OPTIONAL) - the context to bind the event callback to

off(event[, callback])

Unsubscribe from an event or all events. If no callback is provided, it unsubscribes you from all events.

  • event - the name of the event to unsubscribe from
  • callback - the function used when binding to the event

emit(event[, arguments...])

Trigger a named event

  • event - the event name to emit
  • arguments... - any number of arguments to pass to the event subscribers

Test and Build

Build (Tests, Browserifies, and minifies)

npm install
npm run build

Test

npm install
npm test

License

MIT

Keywords

FAQs

Last updated on 05 Feb 2019

Did you know?

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

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc