🚀 DAY 2 OF LAUNCH WEEK: Announcing Socket Certified Patches: One-Click Fixes for Vulnerable Dependencies.Learn more →
Socket
Book a DemoInstallSign in
Socket

@nichoth/events

Package Overview
Dependencies
Maintainers
1
Versions
52
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@nichoth/events

Event emitter and helpers

Source
npmnpm
Version
3.1.1
Version published
Weekly downloads
21
75%
Maintainers
1
Weekly downloads
 
Created
Source

events tests

An event emitter and helpers

featuring

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

install

npm i -S @nichoth/events

example

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'])

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]
        }
    }
})

const bus = new Bus(Bus.flattern(events))
// is the same as
const bust2 = new Bus(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'
//       }
//     }
//   },
// }
//

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

FAQs

Package last updated on 14 Sep 2023

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