Files
OpenMaopaoya/frontend-uni/node_modules/tapable/lib/AsyncSeriesBailHook.js
T

92 lines
2.8 KiB
JavaScript

/*
MIT License http://www.opensource.org/licenses/mit-license.php
Author Tobias Koppers @sokra
*/
"use strict";
const Hook = require("./Hook");
const HookCodeFactory = require("./HookCodeFactory");
/** @typedef {import("./Hook").EXPECTED_ANY} EXPECTED_ANY */
/** @typedef {import("./Hook").EXPECTED_FUNCTION} EXPECTED_FUNCTION */
/** @typedef {import("./Hook").CompileOptions<EXPECTED_ANY, EXPECTED_ANY, EXPECTED_ANY>} CompileOptions */
/** @typedef {import("./Hook").UnsetAdditionalOptions} UnsetAdditionalOptions */
/** @typedef {import("./HookCodeFactory").ContentOptions} ContentOptions */
/**
* @template {EXPECTED_ANY[]} T
* @typedef {import("./Hook").ArgumentNames<T>} ArgumentNames
*/
/**
* @template T
* @typedef {import("./Hook").AsArray<T>} AsArray
*/
/**
* Mirror of the `AsyncSeriesBailHook` class declared in `tapable.d.ts` for
* use as the factory function's return type, so `new AsyncSeriesBailHook(...)`
* resolves to the specific subclass type instead of the generic `Hook`.
* @template T
* @template R
* @template [AdditionalOptions=UnsetAdditionalOptions]
* @typedef {import("../tapable").AsyncSeriesBailHook<T, R, AdditionalOptions>} AsyncSeriesBailHookType
*/
class AsyncSeriesBailHookCodeFactory extends HookCodeFactory {
/**
* @param {ContentOptions} options content generation options
* @returns {string} generated body code
*/
content({ onError, onResult, resultReturns, onDone }) {
return this.callTapsSeries({
onError: (i, err, next, doneBreak) => onError(err) + doneBreak(true),
onResult: (i, result, next) =>
`if(${result} !== undefined) {\n${onResult(
result
)}\n} else {\n${next()}}\n`,
resultReturns,
onDone
});
}
}
const factory = new AsyncSeriesBailHookCodeFactory();
/**
* @this {Hook<EXPECTED_ANY, EXPECTED_ANY, EXPECTED_ANY>}
* @param {CompileOptions} options compile options
* @returns {EXPECTED_FUNCTION} the compiled call function
*/
function COMPILE(options) {
factory.setup(this, options);
return factory.create(options);
}
/**
* @constructor
* @template T
* @template R
* @template [AdditionalOptions=UnsetAdditionalOptions]
* @param {ArgumentNames<AsArray<T>>=} args argument names of the hook
* @param {string=} name name of the hook
* @returns {AsyncSeriesBailHookType<T, R, AdditionalOptions>} a new AsyncSeriesBailHook instance
*/
function AsyncSeriesBailHook(
args = /** @type {ArgumentNames<AsArray<T>>} */ (/** @type {unknown} */ ([])),
name = undefined
) {
const hook = new Hook(args, name);
hook.constructor = AsyncSeriesBailHook;
hook.compile = COMPILE;
// @ts-expect-error for performance reasons
hook._call = undefined;
hook.call = undefined;
return /** @type {AsyncSeriesBailHookType<T, R, AdditionalOptions>} */ (
/** @type {unknown} */ (hook)
);
}
/** @type {EXPECTED_ANY} */
AsyncSeriesBailHook.prototype = null;
module.exports = AsyncSeriesBailHook;