Permalink
Browse files

QBox added with steps and timeout supports

  • Loading branch information...
arunoda committed Apr 4, 2011
0 parents commit f1b459d1cc1ce05916af6a7b309f5f2fa0823ef1
Showing with 129 additions and 0 deletions.
  1. +57 −0 lib/qbox.js
  2. +23 −0 package.json
  3. +49 −0 tests/qbox.js
@@ -0,0 +1,57 @@
+function QBox(steps) {
+
+ var isReady = false;
+ var queue = [];
+
+ this.ready = function(callback) {
+ if(isReady) {
+ callback();
+ } else {
+ queue.push(callback);
+ }
+ };
+
+ this.start = function() {
+ isReady = true;
+ queue.forEach(function(callback) {
+ callback();
+ });
+ };
+
+ this.tick = function(step) {
+
+ if(steps && steps instanceof Array) {
+
+ var index = steps.indexOf(step);
+ if(index >= 0) {
+
+ steps = [].concat(steps.slice(0, index), steps.slice(index+1));
+ if(steps.length == 0) {
+ this.start();
+ }
+ } else {
+
+ throw new Error("Invalid step: '" + step + "' provided");
+ }
+ } else {
+ throw new Error("Cannot tick - no steps are provided");
+ }
+ };
+
+ /**
+ * @param amount - no of millies fot timeout
+ * callback - function([]){} containing remaining steps
+ */
+ this.timeout = function(amount, callback) {
+
+ setTimeout(function() {
+ if(!isReady) {
+ callback(steps);
+ }
+ }, amount);
+ };
+};
+
+exports.create = function(steps) {
+ return new QBox(steps);
+};
@@ -0,0 +1,23 @@
+{
+ "name": "qbox",
+ "version": "0.1.1beta",
+ "directories": {
+ "lib": "./lib"
+ },
+ "main": "./lib/qbox",
+ "description": "JQuery like queue solution for NodeJs",
+ "author": "Arunoda Susiripala <arunoda.susiripala@gmail.com>",
+ "homepage": "https://github.com/arunoda/qbox",
+ "licenses": [
+ {
+ "type": "The MIT License",
+ "url": "http://www.opensource.org/licenses/mit-license.php"
+ }
+ ],
+
+ "repository" :
+ {
+ "type" : "git",
+ "url" : "git@github.com:arunoda/qbox.git"
+ }
+}
@@ -0,0 +1,49 @@
+var qbox = require("../lib/qbox");
+
+exports.testSteps = function(test) {
+
+ test.expect(1);
+ var $ = qbox.create(["db1", "db2"]);
+ $.ready(function() {
+ test.ok(true);
+ });
+
+ $.tick("db1");
+ $.tick("db2");
+ test.done();
+};
+
+exports.testStepsInvalid = function(test) {
+
+ var $ = qbox.create(["db1", "db2"]);
+ $.ready(function() {
+ test.fail();
+ });
+
+ $.tick("db1");
+ test.throws(function() {
+ $.tick("db222");
+ });
+ test.done();
+};
+
+exports.testTimeout = function(test) {
+
+ test.expect(1);
+ var $ = qbox.create(["db1", "db2"]);
+ $.ready(function() {
+ test.fail();
+ });
+
+ $.tick("db1");
+ var start = new Date().getTime();
+ $.timeout(200, function() {
+ var end = new Date().getTime();
+ if(end-start >= 200) {
+ test.ok(true);
+ test.done();
+ } else {
+ test.fail("Timeout time smaller");
+ }
+ });
+};

0 comments on commit f1b459d

Please sign in to comment.