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

ascend-events

Package Overview
Dependencies
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

ascend-events

A simple event emitter for learning and packaging purposes, written in TypeScript.

  • 0.1.5
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
1
Maintainers
1
Weekly downloads
 
Created
Source

Ascend Events

Build Status codecov

Installation

// npm
npm install ascend-events

// yarn
yarn install ascend-events

API

EventEmitter

Create an Event Emitter.

import { EventEmitter } from 'ascend-events';

// New EventEmitter
const eventEmitter = new EventEmitter();

// Extend a custom class
class MyClass extends EventEmitter {
    constructor() {
        super();
    }
}

addListener

Adds a new event listener to the end of the listener array that is associated with the passed eventName. If no key is found, it will set a new element using the passed eventName and listener.

params

  • eventName {string} | {symbol} (required) The name of the event being added
  • listener {Function} (required) The callback function

returns

  • this {EventEmitter}
const eventEmitter = new EventEmitter();

eventEmitter.addListener('listener', () => console.log('listener'));

emit

Calls each listener associated with the passed eventName in the order that they were added. Additional arguments are passed to each listener callback.

params

  • eventName {string} | {symbol} (required) The name of the event being emitted
  • ...args {any[]} (Optional) Arguments to be passed to each listener function

returns

  • this {EventEmitter}
const eventEmitter = new EventEmitter();

eventEmitter.add('test', (msg: string) => console.log('msg: ', msg));

eventEmitter.emit('test', 'hello world');

// prints 'msg: hello world'

getListenerCount

Returns the number of listeners associated with the passed eventName.

params

  • eventName {string} | {symbol} (required) The name of the event to get the listener count of

returns

  • {number}
const eventEmitter = new EventEmitter();

eventEmitter.addListener('msg', (msg: msg) => console.log('msg: ', msg));

eventEmitter.getListenerCount('msg'); // returns 1
eventEmitter.getListenerCount('doesntExist'); // returns 0

setMaxListeners

Sets the maximum number of listeners on this instance of the EventEmitter.

params

  • num {number} (required) The number of maximum listeners

returns

  • this {EventEmitter}
const eventEmitter = new EventEmitter();

eventEmitter.setMaxListeners(5);

console.log(eventEmitter.maxListeners); // prints 5

listeners

Returns the listeners associated with the passed eventName, if no key was found, returns an empty array [].

params

  • eventName {string} | {symbol} (required)

returns

  • listeners {Listener[]}
const eventEmitter = new EventEmitter();

eventEmitter.addListener('event', () => null)

eventEmitter.listeners('event') // returns [ { fn: [Function], once: false } ]

off

alias for EventEmitter.removeListener()

params

  • eventName {string} | {symbol} (required)
  • listener {Function} (required)

returns

  • this {EventEmitter}
const eventEmitter = new EventEmitter();

eventEmitter.on('msg', (msg: string) => console.log('msg: ', msg));

eventEmitter.off('msg', (msg:string) => console.log('msg: ', msg));

on

alias for eventEmitter.addListener()

params

  • eventName {string} | {symbol} (required)
  • listener {Function} (required)

returns

  • this {EventEmitter}
const eventEmitter = new EventEmitter();

eventEmitter.on('msg', (msg: string) => console.log('msg: ', msg));

eventEmitter.emit('msg', 'hello world');

once

Adds a listener associated to the passed eventName that is triggered once and then removed.

params

  • eventName {string} | {symbol} (required)
  • listener {Function} (required)

returns

  • this {EventEmitter}
const eventEmitter = new EventEmitter();

eventEmitter.once('msg', (msg: string) => console.log('msg: ', msg));

eventEmitter.emit('msg', 'hello world'); // prints 'msg: hello world'

eventEmitter.listeners('msg'); // returns []

prependListener

Adds the passed listener to the beginning of the listener array associated with the given eventName.

params

  • eventName {string} | {symbol} (required)
  • listener {Function} (required)

returns

  • this {EventEmitter}
const eventEmitter = new EventEmitter();

eventEmitter.addListener('msg', () => 'called second');

eventEmitter.prependListener('msg', () => 'called first');

eventEmitter.emit('msg');
// prints
//  'called first'
//  'called second'

prependOnceListener

Adds the passed listener to the beginning of the listener array associated with the given eventName, once triggered will be removed.

params

  • eventName {string} | {symbol} (required)
  • listener {Function} (required)

returns

  • this {EventEmitter}
const eventEmitter = new EventEmitter();

eventEmitter.prependOnceListener('msg', () => 'One time');

eventEmitter.emit('msg');

eventEmitter.listeners('msg'); // returns []

removeAllListeners

Removes all listeners associated with the passed eventName, if no eventName was given, all listeners are removed entirely.

params

  • eventName {string} | {symbol} (optional)

returns

  • this {EventEmitter}
const eventEmitter = new EventEmitter();

eventEmitter.addListener('msg', () => console.log('gets removed'));
eventEmitter.addListener('msg2', () => console.log('also gets removed'));

eventEmitter.removeAllListeners();

removeListener

Removes listener associated with the passed eventName

params

  • eventName {string} | {symbol} (required)
  • listener {Function} (required)

returns

  • this {EventEmitter}
const eventEmitter = new EventEmitter();

eventEmitter.addListener('msg', () => console.log('gets removed'));
eventEmitter.addListener('msg', () => console.log('does not get removed'));

eventEmitter.removeListener('msg', () => console.log('gets removed'));

eventEmitter.emit('msg'); //  prints 'does not get removed'

Keywords

FAQs

Package last updated on 16 Jan 2022

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