mirror of
https://github.com/futurepress/epub.js.git
synced 2025-10-03 14:59:18 +02:00
Hooks execute in parallel
This commit is contained in:
parent
a7d5c6f77a
commit
67ff139dd4
4 changed files with 24 additions and 58 deletions
37
dist/epub.js
vendored
37
dist/epub.js
vendored
|
@ -2350,38 +2350,22 @@ EPUBJS.Hook.prototype.register = function(func){
|
||||||
|
|
||||||
// Triggers a hook to run all functions
|
// Triggers a hook to run all functions
|
||||||
EPUBJS.Hook.prototype.trigger = function(){
|
EPUBJS.Hook.prototype.trigger = function(){
|
||||||
var hooks = this.hooks;
|
|
||||||
var length = hooks.length;
|
|
||||||
var current = 0;
|
|
||||||
var executing;
|
|
||||||
var defer = new RSVP.defer();
|
|
||||||
var args = arguments;
|
var args = arguments;
|
||||||
var next = function(){
|
var context = this.context;
|
||||||
current += 1;
|
var promises = []
|
||||||
if(current < length) {
|
|
||||||
return hooks[current].apply(this.context, args);
|
|
||||||
}
|
|
||||||
}.bind(this);
|
|
||||||
if(length) {
|
|
||||||
|
|
||||||
executing = this.hooks[current].apply(this.context, args);
|
this.hooks.forEach(function(task, i) {
|
||||||
|
var executing = task.apply(context, args);
|
||||||
|
|
||||||
if(executing && typeof executing["then"] === "function") {
|
if(executing && typeof executing["then"] === "function") {
|
||||||
// Task is a function that returns a promise
|
// Task is a function that returns a promise
|
||||||
executing.then(next);
|
promises.push(executing);
|
||||||
} else {
|
|
||||||
// Task resolves immediately
|
|
||||||
next();
|
|
||||||
}
|
}
|
||||||
|
// Otherwise Task resolves immediately, continue
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
return RSVP.all(promises);
|
||||||
} else {
|
|
||||||
executing = defer.promise;
|
|
||||||
defer.resolve();
|
|
||||||
}
|
|
||||||
|
|
||||||
return executing;
|
|
||||||
};
|
};
|
||||||
EPUBJS.Parser = function(){};
|
EPUBJS.Parser = function(){};
|
||||||
|
|
||||||
|
@ -3598,7 +3582,7 @@ EPUBJS.Spine.prototype.remove = function(section) {
|
||||||
};
|
};
|
||||||
EPUBJS.replace = {};
|
EPUBJS.replace = {};
|
||||||
EPUBJS.replace.links = function(view, renderer) {
|
EPUBJS.replace.links = function(view, renderer) {
|
||||||
var task = new RSVP.defer();
|
|
||||||
var links = view.document.querySelectorAll("a[href]");
|
var links = view.document.querySelectorAll("a[href]");
|
||||||
var replaceLinks = function(link){
|
var replaceLinks = function(link){
|
||||||
var href = link.getAttribute("href");
|
var href = link.getAttribute("href");
|
||||||
|
@ -3637,8 +3621,7 @@ EPUBJS.replace.links = function(view, renderer) {
|
||||||
replaceLinks(links[i]);
|
replaceLinks(links[i]);
|
||||||
}
|
}
|
||||||
|
|
||||||
task.resolve();
|
|
||||||
return task.promise;
|
|
||||||
};
|
};
|
||||||
EPUBJS.Book = function(_url){
|
EPUBJS.Book = function(_url){
|
||||||
// Promises
|
// Promises
|
||||||
|
|
4
dist/epub.min.js
vendored
4
dist/epub.min.js
vendored
File diff suppressed because one or more lines are too long
|
@ -17,36 +17,20 @@ EPUBJS.Hook.prototype.register = function(func){
|
||||||
|
|
||||||
// Triggers a hook to run all functions
|
// Triggers a hook to run all functions
|
||||||
EPUBJS.Hook.prototype.trigger = function(){
|
EPUBJS.Hook.prototype.trigger = function(){
|
||||||
var hooks = this.hooks;
|
|
||||||
var length = hooks.length;
|
|
||||||
var current = 0;
|
|
||||||
var executing;
|
|
||||||
var defer = new RSVP.defer();
|
|
||||||
var args = arguments;
|
var args = arguments;
|
||||||
var next = function(){
|
var context = this.context;
|
||||||
current += 1;
|
var promises = []
|
||||||
if(current < length) {
|
|
||||||
return hooks[current].apply(this.context, args);
|
|
||||||
}
|
|
||||||
}.bind(this);
|
|
||||||
if(length) {
|
|
||||||
|
|
||||||
executing = this.hooks[current].apply(this.context, args);
|
this.hooks.forEach(function(task, i) {
|
||||||
|
var executing = task.apply(context, args);
|
||||||
|
|
||||||
if(executing && typeof executing["then"] === "function") {
|
if(executing && typeof executing["then"] === "function") {
|
||||||
// Task is a function that returns a promise
|
// Task is a function that returns a promise
|
||||||
executing.then(next);
|
promises.push(executing);
|
||||||
} else {
|
|
||||||
// Task resolves immediately
|
|
||||||
next();
|
|
||||||
}
|
}
|
||||||
|
// Otherwise Task resolves immediately, continue
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
return RSVP.all(promises);
|
||||||
} else {
|
|
||||||
executing = defer.promise;
|
|
||||||
defer.resolve();
|
|
||||||
}
|
|
||||||
|
|
||||||
return executing;
|
|
||||||
};
|
};
|
|
@ -1,6 +1,6 @@
|
||||||
EPUBJS.replace = {};
|
EPUBJS.replace = {};
|
||||||
EPUBJS.replace.links = function(view, renderer) {
|
EPUBJS.replace.links = function(view, renderer) {
|
||||||
var task = new RSVP.defer();
|
|
||||||
var links = view.document.querySelectorAll("a[href]");
|
var links = view.document.querySelectorAll("a[href]");
|
||||||
var replaceLinks = function(link){
|
var replaceLinks = function(link){
|
||||||
var href = link.getAttribute("href");
|
var href = link.getAttribute("href");
|
||||||
|
@ -39,6 +39,5 @@ EPUBJS.replace.links = function(view, renderer) {
|
||||||
replaceLinks(links[i]);
|
replaceLinks(links[i]);
|
||||||
}
|
}
|
||||||
|
|
||||||
task.resolve();
|
|
||||||
return task.promise;
|
|
||||||
};
|
};
|
Loading…
Add table
Add a link
Reference in a new issue