![Oracle Drags Its Feet in the JavaScript Trademark Dispute](https://cdn.sanity.io/images/cgdhsj6q/production/919c3b22c24f93884c548d60cbb338e819ff2435-1024x1024.webp?w=400&fit=max&auto=format)
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.
cocktail-trait-eventable
Advanced tools
A trait to use an Emitter as a delegate.
npm install cocktail --save
npm install cocktail-trait-eventable --save
A getEmitter
method is required as a glue code for this trait.
getEmitter()
: should return an EventEmitter instance or any other object with the same api as node events.EventEmitter
class.
MyClass.js
var cocktail = require('cocktail'),
Eventable = require('cocktail-trait-eventable'),
EventEmitter = require('events').EventEmitter;
cocktail.mix({
'@exports': module,
'@as' : 'class',
'@traits' : [Eventable],
constructor: function() {
this._emitter = new EventEmitter();
},
// glue code for Eventable Trait
getEmitter: function() {
return this._emitter;
},
doSomethingAndFireEvent: function(){
// ... do something here
// and
// now fire an event 'firing'
this.emit('firing', this);
}
});
And then in your index you can do:
index.js
var MyClass = require('./MyClass'),
obj;
obj = new MyClass();
//we can call `on` or `addListener` here since our MyClass is Eventable
obj.on('firing', function(){ console.log('Event Fired!'); });
obj.doSomethingAndFireEvent();
The following methods will be publicly available on the host class:
addListener(eventName, handler)
: Adds a listener to the end of the listeners array for the specified event.
myObj.addListener('event', function(){/*handler*/});
addListener(options)
: Adds a listener to the end of the listeners array for the specified event.
myObj.addListener({event: myObj.onEvent, scope: myObj});
emit(eventName, arg1, arg2, ..., argN)
: Execute each of the listeners in order with the supplied arguments.
myObj.emit('event', myObj, true, 1, 'another param');
on(eventName, handler)
: Idem addListener
method. myObj.on('event', function(){/*handler*/});
on(options)
: Idem addListener
method. myObj.on({event: myObj.onEvent, scope: myObj});
once(eventName, handler)
: Adds a one time event listener.
myObj.once('event', function(){/*handler*/});
once(options)
: Adds a one time event listener.
myObj.once({event: myObj.onEvent, scope: myObj});
removeListener(eventName, handler)
: Removes the given handler for the event.
myObj.removeListener('event', funcHandler);
removeListener(options)
: Removes the given handler for the event.
myObj.removeListener({event: myObj.onEvent});
removeAllListeners(eventName)
: Remove all listeners for the given event.
myObj.removeAllListeners();
FAQs
EventEmitter as delegate trait
We found that cocktail-trait-eventable 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.