pax_global_header00006660000000000000000000000064126075373000014515gustar00rootroot0000000000000052 comment=752690aba16db3c33492924aea0ba94c2c609e65 wildemitter-1.2.0/000077500000000000000000000000001260753730000140465ustar00rootroot00000000000000wildemitter-1.2.0/.gitignore000066400000000000000000000000141260753730000160310ustar00rootroot00000000000000node_moduleswildemitter-1.2.0/README.md000066400000000000000000000106611260753730000153310ustar00rootroot00000000000000# WildEmitter - A lightweight event emitter that supports wildcard handlers ## What's an event emitter? 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 because your object don'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: - Super lightweight - Support for browser/node.js (browser use requires a CommonJS wrapper of some kind, like Stitch or Browserify) - Support for wildcard handlers (`*` or `something*`) - Support for grouping registered handlers and unbinding them all by their group name. This is really handy when, for example, you want unbind all handlers associated with a given "sub-page" within a single page app. - Available with Bower, `bower install wildemitter --save` ## How do I use it? ## ```js var Emitter = require('./wildemitter'); // the example object we're making function Fruit(name) { this.name = name; } //Mix the emitter behaviour into Fruit Emitter.mixin(Fruit); // 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'); ``` ### The old way If you don't want to use `Emitter.mixin`, you can still use it the old way: ```js function Fruit(name) { this.name = name; // call emitter with this context Emitter.call(this); } // and also inherit from Emitter Fruit.prototype = new Emitter; ``` ## Including 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. ## Building/Testing 1. Edit files in `/src` 2. Run `npm test` You can also run build by itself: `npm run build` ## Changelog - v1.0.1 [diff](https://github.com/henrikjoreteg/wildemitter/compare/v1.0.0...v1.0.1) - Fixes wildcard matching issue. - v1.0.0 [diff](https://github.com/henrikjoreteg/wildemitter/compare/v0.0.5...v1.0.0) - Copy emitter array before firing. Though its unlikely this could impact how your application functions, hence bumping the 1st major version number per semver conventions. ## Credits Written by [@HenrikJoreteg](http://twitter.com/henrikjoreteg) inspired by TJ's emitter component. Contributors: https://github.com/HenrikJoreteg/wildemitter/graphs/contributors ##License MIT If you like this follow [@HenrikJoreteg](http://twitter.com/henrikjoreteg) on twitter. wildemitter-1.2.0/bower.json000066400000000000000000000006741260753730000160660ustar00rootroot00000000000000{ "name": "wildemitter", "main": "wildemitter-bare.js", "version": "1.1.0", "homepage": "https://github.com/HenrikJoreteg/wildemitter", "authors": [ "Henrik Joreteg " ], "description": "A super lightweight EventEmitter.", "keywords": [ "events", "emitter", "browser" ], "license": "MIT", "ignore": [ "**/.*", "node_modules", "bower_components", "test", "tests" ] } wildemitter-1.2.0/build.js000066400000000000000000000016731260753730000155120ustar00rootroot00000000000000// Creates the andbang.js source from the provided API // specification. /*global __dirname*/ var fs = require('fs'), fileName = 'wildemitter.js', bareFileName = 'wildemitter-bare.js', template = fs.readFileSync(__dirname + "/src/template.js").toString(), context = { emitter: fs.readFileSync(__dirname + "/src/core.js").toString(), intro: fs.readFileSync(__dirname + "/src/intro.js").toString() }, outputPath = __dirname + '/' + fileName, bareOutputPath = __dirname + '/' + bareFileName; var mustache = require('mustache'), yetify = require('yetify'), colors = require('colors'); var code = mustache.render(template, context); console.log('\n' + yetify.logo()); fs.writeFileSync(outputPath, code, 'utf-8'); console.log(fileName.bold + ' file built.'.grey); fs.writeFileSync(bareOutputPath, context.emitter, 'utf-8'); console.log(bareFileName.bold + ' file built.'.grey + '\n'); process.exit(0); wildemitter-1.2.0/package.json000066400000000000000000000012421260753730000163330ustar00rootroot00000000000000{ "name": "wildemitter", "version": "1.2.0", "author": "Henrik Joreteg ", "scripts": { "build": "node ./build.js", "test": "npm run build && nodeunit test.js" }, "description": "A super lightweight EventEmitter similar to what comes in Node.js, but with a support for wildcard events '*' and grouped handlers", "repository": { "type": "git", "url": "https://github.com/HenrikJoreteg/wildemitter.git" }, "devDependencies": { "colors": "~0.6.2", "mustache": "~0.8.1", "nodeunit": "~0.8.2", "yetify": "~0.1.0" }, "keywords": [ "events", "emitter", "browser" ], "main": "wildemitter.js" } wildemitter-1.2.0/src/000077500000000000000000000000001260753730000146355ustar00rootroot00000000000000wildemitter-1.2.0/src/core.js000066400000000000000000000100441260753730000161220ustar00rootroot00000000000000function WildEmitter() { } WildEmitter.mixin = function (constructor) { var prototype = constructor.prototype || constructor; prototype.isWildEmitter= true; // Listen on the given `event` with `fn`. Store a group name if present. prototype.on = function (event, groupName, fn) { this.callbacks = this.callbacks || {}; var hasGroup = (arguments.length === 3), group = hasGroup ? arguments[1] : undefined, func = hasGroup ? arguments[2] : arguments[1]; func._groupName = group; (this.callbacks[event] = this.callbacks[event] || []).push(func); return this; }; // Adds an `event` listener that will be invoked a single // time then automatically removed. prototype.once = function (event, groupName, fn) { var self = this, hasGroup = (arguments.length === 3), group = hasGroup ? arguments[1] : undefined, func = hasGroup ? arguments[2] : arguments[1]; function on() { self.off(event, on); func.apply(this, arguments); } this.on(event, group, on); return this; }; // Unbinds an entire group prototype.releaseGroup = function (groupName) { this.callbacks = this.callbacks || {}; var item, i, len, handlers; for (item in this.callbacks) { handlers = this.callbacks[item]; for (i = 0, len = handlers.length; i < len; i++) { if (handlers[i]._groupName === groupName) { //console.log('removing'); // remove it and shorten the array we're looping through handlers.splice(i, 1); i--; len--; } } } return this; }; // Remove the given callback for `event` or all // registered callbacks. prototype.off = function (event, fn) { this.callbacks = this.callbacks || {}; var callbacks = this.callbacks[event], i; if (!callbacks) return this; // remove all handlers if (arguments.length === 1) { delete this.callbacks[event]; return this; } // remove specific handler i = callbacks.indexOf(fn); callbacks.splice(i, 1); if (callbacks.length === 0) { delete this.callbacks[event]; } return this; }; /// Emit `event` with the given args. // also calls any `*` handlers prototype.emit = function (event) { this.callbacks = this.callbacks || {}; var args = [].slice.call(arguments, 1), callbacks = this.callbacks[event], specialCallbacks = this.getWildcardCallbacks(event), i, len, item, listeners; if (callbacks) { listeners = callbacks.slice(); for (i = 0, len = listeners.length; i < len; ++i) { if (!listeners[i]) { break; } listeners[i].apply(this, args); } } if (specialCallbacks) { len = specialCallbacks.length; listeners = specialCallbacks.slice(); for (i = 0, len = listeners.length; i < len; ++i) { if (!listeners[i]) { break; } listeners[i].apply(this, [event].concat(args)); } } return this; }; // Helper for for finding special wildcard event handlers that match the event prototype.getWildcardCallbacks = function (eventName) { this.callbacks = this.callbacks || {}; var item, split, result = []; for (item in this.callbacks) { split = item.split('*'); if (item === '*' || (split.length === 2 && eventName.slice(0, split[0].length) === split[0])) { result = result.concat(this.callbacks[item]); } } return result; }; }; WildEmitter.mixin(WildEmitter); wildemitter-1.2.0/src/intro.js000066400000000000000000000007151260753730000163310ustar00rootroot00000000000000/* WildEmitter.js is a slim little event emitter by @henrikjoreteg largely based on @visionmedia's Emitter from UI Kit. Why? I wanted it standalone. I also wanted support for wildcard emitters like this: emitter.on('*', function (eventName, other, event, payloads) { }); emitter.on('somenamespace*', function (eventName, payloads) { }); Please note that callbacks triggered by wildcard registered events also get the event name as the first argument. */ wildemitter-1.2.0/src/template.js000066400000000000000000000000701260753730000170030ustar00rootroot00000000000000{{{intro}}} module.exports = WildEmitter; {{{emitter}}}wildemitter-1.2.0/test.js000066400000000000000000000072741260753730000153750ustar00rootroot00000000000000var Emitter = require('./wildemitter'); function Fruit(name) { this.name = name; Emitter.call(this); } Fruit.prototype = new Emitter; Fruit.prototype.test = function () { this.emit('test', this.name); }; // set up some test fruits var apple = new Fruit('apple'), orange = new Fruit('orange'); exports['Make sure wildcard handlers work'] = function (test) { var count = 0, cb = function () { return function () {count++} }; apple.on('*', cb()); apple.on('te*', cb()); // This should NOT add to count. Regression test for issue #4 apple.on('other*', cb()); apple.on('test', cb()); apple.test(); // sanity check to make sure we've got the emitter isolated to the instance orange.test(); test.equal(count, 3); apple.off('test'); // reset our counter count = 0; apple.test(); test.equal(count, 2); test.done(); }; exports['Test group binding and unbinding'] = function (test) { var count = 0, cb = function () { return function () {count++} }; // test our groups orange.on('test', 'lumped', cb()); orange.on('test', 'lumped', cb()); orange.on('test', 'lumped', cb()); orange.on('test', cb()); orange.test(); test.equal(count, 4); count = 0; orange.releaseGroup('lumped'); orange.test(); test.equal(count, 1); test.done(); }; exports['Test once for multiple functions'] = function (test) { var count, cb1, cb2; count = 0; cb1 = function () { count++; }; cb2 = function () { count++; }; orange.once('test', cb1); orange.once('test', cb2); orange.test(); test.equal(count, 2); orange.test(); test.equal(count, 2); test.done(); }; exports['Test on and off'] = function (test) { var count, cb1, cb2; var orange = new Fruit('orange'); count = 0; cb1 = function () { count++; }; cb2 = function () { count++; }; orange.on('test', cb1); orange.on('test2', cb2); orange.test(); test.equal(count, 1); orange.off('test', cb1); orange.test(); test.equal(count, 1); orange.emit('test2'); test.equal(count, 2); orange.off('test2', cb2); //ensure callbacks array is removed entirely test.ok(orange.callbacks.test == null); test.ok(orange.callbacks.test2 == null); test.done(); }; exports['Mixin to constructor'] = function (test) { function Vegetable(name) { this.name = name; } Emitter.mixin(Vegetable); console.log(Vegetable); Vegetable.prototype.test = function () { this.emit('test', this.name); }; // set up a test vegetable var asparagus = new Vegetable('asparagus'); var count, cb1, cb2; count = 0; cb1 = function () { count++; }; cb2 = function () { count++; }; lettuce.on('test', cb1); lettuce.on('test2', cb2); lettuce.test(); test.equal(count, 1); lettuce.off('test', cb1); lettuce.test(); test.equal(count, 1); lettuce.emit('test2'); test.equal(count, 2); test.done(); }; exports['Mixin to plain javascript objects'] = function (test) { var potato = {}; Emitter.mixin(potato); potato.test = function () { this.emit('test', this.name); }; var count, cb1, cb2; count = 0; cb1 = function () { count++; }; cb2 = function () { count++; }; potato.on('test', cb1); potato.on('test2', cb2); potato.test(); test.equal(count, 1); potato.off('test', cb1); potato.test(); test.equal(count, 1); potato.emit('test2'); test.equal(count, 2); test.done(); }; wildemitter-1.2.0/wildemitter-bare.js000066400000000000000000000100441260753730000176430ustar00rootroot00000000000000function WildEmitter() { } WildEmitter.mixin = function (constructor) { var prototype = constructor.prototype || constructor; prototype.isWildEmitter= true; // Listen on the given `event` with `fn`. Store a group name if present. prototype.on = function (event, groupName, fn) { this.callbacks = this.callbacks || {}; var hasGroup = (arguments.length === 3), group = hasGroup ? arguments[1] : undefined, func = hasGroup ? arguments[2] : arguments[1]; func._groupName = group; (this.callbacks[event] = this.callbacks[event] || []).push(func); return this; }; // Adds an `event` listener that will be invoked a single // time then automatically removed. prototype.once = function (event, groupName, fn) { var self = this, hasGroup = (arguments.length === 3), group = hasGroup ? arguments[1] : undefined, func = hasGroup ? arguments[2] : arguments[1]; function on() { self.off(event, on); func.apply(this, arguments); } this.on(event, group, on); return this; }; // Unbinds an entire group prototype.releaseGroup = function (groupName) { this.callbacks = this.callbacks || {}; var item, i, len, handlers; for (item in this.callbacks) { handlers = this.callbacks[item]; for (i = 0, len = handlers.length; i < len; i++) { if (handlers[i]._groupName === groupName) { //console.log('removing'); // remove it and shorten the array we're looping through handlers.splice(i, 1); i--; len--; } } } return this; }; // Remove the given callback for `event` or all // registered callbacks. prototype.off = function (event, fn) { this.callbacks = this.callbacks || {}; var callbacks = this.callbacks[event], i; if (!callbacks) return this; // remove all handlers if (arguments.length === 1) { delete this.callbacks[event]; return this; } // remove specific handler i = callbacks.indexOf(fn); callbacks.splice(i, 1); if (callbacks.length === 0) { delete this.callbacks[event]; } return this; }; /// Emit `event` with the given args. // also calls any `*` handlers prototype.emit = function (event) { this.callbacks = this.callbacks || {}; var args = [].slice.call(arguments, 1), callbacks = this.callbacks[event], specialCallbacks = this.getWildcardCallbacks(event), i, len, item, listeners; if (callbacks) { listeners = callbacks.slice(); for (i = 0, len = listeners.length; i < len; ++i) { if (!listeners[i]) { break; } listeners[i].apply(this, args); } } if (specialCallbacks) { len = specialCallbacks.length; listeners = specialCallbacks.slice(); for (i = 0, len = listeners.length; i < len; ++i) { if (!listeners[i]) { break; } listeners[i].apply(this, [event].concat(args)); } } return this; }; // Helper for for finding special wildcard event handlers that match the event prototype.getWildcardCallbacks = function (eventName) { this.callbacks = this.callbacks || {}; var item, split, result = []; for (item in this.callbacks) { split = item.split('*'); if (item === '*' || (split.length === 2 && eventName.slice(0, split[0].length) === split[0])) { result = result.concat(this.callbacks[item]); } } return result; }; }; WildEmitter.mixin(WildEmitter); wildemitter-1.2.0/wildemitter.js000066400000000000000000000110211260753730000167300ustar00rootroot00000000000000/* WildEmitter.js is a slim little event emitter by @henrikjoreteg largely based on @visionmedia's Emitter from UI Kit. Why? I wanted it standalone. I also wanted support for wildcard emitters like this: emitter.on('*', function (eventName, other, event, payloads) { }); emitter.on('somenamespace*', function (eventName, payloads) { }); Please note that callbacks triggered by wildcard registered events also get the event name as the first argument. */ module.exports = WildEmitter; function WildEmitter() { } WildEmitter.mixin = function (constructor) { var prototype = constructor.prototype || constructor; prototype.isWildEmitter= true; // Listen on the given `event` with `fn`. Store a group name if present. prototype.on = function (event, groupName, fn) { this.callbacks = this.callbacks || {}; var hasGroup = (arguments.length === 3), group = hasGroup ? arguments[1] : undefined, func = hasGroup ? arguments[2] : arguments[1]; func._groupName = group; (this.callbacks[event] = this.callbacks[event] || []).push(func); return this; }; // Adds an `event` listener that will be invoked a single // time then automatically removed. prototype.once = function (event, groupName, fn) { var self = this, hasGroup = (arguments.length === 3), group = hasGroup ? arguments[1] : undefined, func = hasGroup ? arguments[2] : arguments[1]; function on() { self.off(event, on); func.apply(this, arguments); } this.on(event, group, on); return this; }; // Unbinds an entire group prototype.releaseGroup = function (groupName) { this.callbacks = this.callbacks || {}; var item, i, len, handlers; for (item in this.callbacks) { handlers = this.callbacks[item]; for (i = 0, len = handlers.length; i < len; i++) { if (handlers[i]._groupName === groupName) { //console.log('removing'); // remove it and shorten the array we're looping through handlers.splice(i, 1); i--; len--; } } } return this; }; // Remove the given callback for `event` or all // registered callbacks. prototype.off = function (event, fn) { this.callbacks = this.callbacks || {}; var callbacks = this.callbacks[event], i; if (!callbacks) return this; // remove all handlers if (arguments.length === 1) { delete this.callbacks[event]; return this; } // remove specific handler i = callbacks.indexOf(fn); callbacks.splice(i, 1); if (callbacks.length === 0) { delete this.callbacks[event]; } return this; }; /// Emit `event` with the given args. // also calls any `*` handlers prototype.emit = function (event) { this.callbacks = this.callbacks || {}; var args = [].slice.call(arguments, 1), callbacks = this.callbacks[event], specialCallbacks = this.getWildcardCallbacks(event), i, len, item, listeners; if (callbacks) { listeners = callbacks.slice(); for (i = 0, len = listeners.length; i < len; ++i) { if (!listeners[i]) { break; } listeners[i].apply(this, args); } } if (specialCallbacks) { len = specialCallbacks.length; listeners = specialCallbacks.slice(); for (i = 0, len = listeners.length; i < len; ++i) { if (!listeners[i]) { break; } listeners[i].apply(this, [event].concat(args)); } } return this; }; // Helper for for finding special wildcard event handlers that match the event prototype.getWildcardCallbacks = function (eventName) { this.callbacks = this.callbacks || {}; var item, split, result = []; for (item in this.callbacks) { split = item.split('*'); if (item === '*' || (split.length === 2 && eventName.slice(0, split[0].length) === split[0])) { result = result.concat(this.callbacks[item]); } } return result; }; }; WildEmitter.mixin(WildEmitter);