
Security News
Deno 2.2 Improves Dependency Management and Expands Node.js Compatibility
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
arbitrary-emitter
Advanced tools
Event emitter with ES6 Map sugar for modern browsers and node.js (~400 bytes). arbitrary-emitter.jacoborus.codes
arbitrary-emitter stores listeners and actions in Maps, this allows to use arbitrary values as keys for your listeners.
It's written in vanilla ES6, so you will have to transpile it before using it in old browsers or node.js < v5.9
on
, off
, once
and emit
)on
method returns an unsubscription function (like in redux.js)Install with npm, clone the repo or download and extract the zip. Then import or insert it as script tag.
const emitter = arbitraryEmitter()
const key = {}
emitter.on(key, () => doSomething())
// will `doSomething`
emitter.emit(key)
Adds the listener
function to the end of the listeners array for the event tagged with eventKey
. eventKey
can be any type of value. A check is made to see if the listener has already been added so it won't be called multiple times. Event listeners are invoked in the order they are added.
on
returns removeListener method
const key = {}
let removeListener = emitter.on(key, () => doSomething())
emitter.emit(key) // will `doSomething`
emitter.emit(key) // will `doSomething`
removeListener()
emitter.emit(key) // won't do anything
Same as on
, but listener
will be triggered just one time, then it will be removed.
const key = {}
emitter.once(key, () => doSomethingOnce())
emitter.emit(key) // will `doSomethingOnce`
emitter.emit(key) // won't do anything
Synchronously calls each of the listeners registered for the event tagged with eventKey
, passing the supplied argument options
to each
emitter.on('test', (opts) => console.log(opts.test))
const options = { test: 'Testing!' }
emitter.emit('test', options) // => 'Testing!'
eventKey
but no listener
is passed all the listeners and its key will be removedeventKey
and listener
are passed as arguments just the listener will be removed from its groupemitter.off(key, action) // will remove action from listeners
emitter.off(key) // will remove all the listeners tagged with `key`, and the tag itself
emitter.off() // will remove all the listeners from all the eventKeys and the eventKeys themselves
npm test
Build browser tests (npm run build-tests
) and open test/test.html
npm run build-umd
npm run build-tests
npm run build
© 2016 Jacobo Tabernero - Released under MIT License
FAQs
Map based event emitter in ~350 bytes
The npm package arbitrary-emitter receives a total of 2 weekly downloads. As such, arbitrary-emitter popularity was classified as not popular.
We found that arbitrary-emitter 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.
Security News
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
Security News
React's CRA deprecation announcement sparked community criticism over framework recommendations, leading to quick updates acknowledging build tools like Vite as valid alternatives.
Security News
Ransomware payment rates hit an all-time low in 2024 as law enforcement crackdowns, stronger defenses, and shifting policies make attacks riskier and less profitable.