mirror of
https://github.com/futurepress/epub.js.git
synced 2025-10-03 14:59:18 +02:00
Added render queue
This commit is contained in:
parent
b1ebc754cd
commit
ee19c03287
6 changed files with 282 additions and 65 deletions
118
dist/epub.js
vendored
118
dist/epub.js
vendored
|
@ -3081,6 +3081,7 @@ EPUBJS.core.indexOfSorted = function(item, array, compareFunction, _start, _end)
|
|||
}
|
||||
};
|
||||
|
||||
EPUBJS.core.requestAnimationFrame = window.requestAnimationFrame || window.mozRequestAnimationFrame || window.webkitRequestAnimationFrame || window.msRequestAnimationFrame;
|
||||
EPUBJS.EpubCFI = function(cfiStr){
|
||||
if(cfiStr) return this.parse(cfiStr);
|
||||
};
|
||||
|
@ -3630,7 +3631,7 @@ EPUBJS.Hook.prototype.trigger = function(){
|
|||
EPUBJS.Infinite = function(container, limit){
|
||||
this.container = container;
|
||||
this.windowHeight = window.innerHeight;
|
||||
this.tick = window.requestAnimationFrame || window.mozRequestAnimationFrame || window.webkitRequestAnimationFrame || window.msRequestAnimationFrame;
|
||||
this.tick = EPUBJS.core.requestAnimationFrame;
|
||||
this.scrolled = false;
|
||||
this.ignore = false;
|
||||
|
||||
|
@ -4619,6 +4620,100 @@ EPUBJS.Parser.prototype.ncx = function(tocXml){
|
|||
|
||||
return getTOC(navMap);
|
||||
};
|
||||
EPUBJS.Queue = function(_context){
|
||||
this._q = [];
|
||||
this.context = _context;
|
||||
this.tick = EPUBJS.core.requestAnimationFrame;
|
||||
this.running = false;
|
||||
};
|
||||
|
||||
// Add an item to the queue
|
||||
EPUBJS.Queue.prototype.enqueue = function(task, args, context) {
|
||||
// Handle single args without context
|
||||
if(args && !args.length) {
|
||||
args = [args];
|
||||
}
|
||||
|
||||
this._q.push({
|
||||
"task" : task,
|
||||
"args" : args,
|
||||
"context" : context
|
||||
});
|
||||
return this._q;
|
||||
};
|
||||
|
||||
// Run one item
|
||||
EPUBJS.Queue.prototype.dequeue = function(){
|
||||
var inwait, task;
|
||||
|
||||
if(this._q.length) {
|
||||
inwait = this._q.shift();
|
||||
task = inwait.task;
|
||||
|
||||
if(typeof task === "function"){
|
||||
// Task is a function that returns a promise
|
||||
return task.apply(inwait.context || this.context, inwait.args);
|
||||
} else {
|
||||
// Task is a promise
|
||||
return task;
|
||||
}
|
||||
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
};
|
||||
|
||||
// Run All Immediately
|
||||
EPUBJS.Queue.prototype.flush = function(){
|
||||
while(this._q.length) {
|
||||
this.dequeue();
|
||||
}
|
||||
};
|
||||
|
||||
// Run all sequentially, at convince
|
||||
EPUBJS.Queue.prototype.run = function(){
|
||||
if(!this.running && this._q.length) {
|
||||
this.running = true;
|
||||
this.dequeue().then(function(){
|
||||
this.running = false;
|
||||
}.bind(this));
|
||||
}
|
||||
|
||||
this.tick.call(window, this.run.bind(this));
|
||||
};
|
||||
|
||||
// Clear all items in wait
|
||||
EPUBJS.Queue.prototype.clear = function(){
|
||||
this._q = [];
|
||||
};
|
||||
|
||||
EPUBJS.Queue.prototype.length = function(){
|
||||
return this._q.length;
|
||||
};
|
||||
|
||||
// Create a new tast from a callback
|
||||
EPUBJS.Task = function(task, args, context){
|
||||
var toApply = args || [];
|
||||
var scope = context || this.context;
|
||||
|
||||
return function(){
|
||||
|
||||
return new RSVP.Promise(function(resolve, reject) {
|
||||
|
||||
var callback = function(value){
|
||||
resolve(value);
|
||||
};
|
||||
// Add the callback to the arguments list
|
||||
toApply.push(callback);
|
||||
|
||||
// Apply all arguments to the functions
|
||||
task.apply(scope, toApply);
|
||||
|
||||
});
|
||||
|
||||
};
|
||||
|
||||
};
|
||||
EPUBJS.Renderer = function(book, _options) {
|
||||
var options = _options || {};
|
||||
this.settings = {
|
||||
|
@ -5283,6 +5378,10 @@ EPUBJS.Rendition = function(book, options) {
|
|||
this.infinite.on("scroll", this.check.bind(this));
|
||||
}
|
||||
|
||||
this.q = new EPUBJS.Queue(this);
|
||||
|
||||
this.q.enqueue(this.book.opened);
|
||||
|
||||
};
|
||||
|
||||
/**
|
||||
|
@ -5368,6 +5467,9 @@ EPUBJS.Rendition.prototype.attachTo = function(_element){
|
|||
|
||||
// Trigger Attached
|
||||
|
||||
// Start processing queue
|
||||
this.q.run();
|
||||
|
||||
};
|
||||
|
||||
EPUBJS.Rendition.prototype.attachListeners = function(){
|
||||
|
@ -5386,13 +5488,21 @@ EPUBJS.Rendition.prototype.display = function(what){
|
|||
var displaying = new RSVP.defer();
|
||||
var displayed = displaying.promise;
|
||||
|
||||
|
||||
// Check for fragments
|
||||
if(typeof what === 'string') {
|
||||
what = what.split("#")[0];
|
||||
}
|
||||
|
||||
this.book.opened.then(function(){
|
||||
this.q.enqueue(this.move, what);
|
||||
|
||||
|
||||
return displayed;
|
||||
};
|
||||
|
||||
EPUBJS.Rendition.prototype.move = function(what){
|
||||
var displaying = new RSVP.defer();
|
||||
var displayed = displaying.promise;
|
||||
|
||||
var section = this.book.spine.get(what);
|
||||
var view;
|
||||
|
||||
|
@ -5422,8 +5532,6 @@ EPUBJS.Rendition.prototype.display = function(what){
|
|||
displaying.reject(new Error("No Section Found"));
|
||||
}
|
||||
|
||||
}.bind(this));
|
||||
|
||||
return displayed;
|
||||
};
|
||||
|
||||
|
|
6
dist/epub.min.js
vendored
6
dist/epub.min.js
vendored
File diff suppressed because one or more lines are too long
|
@ -423,3 +423,5 @@ EPUBJS.core.indexOfSorted = function(item, array, compareFunction, _start, _end)
|
|||
return EPUBJS.core.indexOfSorted(item, array, compareFunction, start, pivot);
|
||||
}
|
||||
};
|
||||
|
||||
EPUBJS.core.requestAnimationFrame = window.requestAnimationFrame || window.mozRequestAnimationFrame || window.webkitRequestAnimationFrame || window.msRequestAnimationFrame;
|
|
@ -1,7 +1,7 @@
|
|||
EPUBJS.Infinite = function(container, limit){
|
||||
this.container = container;
|
||||
this.windowHeight = window.innerHeight;
|
||||
this.tick = window.requestAnimationFrame || window.mozRequestAnimationFrame || window.webkitRequestAnimationFrame || window.msRequestAnimationFrame;
|
||||
this.tick = EPUBJS.core.requestAnimationFrame;
|
||||
this.scrolled = false;
|
||||
this.ignore = false;
|
||||
|
||||
|
|
94
lib/epubjs/queue.js
Normal file
94
lib/epubjs/queue.js
Normal file
|
@ -0,0 +1,94 @@
|
|||
EPUBJS.Queue = function(_context){
|
||||
this._q = [];
|
||||
this.context = _context;
|
||||
this.tick = EPUBJS.core.requestAnimationFrame;
|
||||
this.running = false;
|
||||
};
|
||||
|
||||
// Add an item to the queue
|
||||
EPUBJS.Queue.prototype.enqueue = function(task, args, context) {
|
||||
// Handle single args without context
|
||||
if(args && !args.length) {
|
||||
args = [args];
|
||||
}
|
||||
|
||||
this._q.push({
|
||||
"task" : task,
|
||||
"args" : args,
|
||||
"context" : context
|
||||
});
|
||||
return this._q;
|
||||
};
|
||||
|
||||
// Run one item
|
||||
EPUBJS.Queue.prototype.dequeue = function(){
|
||||
var inwait, task;
|
||||
|
||||
if(this._q.length) {
|
||||
inwait = this._q.shift();
|
||||
task = inwait.task;
|
||||
|
||||
if(typeof task === "function"){
|
||||
// Task is a function that returns a promise
|
||||
return task.apply(inwait.context || this.context, inwait.args);
|
||||
} else {
|
||||
// Task is a promise
|
||||
return task;
|
||||
}
|
||||
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
};
|
||||
|
||||
// Run All Immediately
|
||||
EPUBJS.Queue.prototype.flush = function(){
|
||||
while(this._q.length) {
|
||||
this.dequeue();
|
||||
}
|
||||
};
|
||||
|
||||
// Run all sequentially, at convince
|
||||
EPUBJS.Queue.prototype.run = function(){
|
||||
if(!this.running && this._q.length) {
|
||||
this.running = true;
|
||||
this.dequeue().then(function(){
|
||||
this.running = false;
|
||||
}.bind(this));
|
||||
}
|
||||
|
||||
this.tick.call(window, this.run.bind(this));
|
||||
};
|
||||
|
||||
// Clear all items in wait
|
||||
EPUBJS.Queue.prototype.clear = function(){
|
||||
this._q = [];
|
||||
};
|
||||
|
||||
EPUBJS.Queue.prototype.length = function(){
|
||||
return this._q.length;
|
||||
};
|
||||
|
||||
// Create a new tast from a callback
|
||||
EPUBJS.Task = function(task, args, context){
|
||||
var toApply = args || [];
|
||||
var scope = context || this.context;
|
||||
|
||||
return function(){
|
||||
|
||||
return new RSVP.Promise(function(resolve, reject) {
|
||||
|
||||
var callback = function(value){
|
||||
resolve(value);
|
||||
};
|
||||
// Add the callback to the arguments list
|
||||
toApply.push(callback);
|
||||
|
||||
// Apply all arguments to the functions
|
||||
task.apply(scope, toApply);
|
||||
|
||||
});
|
||||
|
||||
};
|
||||
|
||||
};
|
|
@ -36,6 +36,10 @@ EPUBJS.Rendition = function(book, options) {
|
|||
this.infinite.on("scroll", this.check.bind(this));
|
||||
}
|
||||
|
||||
this.q = new EPUBJS.Queue(this);
|
||||
|
||||
this.q.enqueue(this.book.opened);
|
||||
|
||||
};
|
||||
|
||||
/**
|
||||
|
@ -121,6 +125,9 @@ EPUBJS.Rendition.prototype.attachTo = function(_element){
|
|||
|
||||
// Trigger Attached
|
||||
|
||||
// Start processing queue
|
||||
this.q.run();
|
||||
|
||||
};
|
||||
|
||||
EPUBJS.Rendition.prototype.attachListeners = function(){
|
||||
|
@ -139,13 +146,21 @@ EPUBJS.Rendition.prototype.display = function(what){
|
|||
var displaying = new RSVP.defer();
|
||||
var displayed = displaying.promise;
|
||||
|
||||
|
||||
// Check for fragments
|
||||
if(typeof what === 'string') {
|
||||
what = what.split("#")[0];
|
||||
}
|
||||
|
||||
this.book.opened.then(function(){
|
||||
this.q.enqueue(this.move, what);
|
||||
|
||||
|
||||
return displayed;
|
||||
};
|
||||
|
||||
EPUBJS.Rendition.prototype.move = function(what){
|
||||
var displaying = new RSVP.defer();
|
||||
var displayed = displaying.promise;
|
||||
|
||||
var section = this.book.spine.get(what);
|
||||
var view;
|
||||
|
||||
|
@ -175,8 +190,6 @@ EPUBJS.Rendition.prototype.display = function(what){
|
|||
displaying.reject(new Error("No Section Found"));
|
||||
}
|
||||
|
||||
}.bind(this));
|
||||
|
||||
return displayed;
|
||||
};
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue