npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

ascend-events

v0.1.5

Published

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

Downloads

5

Readme

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'