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

@nichoth/events

v3.2.0

Published

Event emitter and helpers

Downloads

61

Readme

events

tests Socket Badge module types license

An extra minimal event emitter

featuring

  • 0 production dependencies
  • CJS and ESM versions
  • 637 bytes minified and gzipped

install

npm i -S @nichoth/events

example

You can pass in an array of valid event names. If you subscribe to or emit an event not in the list, this will throw a runtime error. Also, you can create a typed event bus because Bus takes a type argument.

create an event bus, with types

import { Bus } from '@nichoth/events'

const eventTree = Bus.createEvents({
    a: ['b', 'c', 'd'],
    b: {
        _: ['e', 'f'],
        c: ['1', '2', '3']
    }
})
const events = Bus.flatten(eventTree)

const bus = new Bus<Array<typeof events[number]>>(events)

bus.on(eventTree.a)

create an event bus

import { Bus } from '@nichoth/events'
const bus = new Bus()

// you can pass in a list of event names that are allowed.
// If you subscribe or emit something not in the list, it will throw an error.
const bus2 = new Bus(['valid', 'events'])

create namespaced events

Take an object of arrays of strings, and return a new object where the leaf nodes are strings containing the full object path.

import { Bus } from '@nichoth/events'

Bus.createEvents({
    a: {
        _: [1, 2, 3]
        b: {
            c: [1,2,3]
        }
    }
})

// => {
//   a: {
//     1: 'a.1',
//     2: 'a.2',
//     3: 'a.3
//     b: {
//       c: {
//         1: 'a.b.c.1',
//         2: 'a.b.c.2',
//         3: 'a.b.c.3'
//       }
//     }
//   },
// }
//

Bus.flatten

Get an array of the leaf node values of an object of any shape, for example the return value of Bus.createEvents.

It's recommended to use the .flatten static function to get the event name values after calling .createEvents. Or, if you pass in anything that is not an array, the constructor will call .flatten on it.

import { Bus } from '@nichoth/events'

const events = Bus.createEvents({
    a: {
        _: [1, 2, 3]
        b: {
            c: [1,2,3]
        }
    }
})

// pass in a list of valid event names
const bus = new Bus(Bus.flatten(events))
// is the same as
const bust2 = new Bus(events)

subscribe

import { Bus } from '@nichoth/events'
const bus = new Bus()

const off = bus.on(events.a['1'], (data) => {
    t.equal(data, 'test data', 'first listener gets the event')
    off()  // unsubscribe
})

emit events

import { Bus } from '@nichoth/events'
const bus = new Bus()

bus.emit(events.a['1'], 'test data')

You can partially apply the the .emit function

const emitFoo = bus.emit('foo')

bus.on('foo', data => {
    console.log(data)
    // => { example: 'data' }
})

emitFoo({ example: 'data' })

develop

Install dev deps with --legacy-peer-deps.

npm i --legacy--peer-deps

test

npm test