import '@aws/lambda-invoke-store'; import type { InvocationScopedOptions } from './types/InvocationScoped.js'; /** * Holds a value scoped to a single invocation when invocations may run * concurrently in the same execution environment, and shared across * invocations otherwise. * * The storage is resolved on every access rather than once at construction, * so a value created during initialization keeps working once an invocation * context exists. Reads and writes are scoped to the invocation only when the * `AWS_LAMBDA_MAX_CONCURRENCY` environment variable is set, the RIC exposes an * `InvokeStore`, and an invocation context is active; in every other case they * fall through to the shared value, which is also what * {@link InvocationScoped.getShared | `getShared()`} and * {@link InvocationScoped.setShared | `setShared()`} always operate on. * * Values are handed out by reference: a caller that retains the value can * mutate it, so containers meant to stay per-invocation should not be passed * outside the invocation that owns them. * * @example * ```ts * import { InvocationScoped } from '@aws-lambda-powertools/commons/utils/invocation-scoped'; * * // A value with a shared default that invocations can override * const logLevel = new InvocationScoped('powertools.logger.logLevel', { * initial: 12, * }); * logLevel.set(8); // only this invocation logs at DEBUG * * // A container created anew for each invocation * const attributes = new InvocationScoped>( * 'powertools.logger.temporaryAttributes', * { fresh: () => ({}) } * ); * attributes.get().foo = 'bar'; * ``` * * @typeParam T - The type of the value being held * * @internal */ declare class InvocationScoped { #private; /** * @param name - Description of the value, used for the storage key, e.g. `powertools.logger.logLevel` * @param {Object} options - Options to create the value * @param options.initial - Value shared across invocations until one sets its own * @param options.fresh - Creates the value, called once per invocation on first access, and once for the shared value */ constructor(name: string, options: InvocationScopedOptions); /** * Whether reads and writes are currently scoped to a single invocation. */ get isScoped(): boolean; /** * Gets the invocation-scoped value when there is one, otherwise the shared * value. * * A `fresh` value is created on first access, and the same one is handed back * for the rest of the invocation. */ get(): T; /** * Sets the value for the current invocation, or the shared value when no * invocation context is active, i.e. during initialization. * * `undefined` is a value like any other: it shadows the shared value until * {@link InvocationScoped.reset | `reset()`} discards it. * * @param value - The value to store */ set(value: T): void; /** * Discards the value, so that a `fresh` one is created on the next access and * an `initial` one goes back to the shared value. * * Outside an invocation context the shared value is discarded instead, which * puts an `initial` value back to the one given at construction. */ reset(): void; /** * Gets the value shared across invocations, ignoring any invocation context. * * It is created on first access, whether from here or from * {@link InvocationScoped.get | `get()`}. */ getShared(): T; /** * Sets the value shared across invocations, ignoring any invocation context. * * @param value - The value to store */ setShared(value: T): void; } export { InvocationScoped }; //# sourceMappingURL=InvocationScoped.d.ts.map