
Product
Introducing Socket Firewall Enterprise: Flexible, Configurable Protection for Modern Package Ecosystems
Socket Firewall Enterprise is now available with flexible deployment, configurable policies, and expanded language support.
@nichoth/events
Advanced tools
An extra minimal event emitter
featuring
npm i -S @nichoth/events
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.
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)
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'])
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'
// }
// }
// },
// }
//
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)
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
})
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' })
Install dev deps with --legacy-peer-deps.
npm i --legacy--peer-deps
npm test
FAQs
Event emitter and helpers
The npm package @nichoth/events receives a total of 108 weekly downloads. As such, @nichoth/events popularity was classified as not popular.
We found that @nichoth/events demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
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.

Product
Socket Firewall Enterprise is now available with flexible deployment, configurable policies, and expanded language support.

Security News
Open source dashboard CNAPulse tracks CVE Numbering Authorities’ publishing activity, highlighting trends and transparency across the CVE ecosystem.

Product
Detect malware, unsafe data flows, and license issues in GitHub Actions with Socket’s new workflow scanning support.