Permalink
Browse files

modified reset() not to clean the queue

queue will be cleaned after every start()
new single onStart() callback calls after every new start
  • Loading branch information...
1 parent 89420f6 commit 8a3c8730530f92e2d82ab0cfeaa002d2163e7d22 @arunoda committed Sep 17, 2011
Showing with 46 additions and 2 deletions.
  1. +23 −0 lib/qbox.js
  2. +1 −1 package.json
  3. +22 −1 tests/qbox.js
View
@@ -1,8 +1,12 @@
+var util = require('util');
+var EventEmitter = require('events').EventEmitter;
+
function QBox(steps) {
var isReady = false;
var queue = [];
var isStop = false;
+ var startingCallback;
var ticks;
@@ -16,14 +20,28 @@ function QBox(steps) {
}
};
+ /**
+ Start the QBox and execute all the callbacks in the queue
+ and clear the queue
+ */
this.start = function() {
if(!isStop) {
isReady = true;
queue.forEach(function(callback) {
callback();
});
+ queue = [];
+ if(startingCallback) startingCallback();
}
};
+
+ /**
+ Add a single callback to call each and every time
+ Qbox starting (mostely used with reset())
+ */
+ this.onStart = function(callback) {
+ startingCallback = callback;
+ };
this.tick = function(step) {
@@ -64,10 +82,15 @@ function QBox(steps) {
isStop = true;
};
+ /**
+ Reset the QBOX and make is available to start again
+ And does not clean callbacks in the queue and onStart Callback
+ */
this.reset = function() {
isReady = false;
isStop = false;
addTicks();
+ queue = [];
};
function addTicks() {
View
@@ -1,6 +1,6 @@
{
"name": "qbox",
- "version": "0.1.4",
+ "version": "0.1.5",
"directories": {
"lib": "./lib"
},
View
@@ -1,4 +1,4 @@
-var qbox = require("../lib/qbox");
+var qbox = require("qbox");
exports.testSteps = function(test) {
@@ -124,6 +124,27 @@ exports.testResetAfterStop = function(test) {
$.tick(2);
$.tick(1);
$.reset();
+ $.ready(function() {
+ test.ok(true);
+ });
+ $.tick(1);
+ $.tick(2);
+
+ test.done();
+};
+
+exports.testOnStart = function(test) {
+
+ test.expect(2);
+ var $ = qbox.create([1,2]);
+
+ $.onStart(function() {
+ test.ok(true);
+ });
+
+ $.tick(2);
+ $.tick(1);
+ $.reset();
$.tick(1);
$.tick(2);

0 comments on commit 8a3c873

Please sign in to comment.