Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added a #either method

  • Loading branch information...
commit 26297762b1ca96073d6ff2b3de14e7dbae0b8a62 1 parent 1521291
@3rd-Eden 3rd-Eden authored
Showing with 65 additions and 3 deletions.
  1. +31 −2 lib/index.js
  2. +1 −1  package.json
  3. +33 −0 tests/eventreactor.test.js
View
33 lib/index.js
@@ -35,6 +35,36 @@
};
/**
+ * This function should be called once by either one of these given events.
+ *
+ * @api public
+ */
+
+ EventEmitter.prototype.either = function either () {
+ var args = slice.call(arguments, 0)
+ , fn = args.pop()
+ , i = args.length;
+
+ function callback () {
+ fn.apply(this, arguments);
+
+ // remove all the assigned event listeners again
+ i = args.length;
+ while (i--) {
+ this.removeListener(args[i], callback);
+ }
+ }
+
+ // assign the event listeners
+ i = args.length;
+ while (i--) {
+ this.on(args[i], callback);
+ }
+
+ return this;
+ };
+
+ /**
* Small utility to use a object to assign EventEmitters.
*
* @param {Object} events
@@ -231,7 +261,6 @@
* @api public
*/
- exports.version = '0.0.5';
+ exports.version = '0.0.6';
}
-
})();
View
2  package.json
@@ -1,7 +1,7 @@
{
"name": "eventreactor"
, "description": "EventEmitters on a syntax suger rush"
- , "version": "0.0.5"
+ , "version": "0.0.6"
, "author": "Arnout Kazemier <opensource@observe.it>"
, "homepage": "http://observe.it"
, "keywords": ["event", "eventemitter", "observer"]
View
33 tests/eventreactor.test.js
@@ -77,6 +77,39 @@ describe('EventReactor', function () {
})
})
+ describe('#either', function () {
+ it('is added to the EventEmitter.prototype', function () {
+ EventEmitter.prototype.either.should.be.a('function')
+ })
+
+ it('is chainable', function () {
+ var EE = new EventEmitter
+
+ EE.either().should.equal(EE)
+ })
+
+ it('emit the event once, and it listens to every applied event', function () {
+ var EE = new EventEmitter
+ , count = 0
+
+ function listen () {
+ ++count;
+ }
+
+ EE.either('foo', 'bar', 'baz', listen)
+ EE.emit('foo')
+
+ count.should.equal(1)
+ EE.emit('bar')
+ count.should.equal(1)
+
+ EE.either('foo', 'bar', 'baz', listen)
+ EE.emit('bar')
+
+ count.should.equal(2)
+ })
+ })
+
describe('#multiple', function () {
it('is added to the EventEmitter.prototype', function () {
EventEmitter.prototype.multiple.should.be.a('function')
Please sign in to comment.
Something went wrong with that request. Please try again.