* Convert a list of hooks into a function that can be used to do an operation through * a chain of hooks. If any of the hook returns without calling the next hook, it * must return shortCircuit: true to stop the chain from continuing to avoid * forgetting to invoke the next hook by mistake. * @p
(hooks, name, defaultStep, validate, mergedContext)
| 169 | * @returns {any} |
| 170 | */ |
| 171 | function buildHooks(hooks, name, defaultStep, validate, mergedContext) { |
| 172 | let lastRunIndex = hooks.length; |
| 173 | /** |
| 174 | * Helper function to wrap around invocation of user hook or the default step |
| 175 | * in order to fill in missing arguments or check returned results. |
| 176 | * Due to the merging of the context, this must be a closure. |
| 177 | * @param {number} index Index in the chain. Default step is 0, last added hook is 1, |
| 178 | * and so on. |
| 179 | * @param {Function} userHookOrDefault Either the user hook or the default step to invoke. |
| 180 | * @param {Function|undefined} next The next wrapped step. If this is the default step, it's undefined. |
| 181 | * @returns {Function} Wrapped hook or default step. |
| 182 | */ |
| 183 | function wrapHook(index, userHookOrDefault, next) { |
| 184 | return function nextStep(arg0, context) { |
| 185 | lastRunIndex = index; |
| 186 | if (context && context !== mergedContext) { |
| 187 | ObjectAssign(mergedContext, context); |
| 188 | } |
| 189 | const hookResult = userHookOrDefault(arg0, mergedContext, next); |
| 190 | if (lastRunIndex > 0 && lastRunIndex === index && !hookResult.shortCircuit) { |
| 191 | throw new ERR_INVALID_RETURN_PROPERTY_VALUE('true', name, 'shortCircuit', |
| 192 | hookResult.shortCircuit); |
| 193 | } |
| 194 | return validate(arg0, mergedContext, hookResult); |
| 195 | }; |
| 196 | } |
| 197 | const chain = [wrapHook(0, defaultStep)]; |
| 198 | for (let i = 0; i < hooks.length; ++i) { |
| 199 | const wrappedHook = wrapHook(i + 1, hooks[i][name], chain[i]); |
| 200 | ArrayPrototypePush(chain, wrappedHook); |
| 201 | } |
| 202 | return chain[chain.length - 1]; |
| 203 | } |
| 204 | |
| 205 | /** |
| 206 | * @typedef {object} ModuleResolveResult |
no test coverage detected
searching dependent graphs…