Adam Shaw пре 8 година
родитељ
комит
59ac92bede
2 измењених фајлова са 47 додато и 7 уклоњено
  1. 17 7
      src/common/TaskQueue.js
  2. 30 0
      tests/automated-better/util/TaskQueue.js

+ 17 - 7
src/common/TaskQueue.js

@@ -1,6 +1,7 @@
 
 function TaskQueue() {
 	var _this = this;
+	var isPaused = false;
 	var isRunning = 0;
 	var q = [];
 
@@ -8,16 +9,25 @@ function TaskQueue() {
 
 	this.queue = function(/* taskFunc, taskFunc... */) {
 		q.push.apply(q, arguments); // append
+		tryStart();
+	};
+
+	this.pause = function() {
+		isPaused = true;
+	};
+
+	this.resume = function() {
+		isPaused = false;
+		tryStart();
+	};
 
-		if (!isRunning) {
+	function tryStart() {
+		if (!isRunning && !isPaused && q.length) {
 			isRunning = true;
 			_this.trigger('start');
-
-			if (q.length) { // at least one new task added?
-				runNext();
-			}
+			runNext();
 		}
-	};
+	}
 
 	function runNext() { // does not check for empty q
 		var taskFunc = q.shift();
@@ -31,7 +41,7 @@ function TaskQueue() {
 		}
 
 		function done() {
-			if (q.length) {
+			if (!isPaused && q.length) {
 				runNext();
 			}
 			else {

+ 30 - 0
tests/automated-better/util/TaskQueue.js

@@ -109,4 +109,34 @@ describe('TaskQueue', function() {
 			done();
 		}, 200);
 	});
+
+	describe('pausing', function() {
+
+		it('prevents task from rendering', function() {
+			var q = new TaskQueue();
+			var ops = [];
+
+			q.on('start', function() {
+				ops.push('start-event');
+			});
+			q.on('stop', function() {
+				ops.push('stop-event');
+			});
+
+			q.pause();
+
+			q.queue(function() {
+				ops.push('run1');
+			});
+			q.queue(function() {
+				ops.push('run2');
+			});
+
+			expect(ops).toEqual([ ]);
+
+			q.resume();
+
+			expect(ops).toEqual([ 'start-event', 'run1', 'run2', 'stop-event' ]);
+		});
+	});
 });