Permalink
Browse files

Init.

  • Loading branch information...
0 parents commit 54dc31d184456872f3039c58e6cf7ea34c17bb9c @MathieuTurcotte committed Mar 29, 2012
Showing with 620 additions and 0 deletions.
  1. +1 −0 .gitignore
  2. +9 −0 .travis.yml
  3. +19 −0 LICENSE.txt
  4. +34 −0 examples/all-timeout.js
  5. +34 −0 examples/all.js
  6. +34 −0 examples/any-timeout.js
  7. +34 −0 examples/any.js
  8. +19 −0 examples/delay.js
  9. +9 −0 index.js
  10. +25 −0 lib/all.js
  11. +22 −0 lib/any.js
  12. +67 −0 lib/multiple.js
  13. +18 −0 package.json
  14. +100 −0 tests/all.js
  15. +90 −0 tests/any.js
  16. +105 −0 tests/multiple.js
@@ -0,0 +1 @@
+node_modules/
@@ -0,0 +1,9 @@
+language: node_js
+node_js:
+ - 0.6
+ - 0.7
+
+notifications:
+ email:
+ - turcotte.mat@gmail.com
+
@@ -0,0 +1,19 @@
+Copyright (C) 2012 Mathieu Turcotte
+
+Permission is hereby granted, free of charge, to any person obtaining a copy of
+this software and associated documentation files (the "Software"), to deal in
+the Software without restriction, including without limitation the rights to
+use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
+of the Software, and to permit persons to whom the Software is furnished to do
+so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all
+copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+SOFTWARE.
@@ -0,0 +1,34 @@
+#!/usr/bin/env node
+
+var WaitForAll = require('../index').WaitForAll,
+ Delay = require('./delay');
+
+var delays = [
+ new Delay(200),
+ new Delay(400),
+ new Delay(1400)
+];
+
+// Only two delays will fire before timeout.
+var all = new WaitForAll({
+ timeout: 800,
+ event: 'done'
+});
+
+all.add(delays);
+
+all.once('done', function() {
+ console.log('Done!');
+});
+
+all.once('timeout', function() {
+ console.log('Timeout!');
+});
+
+all.wait();
+
+delays.forEach(function(delay) {
+ delay.start();
+});
+
+console.log('Waiting...');
@@ -0,0 +1,34 @@
+#!/usr/bin/env node
+
+var WaitForAll = require('../index').WaitForAll,
+ Delay = require('./delay');
+
+var delays = [
+ new Delay(200),
+ new Delay(400),
+ new Delay(1400)
+];
+
+// All delays will fire before timeout.
+var all = new WaitForAll({
+ timeout: 2000,
+ event: 'done'
+});
+
+all.add(delays);
+
+all.once('done', function() {
+ console.log('Done!');
+});
+
+all.once('timeout', function() {
+ console.log('Timeout!');
+});
+
+all.wait();
+
+delays.forEach(function(delay) {
+ delay.start();
+});
+
+console.log('Waiting...');
@@ -0,0 +1,34 @@
+#!/usr/bin/env node
+
+var WaitForAny = require('../index').WaitForAny,
+ Delay = require('./delay');
+
+var delays = [
+ new Delay(200),
+ new Delay(400),
+ new Delay(1400)
+];
+
+// No delay will fire before timeout.
+var any = new WaitForAny({
+ timeout: 100,
+ event: 'done'
+});
+
+any.add(delays);
+
+any.once('done', function() {
+ console.log('Done!');
+});
+
+any.once('timeout', function() {
+ console.log('Timeout!');
+});
+
+any.wait();
+
+delays.forEach(function(delay) {
+ delay.start();
+});
+
+console.log('Waiting...');
@@ -0,0 +1,34 @@
+#!/usr/bin/env node
+
+var WaitForAny = require('../index').WaitForAny,
+ Delay = require('./delay');
+
+var delays = [
+ new Delay(200),
+ new Delay(400),
+ new Delay(1400)
+];
+
+// First delay will fire before timeout.
+var any = new WaitForAny({
+ timeout: 300,
+ event: 'done'
+});
+
+any.add(delays);
+
+any.once('done', function() {
+ console.log('Done!');
+});
+
+any.once('timeout', function() {
+ console.log('Timeout!');
+});
+
+any.wait();
+
+delays.forEach(function(delay) {
+ delay.start();
+});
+
+console.log('Waiting...');
@@ -0,0 +1,19 @@
+var util = require('util'),
+ events = require('events');
+
+function Delay(delay) {
+ events.EventEmitter.call(this);
+ this.delay = delay;
+ this.onDone = this.handleDone.bind(this);
+}
+util.inherits(Delay, events.EventEmitter);
+
+Delay.prototype.start = function() {
+ setTimeout(this.onDone, this.delay);
+};
+
+Delay.prototype.handleDone = function() {
+ this.emit('done');
+};
+
+module.exports = Delay;
@@ -0,0 +1,9 @@
+/*
+ * Copyright (c) 2012 Mathieu Turcotte
+ * Licensed under the MIT license.
+ */
+
+module.exports = {
+ WaitForAny: require('./lib/any'),
+ WaitForAll: require('./lib/all')
+};
@@ -0,0 +1,25 @@
+/*
+ * Copyright (c) 2012 Mathieu Turcotte
+ * Licensed under the MIT license.
+ */
+
+var util = require('util'),
+ events = require('events');
+
+var WaitForMultiple = require('./multiple');
+
+function WaitForAll(options) {
+ WaitForMultiple.call(this, options);
+}
+util.inherits(WaitForAll, WaitForMultiple);
+
+WaitForAll.prototype.handleDone = function() {
+ this.subjects.pop();
+
+ if (this.subjects.length == 0) {
+ this.emit('done');
+ this.clear();
+ }
+};
+
+module.exports = WaitForAll;
@@ -0,0 +1,22 @@
+/*
+ * Copyright (c) 2012 Mathieu Turcotte
+ * Licensed under the MIT license.
+ */
+
+var util = require('util'),
+ events = require('events');
+
+var WaitForMultiple = require('./multiple');
+
+function WaitForAny(options) {
+ WaitForMultiple.call(this, options);
+}
+util.inherits(WaitForAny, WaitForMultiple);
+
+WaitForAny.prototype.handleDone = function() {
+ this.stopListening();
+ this.emit('done');
+ this.clear();
+};
+
+module.exports = WaitForAny;
@@ -0,0 +1,67 @@
+/*
+ * Copyright (c) 2012 Mathieu Turcotte
+ * Licensed under the MIT license.
+ */
+
+var util = require('util'),
+ events = require('events');
+
+function WaitForMultiple(options) {
+ events.EventEmitter.call(this);
+ var options = options || {};
+
+ this.subjects = [];
+
+ this.eventName = options.event || 'done';
+
+ this.timeoutDelay = options.timeout;
+ this.timeoutID = -1;
+
+ this.handlers = {
+ done: this.handleDone.bind(this),
+ timeout: this.handleTimeout.bind(this)
+ };
+}
+util.inherits(WaitForMultiple, events.EventEmitter);
+
+WaitForMultiple.prototype.add = function(arg) {
+ if (Array.isArray(arg)) {
+ this.subjects = this.subjects.concat(arg);
+ } else {
+ this.subjects.push(arg);
+ }
+};
+
+WaitForMultiple.prototype.wait = function() {
+ this.subjects.forEach(function(subject) {
+ subject.once(this.eventName, this.handlers.done);
+ }, this);
+
+ if (this.timeoutDelay) {
+ this.timeoutID = setTimeout(this.handlers.timeout, this.timeoutDelay);
+ }
+};
+
+WaitForMultiple.prototype.stopListening = function() {
+ this.subjects.forEach(function(subject) {
+ subject.removeListener(this.eventName, this.handlers.done);
+ }, this);
+};
+
+WaitForMultiple.prototype.clear = function() {
+ clearTimeout(this.timeoutID);
+ this.removeAllListeners();
+ this.subjects = [];
+};
+
+WaitForMultiple.prototype.handleTimeout = function() {
+ this.stopListening();
+ this.emit('timeout');
+ this.clear();
+};
+
+WaitForMultiple.prototype.handleDone = function() {
+ throw new Error('Not implemented!');
+};
+
+module.exports = WaitForMultiple;
@@ -0,0 +1,18 @@
+{
+ "name": "ewait",
+ "description": "WaitForAll and WaitForAny on EventEmitter.",
+ "version": "0.1.0",
+ "author": "Mathieu Turcotte <turcotte.mat@gmail.com>",
+ "keywords": ["wait", "events", "async"],
+ "devDependencies": {
+ "nodeunit": "0.7",
+ "sinon": "1.3"
+ },
+ "repository": {
+ "type": "git",
+ "url": "https://github.com/MathieuTurcotte/node-wait.git"
+ },
+ "scripts": {
+ "test": "node_modules/nodeunit/bin/nodeunit tests/"
+ }
+}
Oops, something went wrong.

0 comments on commit 54dc31d

Please sign in to comment.