Security News
Oracle Drags Its Feet in the JavaScript Trademark Dispute
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
An enhanced EventEmitter with extra methods for detecting whether an event has any handlers or not for efficient event handler attachment.
An enhanced EventEmitter with extra methods for detecting whether an event has any handlers or not for efficient event handler attachment.
npm install emitter-b
or download the built package EmitterB.umd.js
from the 'dist' folder in the repository
var EmitterB = require('emitter-b') // node.js and webpack
define(['EmitterB.umd.js'], function(EmitterB) { ... } // amd
<script src="EmitterB.umd.js"></script>
<script>
EmitterB; // global 'EmitterB' module object
</script>
All methods and properties from EventEmitter
are inherited. Most of these are inherited from EventEmitter
. The important ones:
emitter.emit(event, data, data2, ...)
- Emits an event that triggers handlers setup via the Gem's on
methods.
emitter.on(event, callback)
- Registers a callback
that will be called when the passed event
is emit
ted.
event
- The string event name to listen for.callback(data, data2, ...)
- the callback gets any arguments passed to emit
after the event name.emitter.once(event, callback)
- Like on
but the callback
will only be called the first time the event happens.
emitter.off(event, callback)
- Removes a callback as an event handler (the callback
won't be called for that event again).
emitter.removeListener(event,callback)
- Same as off
.
emitter.removeAllListeners(event)
- Removes all the callbacks for the passed event
.
emitter.removeAllListeners()
- Removes all callbacks.
ifon
The ifon
and related methods are useful primarily for performance reasons. They allow registering event listeners only when they're needed, so that the browser doesn't get overloaded with event handlers. Its recommended that ifon
is used whenever possible.
An example:
var child = EmitterB()
var parent = EmitterB()
var handler;
parent.ifon('someoneClickedTheThing', function() {
child.on('click', handler = function() {
parent.emit('someoneClickedTheThing', "I can't believe it")
})
})
parent.ifoff('someoneClickedTheThing', function() {
child.off('click', handler)
})
emitter.ifon(event, callback)
- Registers a callback that will be called when a handler is registered for event
if it had no handler registered previously. If there is already a listener attached to that event, callback
is called immediately.
emitter.ifon(callback)
- Registers a callback that will be called when the first handler for any event is registered.
callback(event)
- The callback gets the newly registered event type as its argument.emitter.ifoff(event, callback)
- Registers a callback that will be called when the last handler for event
is unregistered.
emitter.ifoff(callback)
- Registers a callback that will be called when the last handler for any event is unregistered.
callback(event)
- The callback gets the unregistered event type as its argument.emitter.removeIfon()
- Removes all ifon
handlers.
emitter.removeIfon(event)
- Removes all ifon
handlers for the passed event
.
emitter.removeIfon(callback)
- Removes callback
as an "all" ifon
handler (a callback passed to ifon
without an event).
emitter.removeIfon(event, callback)
- Removes callback
as an ifon
handler for the passed event
.
emitter.removeIfoff()
- Removes all ifoff
handlers.
emitter.removeIfoff(event)
- Removes all ifoff
handlers for the passed event
.
emitter.removeIfoff(callback)
- Removes callback
as an "all" ifoff
handler (a callback passed to ifoff
without an event).
emitter.removeIfoff(event, callback)
- Removes callback
as an ifoff
handler for the passed event
.
proxy
The proxy
method uses ifon
and ifoff
to minmize the number of event listeners that need to be attached in the system.
emitter.proxy(emitter, options)
- Proxies event registration to emitter
.
emitter
- The emitter (usually a Gem
) to proxy handler binding tooptions
- An object that defines what events are proxied. If undefined
, all events are proxied. The object can have one of the following properties:
only
- An array of events to proxy.except
- An array of events to not proxy. All other events are proxied.Example of proxy
:
var A = Text()
var B = Text()
A.proxy(B)
A.on("click", function(arg) {
console.log("hey hey heyyy! "+arg)
})
B.emit("click", "Ughh..") // console prints "hey hey heyyy! Ughh.."
Want to contribute? Start with the Contributing Guide!
Anything helps:
Released under the MIT license: http://opensource.org/licenses/MIT
FAQs
An enhanced EventEmitter with extra methods for detecting whether an event has any handlers or not for efficient event handler attachment.
The npm package emitter-b receives a total of 496 weekly downloads. As such, emitter-b popularity was classified as not popular.
We found that emitter-b 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
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
Security News
The Linux Foundation is warning open source developers that compliance with global sanctions is mandatory, highlighting legal risks and restrictions on contributions.
Security News
Maven Central now validates Sigstore signatures, making it easier for developers to verify the provenance of Java packages.