mirror of
https://github.com/futurepress/epub.js.git
synced 2025-10-03 14:59:18 +02:00
Fix eslint errors
This commit is contained in:
parent
c99bfd07b7
commit
a435650c3b
31 changed files with 1077 additions and 1102 deletions
63
src/utils/hook.js
Normal file
63
src/utils/hook.js
Normal file
|
@ -0,0 +1,63 @@
|
|||
/**
|
||||
* Hooks allow for injecting functions that must all complete in order before finishing
|
||||
* They will execute in parallel but all must finish before continuing
|
||||
* Functions may return a promise if they are asycn.
|
||||
* @param {any} context scope of this
|
||||
* @example this.content = new EPUBJS.Hook(this);
|
||||
*/
|
||||
class Hook {
|
||||
constructor(context){
|
||||
this.context = context || this;
|
||||
this.hooks = [];
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a function to be run before a hook completes
|
||||
* @example this.content.register(function(){...});
|
||||
*/
|
||||
register(){
|
||||
for(var i = 0; i < arguments.length; ++i) {
|
||||
if (typeof arguments[i] === "function") {
|
||||
this.hooks.push(arguments[i]);
|
||||
} else {
|
||||
// unpack array
|
||||
for(var j = 0; j < arguments[i].length; ++j) {
|
||||
this.hooks.push(arguments[i][j]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Triggers a hook to run all functions
|
||||
* @example this.content.trigger(args).then(function(){...});
|
||||
*/
|
||||
trigger(){
|
||||
var args = arguments;
|
||||
var context = this.context;
|
||||
var promises = [];
|
||||
|
||||
this.hooks.forEach(function(task) {
|
||||
var executing = task.apply(context, args);
|
||||
|
||||
if(executing && typeof executing["then"] === "function") {
|
||||
// Task is a function that returns a promise
|
||||
promises.push(executing);
|
||||
}
|
||||
// Otherwise Task resolves immediately, continue
|
||||
});
|
||||
|
||||
|
||||
return Promise.all(promises);
|
||||
}
|
||||
|
||||
// Adds a function to be run before a hook completes
|
||||
list(){
|
||||
return this.hooks;
|
||||
}
|
||||
|
||||
clear(){
|
||||
return this.hooks = [];
|
||||
}
|
||||
}
|
||||
export default Hook;
|
Loading…
Add table
Add a link
Reference in a new issue