import { type Token } from "./tokens.ts"; import type { Provider, ProviderList } from "./providers.ts"; /** * A dependency injection (DI) container will keep track of all bindings * and hold the actual instances of your services. */ export declare class Container { private readonly providers; private readonly singletons; /** * Async constructions that have been started but have not settled yet, keyed by token. * * Without this, two overlapping `getAsync()` calls for the same token would both see an * empty `singletons` entry and construct it twice, breaking singleton semantics. */ private readonly pending; private readonly parent?; private readonly factory; constructor(parent?: Container); /** * Binds multiple providers to this container. * * Providers may be passed individually or as (nested) arrays. To define a list of providers * upfront, outside of a container, use `defineProviders()`. * * @param providers one or more providers, optionally nested in arrays * * {@link https://needle-di.io/concepts/binding.html#binding-multiple-providers} */ bindAll(...providers: ProviderList): this; /** * Binds a provider to this container. * * {@link https://needle-di.io/concepts/binding.html#binding} */ bind(provider: Provider): this; /** * Unbinds a token. * * This removes all providers for that token, including multi-providers, * as well as any instances that were already constructed. * * {@link https://needle-di.io/concepts/binding.html#clear-binding} */ unbind(token: Token): this; /** * Unbinds all providers. * * {@link https://needle-di.io/concepts/binding.html#binding} */ unbindAll(): this; /** * Retrieves a service from this container. * * {@link https://needle-di.io/concepts/containers.html} */ get(token: Token): T; get(token: Token, options: { multi: true; }): T[]; get(token: Token, options: { optional: true; }): T | undefined; get(token: Token, options: { multi: true; optional: true; }): T[] | undefined; get(token: Token, options: { lazy: true; }): () => T; get(token: Token, options: { lazy: true; multi: true; }): () => T[]; get(token: Token, options: { lazy: true; optional: true; }): () => T | undefined; get(token: Token, options: { lazy: true; multi: true; optional: true; }): () => T[] | undefined; get(token: Token, options?: { optional?: boolean; multi?: boolean; lazy?: false; }): T | T[] | undefined; get(token: Token, options?: { optional?: boolean; multi?: boolean; lazy?: boolean; }): T | T[] | undefined | (() => T | T[] | undefined); /** * Retrieves a service from this container asynchronously. * * {@link https://needle-di.io/advanced/async-injection.html} */ getAsync(token: Token): Promise; getAsync(token: Token, options: { multi: true; }): Promise; getAsync(token: Token, options: { optional: true; }): Promise; getAsync(token: Token, options: { multi: true; optional: true; }): Promise; getAsync(token: Token, options: { lazy: true; }): () => Promise; getAsync(token: Token, options: { lazy: true; multi: true; }): () => Promise; getAsync(token: Token, options: { lazy: true; optional: true; }): () => Promise; getAsync(token: Token, options: { lazy: true; multi: true; optional: true; }): () => Promise; getAsync(token: Token, options?: { optional?: boolean; multi?: boolean; lazy?: false; }): Promise; getAsync(token: Token, options?: { optional?: boolean; multi?: boolean; lazy?: boolean; }): Promise | (() => Promise); /** * Runs a function within an injection context backed by this container, so that it * can use `inject()` and `injectAsync()` instead of `container.get()`. The return * value of the function is passed through. * * The injection context is only active for as long as the function runs * synchronously. When passing an async function, `inject()` and `injectAsync()` * must therefore be called before its first `await`. * * {@link https://needle-di.io/concepts/injection.html#running-in-an-injection-context} */ runInInjectionContext(block: (container: Container) => T): T; /** * Creates a child container. * * {@link https://needle-di.io/advanced/child-containers.html} */ createChild(): Container; /** * Returns whether the container has one or more providers for this token. */ has(token: Token): boolean; /** * Constructs the providers for a token asynchronously, ensuring that concurrent * requests for the same token share a single construction and therefore a single * instance. Callers that join an already running construction inherit its result, * not its chain: their own chain has already been checked for cycles by the caller. */ private constructOnce; private autoBindIfNeeded; private existingProviderAlreadyProvided; } /** * Bootstraps a new container and obtains a service using the provided token. */ export declare function bootstrap(token: Token): T; /** * Bootstraps a new container and obtains a service asynchronously using the provided token. */ export declare function bootstrapAsync(token: Token): Promise;