Ver Fonte

make taskqueue iterative. smaller stacks

Adam Shaw há 8 anos atrás
pai
commit
5c9be017dd
2 ficheiros alterados com 24 adições e 28 exclusões
  1. 1 1
      src/common/RenderQueue.js
  2. 23 27
      src/common/TaskQueue.js

+ 1 - 1
src/common/RenderQueue.js

@@ -95,7 +95,7 @@ var RenderQueue = TaskQueue.extend({
 
 
 	runTask: function(task) {
-		this.runTaskFunc(task.func);
+		return task.func();
 	},
 
 

+ 23 - 27
src/common/TaskQueue.js

@@ -32,7 +32,7 @@ var TaskQueue = Class.extend(EmitterMixin, {
 		if (!this.isRunning && this.canRunNext()) {
 			this.isRunning = true;
 			this.trigger('start');
-			this.runNext();
+			this.runRemaining();
 		}
 	},
 
@@ -42,36 +42,32 @@ var TaskQueue = Class.extend(EmitterMixin, {
 	},
 
 
-	runNext: function() { // does not check canRunNext
-		this.runTask(this.q.shift());
-	},
-
+	runRemaining: function() { // assumes at least one task in queue. does not check canRunNext for first task.
+		var _this = this;
+		var task;
+		var res;
+
+		do {
+			task = this.q.shift(); // always freshly reference q. might have been reassigned.
+			res = this.runTask(task);
+
+			if (res && res.then) {
+				res.then(function() {
+					if (_this.canRunNext()) {
+						_this.runRemaining();
+					}
+				});
+				return; // prevent marking as stopped
+			}
+		} while (this.canRunNext());
 
-	runTask: function(task) {
-		this.runTaskFunc(task);
+		this.isRunning = false;
+		this.trigger('stop'); // not really a 'stop' ... more of a 'drained'
 	},
 
 
-	runTaskFunc: function(taskFunc) {
-		var _this = this;
-		var res = taskFunc();
-
-		if (res && res.then) {
-			res.then(done);
-		}
-		else {
-			done();
-		}
-
-		function done() {
-			if (_this.canRunNext()) {
-				_this.runNext();
-			}
-			else {
-				_this.isRunning = false;
-				_this.trigger('stop');
-			}
-		}
+	runTask: function(task) {
+		return task(); // task *is* the function, but subclasses can change the format of a task
 	}
 
 });