mirror of
https://github.com/futurepress/epub.js.git
synced 2025-10-03 14:59:18 +02:00
updated how tasks are added to queue
This commit is contained in:
parent
ee19c03287
commit
338bc64884
5 changed files with 1540 additions and 2092 deletions
|
@ -7,17 +7,39 @@ EPUBJS.Queue = function(_context){
|
|||
|
||||
// Add an item to the queue
|
||||
EPUBJS.Queue.prototype.enqueue = function(task, args, context) {
|
||||
var deferred, promise;
|
||||
var queued;
|
||||
|
||||
// Handle single args without context
|
||||
if(args && !args.length) {
|
||||
args = [args];
|
||||
}
|
||||
|
||||
if(typeof task === "function"){
|
||||
|
||||
deferred = new RSVP.defer();
|
||||
promise = deferred.promise;
|
||||
|
||||
queued = {
|
||||
"task" : task,
|
||||
"args" : args,
|
||||
"context" : context,
|
||||
"deferred" : deferred,
|
||||
"promise" : promise
|
||||
};
|
||||
|
||||
} else {
|
||||
// Task is a promise
|
||||
queued = {
|
||||
"promise" : task
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
this._q.push(queued);
|
||||
|
||||
this._q.push({
|
||||
"task" : task,
|
||||
"args" : args,
|
||||
"context" : context
|
||||
});
|
||||
return this._q;
|
||||
|
||||
return queued.promise;
|
||||
};
|
||||
|
||||
// Run one item
|
||||
|
@ -28,12 +50,15 @@ EPUBJS.Queue.prototype.dequeue = function(){
|
|||
inwait = this._q.shift();
|
||||
task = inwait.task;
|
||||
|
||||
if(typeof task === "function"){
|
||||
if(task){
|
||||
// Task is a function that returns a promise
|
||||
return task.apply(inwait.context || this.context, inwait.args);
|
||||
return task.apply(inwait.context || this.context, inwait.args).then(function(){
|
||||
inwait.deferred.resolve.apply(inwait.context || this.context, arguments);
|
||||
}.bind(this));
|
||||
|
||||
} else {
|
||||
// Task is a promise
|
||||
return task;
|
||||
return inwait.promise;
|
||||
}
|
||||
|
||||
} else {
|
||||
|
@ -71,13 +96,11 @@ EPUBJS.Queue.prototype.length = function(){
|
|||
|
||||
// Create a new tast from a callback
|
||||
EPUBJS.Task = function(task, args, context){
|
||||
var toApply = args || [];
|
||||
var scope = context || this.context;
|
||||
|
||||
return function(){
|
||||
|
||||
var toApply = arguments || [];
|
||||
|
||||
return new RSVP.Promise(function(resolve, reject) {
|
||||
|
||||
var callback = function(value){
|
||||
resolve(value);
|
||||
};
|
||||
|
@ -85,9 +108,9 @@ EPUBJS.Task = function(task, args, context){
|
|||
toApply.push(callback);
|
||||
|
||||
// Apply all arguments to the functions
|
||||
task.apply(scope, toApply);
|
||||
task.apply(this, toApply);
|
||||
|
||||
});
|
||||
}.bind(this));
|
||||
|
||||
};
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue