Security News
PyPI Introduces Digital Attestations to Strengthen Python Package Security
PyPI now supports digital attestations, enhancing security and trust by allowing package maintainers to verify the authenticity of Python packages.
wildemitter
Advanced tools
A super lightweight EventEmitter similar to what comes in Node.js, but with a support for wildcard events '*' and grouped handlers
If you've ever listened for a click event in a browser you've used an emitter. But, user interaction isn't the only thing that can trigger an event worth listening to. You can also make other objects capable of emitting events. That's what wildemitter is for. You can extend your objects with it so that you can emit events from them and register handlers on them. This pattern helps you write more re-usable code becaause your objct doen't have to know how it's going to be used. It can simply emit events any time something happens that other code may be interested in.
You'll see this type of pattern a lot in node.js. Where lots of things in the standard libraries inherit from EventEmitter and emit various events to indicate progress, errors, completion, etc.
So, why make another one? Aren't there others already?
Well, yes there are, but not quite what I wanted. This one is largely based on the emitter in @visionmedia's UIKit. So, much props to TJ for that. But there were a few more things I wanted. Specifically the following:
*
or something*
)##How do I use it?
var Emitter = require('./wildemitter');
// the example object we're making
function Fruit(name) {
this.name = name;
// call emitter with this context
Emitter.call(this);
}
// inherit from Emitter
Fruit.prototype = new Emitter;
// a function that emits an events when called
Fruit.prototype.test = function () {
this.emit('test', this.name);
};
// set up some test fruits
var apple = new Fruit('apple');
apple.on('*', function () {
console.log('"*" handler called', arguments);
});
apple.on('te*', function () {
console.log('"te*" handler called', arguments);
});
apple.on('test', function () {
console.log('"test" handler called', arguments);
});
// calling the method that emits events.
apple.test();
// it should write the following the log:
/*
"test" handler called { '0': 'apple' }
"*" handler called { '0': 'test', '1': 'apple' }
"te*" handler called { '0': 'test', '1': 'apple' }
*/
// this will remove any handlers explicitly listening for 'test' events.
apple.off('test');
// calling our method again would this time only call the two wildcard handlers
// producing the following output
/*
"*" handler called { '0': 'test', '1': 'apple' }
"te*" handler called { '0': 'test', '1': 'apple' }
*/
// grouped handlers example, we'll create another fruit
var orange = new Fruit('orange');
// In this case "today" is the name of the group.
// here we'll bind some handlers that all pass 'today'
// as the group name
orange.on('test', 'today', someHandler);
orange.on('someOtherEvent', 'today', someHandler);
orange.on('*', 'today', someHandler);
// we can now unbind all three of those handlers like this
orange.releaseGroup('today');
Emitters are often something you want to be able to include in another lib. There's also file called wildemitter-bare.js that doesn't have any export mechanism.
You can run the tests with nodeunit
by running: nodeunit test.js
Written by @HenrikJoreteg inspired by TJ's emitter component.
Contributors:
##License MIT
If you like this follow @HenrikJoreteg on twitter.
FAQs
A super lightweight EventEmitter similar to what comes in Node.js, but with a support for wildcard events '*' and grouped handlers
The npm package wildemitter receives a total of 0 weekly downloads. As such, wildemitter popularity was classified as not popular.
We found that wildemitter demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 5 open source maintainers 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
PyPI now supports digital attestations, enhancing security and trust by allowing package maintainers to verify the authenticity of Python packages.
Security News
GitHub removed 27 malicious pull requests attempting to inject harmful code across multiple open source repositories, in another round of low-effort attacks.
Security News
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.