I am trying to implement a simple queue for functions that take callbacks. My problem is that in the flush method, "this" is null when the action being performed is some long-running thing (in my case, indexedDB calls). I've never experienced this sort of behavior before, so please educate me on what is going on?
Here's the code:
var Queue = (function () {
function Queue() {
};
Queue.prototype.items = [];
Queue.prototype.results = [];
Queue.prototype.add = function (action) {
this.items.push(action);
};
Queue.prototype.complete = function () { };
Queue.prototype.flush = function () {
var args = Array.prototype.slice.call(arguments);
if (args.length > 0) { this.results.push(args); }
if (this.items.length > 0) {
var action = this.items.shift();
action.call(this);
} else { // Complete, call back multi.
var results = this.results;
this.clear();
this.complete(results);
}
};
Queue.prototype.clear = function () {
this.items = [];
this.results = [];
};
Queue.create = function () {
return new Queue;
};
return Queue;
})();