UNPKG

@aws-amplify/core

Version:

Core category of aws-amplify

1 lines • 2.05 kB
{"version":3,"file":"createCtxResolver.mjs","sources":["../../../src/context/createCtxResolver.ts"],"sourcesContent":["// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.\n// SPDX-License-Identifier: Apache-2.0\nimport { getGlobalContext } from './globalContext';\n/**\n * Creates a context resolver for class-based providers that\n * accept an optional explicit {@link AmplifyContext} at construction time.\n *\n * The returned function resolves the context **fresh on every call**:\n * the explicitly-passed context (when one was given) always wins, otherwise\n * the *current* global context is looked up at call time.\n *\n * **Never capture `getGlobalContext()` in a constructor.** `configure()`\n * replaces the frozen global context object wholesale, so a snapshot taken at\n * construction goes stale after any reconfigure, and eager resolution throws\n * {@link NoAmplifyContextError} when the provider is instantiated before\n * `configure()` runs. Store only the explicitly-passed context and call the\n * resolver returned here once per operation.\n *\n * @example\n * ```ts\n * class MyProvider {\n * private readonly resolveCtx: () => AmplifyContext;\n * constructor(ctx?: AmplifyContext) {\n * this.resolveCtx = createCtxResolver(ctx);\n * }\n * async doWork() {\n * const ctx = this.resolveCtx(); // fresh per operation\n * }\n * }\n * ```\n *\n * @param explicitCtx - The context explicitly passed by the caller, if any.\n * @returns A function resolving `explicitCtx ?? getGlobalContext()` per call.\n * Throws {@link NoAmplifyContextError} when neither is available.\n *\n * @internal\n */\nexport function createCtxResolver(explicitCtx) {\n return () => explicitCtx ?? getGlobalContext();\n}\n"],"names":[],"mappings":";;AAAA;AACA;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACO,SAAS,iBAAiB,CAAC,WAAW,EAAE;AAC/C,IAAI,OAAO,MAAM,WAAW,IAAI,gBAAgB,EAAE;AAClD;;;;"}