UNPKG

@aws-amplify/core

Version:

Core category of aws-amplify

45 lines (42 loc) • 1.67 kB
import { getGlobalContext } from './globalContext.mjs'; // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. // SPDX-License-Identifier: Apache-2.0 /** * Creates a context resolver for class-based providers that * accept an optional explicit {@link AmplifyContext} at construction time. * * The returned function resolves the context **fresh on every call**: * the explicitly-passed context (when one was given) always wins, otherwise * the *current* global context is looked up at call time. * * **Never capture `getGlobalContext()` in a constructor.** `configure()` * replaces the frozen global context object wholesale, so a snapshot taken at * construction goes stale after any reconfigure, and eager resolution throws * {@link NoAmplifyContextError} when the provider is instantiated before * `configure()` runs. Store only the explicitly-passed context and call the * resolver returned here once per operation. * * @example * ```ts * class MyProvider { * private readonly resolveCtx: () => AmplifyContext; * constructor(ctx?: AmplifyContext) { * this.resolveCtx = createCtxResolver(ctx); * } * async doWork() { * const ctx = this.resolveCtx(); // fresh per operation * } * } * ``` * * @param explicitCtx - The context explicitly passed by the caller, if any. * @returns A function resolving `explicitCtx ?? getGlobalContext()` per call. * Throws {@link NoAmplifyContextError} when neither is available. * * @internal */ function createCtxResolver(explicitCtx) { return () => explicitCtx ?? getGlobalContext(); } export { createCtxResolver }; //# sourceMappingURL=createCtxResolver.mjs.map