UNPKG

@temporalio/common

Version:

Common library for code that's used across the Client, Worker, and/or Workflow

46 lines 2.37 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.composeInterceptorsWith = composeInterceptorsWith; exports.composeInterceptors = composeInterceptors; /** * Shared implementation for interceptor chaining. * * The workflow package needs one small behavioral extension over the generic interceptor * semantics: every `next(...)` continuation it hands to workflow interceptors must first * restore the workflow random scope that was current when that continuation was created. * * We keep the chain-construction loop here as the single source of truth so ordering and * basic composition behavior cannot drift between the generic implementation used by client, * worker, activity, and nexus code and the workflow-specific implementation that decorates * `next(...)` at each boundary. The only behavior that varies between callers is the * `wrapNext` hook: most packages use the identity wrapper, while workflow code supplies a * wrapper that re-enters the captured workflow async-local random scope. */ function composeInterceptorsWith(interceptors, method, next, wrapNext) { let composedNext = next; for (let i = interceptors.length - 1; i >= 0; --i) { const interceptor = interceptors[i]; if (interceptor?.[method] !== undefined) { const prev = wrapNext(composedNext); // We lose type safety here because Typescript can't deduce that interceptor[method] is a function that returns // the same type as Next<I, M> composedNext = ((input) => interceptor[method](input, prev)); } } return composedNext; } /** * Compose all interceptor methods into a single function. * * Calling the composed function results in calling each of the provided interceptor, in order (from the first to * the last), followed by the original function provided as argument to `composeInterceptors()`. * * @param interceptors a list of interceptors * @param method the name of the interceptor method to compose * @param next the original function to be executed at the end of the interception chain */ // ts-prune-ignore-next (imported via lib/interceptors) function composeInterceptors(interceptors, method, next) { return composeInterceptorsWith(interceptors, method, next, ((wrappedNext) => wrappedNext)); } //# sourceMappingURL=interceptors.js.map