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
|
||||
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 next = function(){
|
||||
current += 1;
|
||||
if(current < length) {
|
||||
return hooks[current].apply(this.context, args);
|
||||
}
|
||||
}.bind(this);
|
||||
if(length) {
|
||||
var context = this.context;
|
||||
var promises = []
|
||||
|
||||
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") {
|
||||
// Task is a function that returns a promise
|
||||
executing.then(next);
|
||||
} else {
|
||||
// Task resolves immediately
|
||||
next();
|
||||
promises.push(executing);
|
||||
}
|
||||
// Otherwise Task resolves immediately, continue
|
||||
});
|
||||
|
||||
|
||||
|
||||
} else {
|
||||
executing = defer.promise;
|
||||
defer.resolve();
|
||||
}
|
||||
|
||||
return executing;
|
||||
return RSVP.all(promises);
|
||||
};
|
||||
EPUBJS.Parser = function(){};
|
||||
|
||||
|
@ -3598,7 +3582,7 @@ EPUBJS.Spine.prototype.remove = function(section) {
|
|||
};
|
||||
EPUBJS.replace = {};
|
||||
EPUBJS.replace.links = function(view, renderer) {
|
||||
var task = new RSVP.defer();
|
||||
|
||||
var links = view.document.querySelectorAll("a[href]");
|
||||
var replaceLinks = function(link){
|
||||
var href = link.getAttribute("href");
|
||||
|
@ -3637,8 +3621,7 @@ EPUBJS.replace.links = function(view, renderer) {
|
|||
replaceLinks(links[i]);
|
||||
}
|
||||
|
||||
task.resolve();
|
||||
return task.promise;
|
||||
|
||||
};
|
||||
EPUBJS.Book = function(_url){
|
||||
// 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
|
||||
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 next = function(){
|
||||
current += 1;
|
||||
if(current < length) {
|
||||
return hooks[current].apply(this.context, args);
|
||||
}
|
||||
}.bind(this);
|
||||
if(length) {
|
||||
var context = this.context;
|
||||
var promises = []
|
||||
|
||||
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") {
|
||||
// Task is a function that returns a promise
|
||||
executing.then(next);
|
||||
} else {
|
||||
// Task resolves immediately
|
||||
next();
|
||||
promises.push(executing);
|
||||
}
|
||||
// Otherwise Task resolves immediately, continue
|
||||
});
|
||||
|
||||
|
||||
|
||||
} else {
|
||||
executing = defer.promise;
|
||||
defer.resolve();
|
||||
}
|
||||
|
||||
return executing;
|
||||
return RSVP.all(promises);
|
||||
};
|
|
@ -1,6 +1,6 @@
|
|||
EPUBJS.replace = {};
|
||||
EPUBJS.replace.links = function(view, renderer) {
|
||||
var task = new RSVP.defer();
|
||||
|
||||
var links = view.document.querySelectorAll("a[href]");
|
||||
var replaceLinks = function(link){
|
||||
var href = link.getAttribute("href");
|
||||
|
@ -39,6 +39,5 @@ EPUBJS.replace.links = function(view, renderer) {
|
|||
replaceLinks(links[i]);
|
||||
}
|
||||
|
||||
task.resolve();
|
||||
return task.promise;
|
||||
|
||||
};
|
Loading…
Add table
Add a link
Reference in a new issue