@alova/wormhole
Version:
More modern openAPI generating solution for alova.js
58 lines (57 loc) • 1.76 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.PluginDriver = void 0;
/**
* PluginDriver manages plugin lifecycle and provides unified hook execution
* Similar to Rollup's PluginDriver but adapted for wormhole's needs
*/
class PluginDriver {
constructor(plugins = []) {
this.plugins = plugins.filter(Boolean); // Remove null/undefined plugins
}
async runHook(name, args, plugin) {
return Promise.resolve()
.then(() => {
const handler = plugin[name];
if (typeof handler !== 'function') {
return null;
}
const result = handler.apply(plugin, args);
return result;
});
}
/**
* Execute hooks sequentially
*/
async hookSeq(name, args, resultFn) {
let promise = Promise.resolve();
for (const plugin of this.plugins) {
promise = promise.then(result => this.runHook(name, resultFn(result, args), plugin));
}
return promise;
}
/**
* Execute hooks until first non-null result
*/
async hookFirst(name, args) {
for (const plugin of this.plugins) {
const result = await this.runHook(name, args, plugin);
if (result) {
return result;
}
}
return Promise.resolve(null);
}
/**
* Execute hooks in parallel
*/
async hookParallel(name, args) {
const parallelPromises = [];
for (const plugin of this.plugins) {
parallelPromises.push(this.runHook(name, args, plugin));
}
await Promise.all(parallelPromises);
}
}
exports.PluginDriver = PluginDriver;
exports.default = PluginDriver;