Permalink
Browse files

Add functional counterparts to WaitForAll and WaitForAny.

  • Loading branch information...
1 parent ee86b37 commit c6c4e9b0940a23bf11cc26d407e0237a6182a84f @MathieuTurcotte committed May 2, 2012
Showing with 105 additions and 3 deletions.
  1. +24 −0 examples/all-functionnal.js
  2. +24 −0 examples/any-functionnal.js
  3. +9 −2 index.js
  4. +3 −1 lib/multiple.js
  5. +45 −0 lib/waitfn.js
@@ -0,0 +1,24 @@
+#!/usr/bin/env node
+
+var ewait = require('../index'),
+ Delay = require('./delay');
+
+var delays = [
+ new Delay(200),
+ new Delay(400),
+ new Delay(1400)
+];
+
+ewait.waitForAll(delays, function(err) {
+ if (err != null) {
+ console.log('Timeout!');
+ } else {
+ console.log('Done!');
+ }
+}, 2000, 'done');
+
+delays.forEach(function(delay) {
+ delay.start();
+});
+
+console.log('Waiting...');
@@ -0,0 +1,24 @@
+#!/usr/bin/env node
+
+var ewait = require('../index'),
+ Delay = require('./delay');
+
+var delays = [
+ new Delay(200),
+ new Delay(400),
+ new Delay(1400)
+];
+
+ewait.waitForAny(delays, function(err) {
+ if (err != null) {
+ console.log('Timeout!');
+ } else {
+ console.log('Done!');
+ }
+}, 'done', 300);
+
+delays.forEach(function(delay) {
+ delay.start();
+});
+
+console.log('Waiting...');
View
@@ -3,7 +3,14 @@
* Licensed under the MIT license.
*/
+var waitFn = require('./lib/waitfn'),
+ WaitForAny = require('./lib/any'),
+ WaitForAll = require('./lib/all');
+
module.exports = {
- WaitForAny: require('./lib/any'),
- WaitForAll: require('./lib/all')
+ WaitForAny: WaitForAny,
+ WaitForAll: WaitForAll,
+ waitForAny: waitFn(WaitForAny),
+ waitForAll: waitFn(WaitForAll)
};
+
View
@@ -14,7 +14,7 @@ function WaitForMultiple(options) {
this.subjects = [];
- this.eventName = options.event || 'done';
+ this.eventName = options.event || WaitForMultiple.EVENT_NAME;
this.timeoutDelay = options.timeout;
this.timeoutID = -1;
@@ -26,6 +26,8 @@ function WaitForMultiple(options) {
}
util.inherits(WaitForMultiple, events.EventEmitter);
+WaitForMultiple.EVENT_NAME = 'done';
+
WaitForMultiple.prototype.add = function(arg) {
if (Array.isArray(arg)) {
this.subjects = this.subjects.concat(arg);
View
@@ -0,0 +1,45 @@
+/*
+ * Copyright (c) 2012 Mathieu Turcotte
+ * Licensed under the MIT license.
+ */
+
+var WaitForMultiple = require('./multiple');
+
+function makeWaitFunction(Waiter) {
+ return function(emitters, callback) {
+ var extraArgs = Array.prototype.slice.call(arguments);
+ var parsedArgs = parseTimeoutAndEventName(extraArgs);
+
+ var waiter = new Waiter(parsedArgs);
+
+ waiter.once('done', function() {
+ callback(null);
+ });
+
+ waiter.once('timeout', function() {
+ callback(new Error('Timeout.'));
+ });
+
+ waiter.add(emitters);
+ waiter.wait();
+ };
+};
+
+function parseTimeoutAndEventName(args) {
+ var parsedArgs = {
+ timeout: undefined,
+ event: WaitForMultiple.EVENT_NAME
+ };
+
+ args.slice(2, 4).forEach(function(arg) {
+ if (typeof arg == "string") {
+ parsedArgs.event = arg;
+ } else if (typeof arg == "number") {
+ parsedArgs.timeout = arg;
+ }
+ });
+
+ return parsedArgs;
+};
+
+module.exports = makeWaitFunction;

0 comments on commit c6c4e9b

Please sign in to comment.