Skip to content

Class: Container

Defined in: src/wirestate-core/container/container.ts:122

Dependency injection container for one Wirestate scope.

Remarks

A container owns its local bindings and the instances created from them. It also owns provider lifecycle state and the plugin bindings installed on that container. Child containers inherit parent bindings while keeping their own local registrations and lifecycle state.

Throws

WirestateError If the config is invalid or activate names a token missing from bindings.

Example

typescript
import { Container, Injectable } from "@wirestate/core";

@Injectable()
class LoggerService {}

@Injectable()
class CounterService {}

const container: Container = new Container({
  bindings: [CounterService, LoggerService],
});

const loggerService: LoggerService = container.get(LoggerService);

Extends

  • ContainerKernel

Constructors

Constructor

ts
new Container(config?: ContainerConfig): Container;

Defined in: src/wirestate-core/container/container.ts:130

Creates a Wirestate container.

Parameters

ParameterTypeDescription
configContainerConfigContainer setup config.

Returns

Container

Throws

WirestateError If the config is invalid.

Overrides

ts
ContainerKernel.constructor

Methods

bind()

ts
bind<T>(binding: 
  | Newable<object>
  | BindingDescriptor<T>): this;

Defined in: src/wirestate-core/container/container.ts:184

Binds a service class or a binding descriptor to this container.

Type Parameters

Type Parameter
T

Parameters

ParameterTypeDescription
binding| Newable<object> | BindingDescriptor<T>Service class or binding descriptor to register.

Returns

this

The same container for chaining.

Remarks

A bare service class binds as a singleton instance binding keyed by the class itself. Binding a descriptor lets you use an explicit token, implementation class, factory, value, or transient scope.

Throws

WirestateError If the binding is invalid, or a transient instance binding's class declares a lifecycle or messaging handler.

Overrides

ts
ContainerKernel.bind

deprovision()

ts
deprovision(): this;

Defined in: src/wirestate-core/container/container.ts:225

Deprovisions this container for a framework provider.

Returns

this

The same container for chaining.

Remarks

Runs @OnDeprovision in reverse provision order. Idempotent: deprovisioning a container that is not currently provisioned is a no-op.


get()

Call Signature

ts
get<T>(token: ServiceToken<T>): T;

Defined in: src/wirestate-core/container/container-kernel.ts:126

Retrieves a service from this container.

Resolution options can make a lookup optional or lazy. Optional lookups resolve undefined instead of throwing. Lazy lookups return a thunk that resolves on first call.

Type Parameters
Type Parameter
T
Parameters
ParameterTypeDescription
tokenServiceToken<T>Token to resolve.
Returns

T

The resolved value, thunk, or undefined for optional misses.

Throws

WirestateError If the token is not bound and not optional, or if a circular dependency is detected while constructing the value. Errors thrown by a binding's constructor or factory propagate unchanged.

Inherited from
ts
ContainerKernel.get

Call Signature

ts
get<T>(token: ServiceToken<T>, options: {
  optional: true;
}): Optional<T>;

Defined in: src/wirestate-core/container/container-kernel.ts:127

Retrieves a service from this container.

Resolution options can make a lookup optional or lazy. Optional lookups resolve undefined instead of throwing. Lazy lookups return a thunk that resolves on first call.

Type Parameters
Type Parameter
T
Parameters
ParameterTypeDescription
tokenServiceToken<T>Token to resolve.
options{ optional: true; }-
options.optionaltrue-
Returns

Optional<T>

The resolved value, thunk, or undefined for optional misses.

Throws

WirestateError If the token is not bound and not optional, or if a circular dependency is detected while constructing the value. Errors thrown by a binding's constructor or factory propagate unchanged.

Inherited from
ts
ContainerKernel.get

Call Signature

ts
get<T>(token: ServiceToken<T>, options: {
  lazy: true;
}): () => T;

Defined in: src/wirestate-core/container/container-kernel.ts:128

Retrieves a service from this container.

Resolution options can make a lookup optional or lazy. Optional lookups resolve undefined instead of throwing. Lazy lookups return a thunk that resolves on first call.

Type Parameters
Type Parameter
T
Parameters
ParameterTypeDescription
tokenServiceToken<T>Token to resolve.
options{ lazy: true; }-
options.lazytrue-
Returns

The resolved value, thunk, or undefined for optional misses.

() => T

Throws

WirestateError If the token is not bound and not optional, or if a circular dependency is detected while constructing the value. Errors thrown by a binding's constructor or factory propagate unchanged.

Inherited from
ts
ContainerKernel.get

Call Signature

ts
get<T>(token: ServiceToken<T>, options: {
  lazy: true;
  optional: true;
}): () => Optional<T>;

Defined in: src/wirestate-core/container/container-kernel.ts:129

Retrieves a service from this container.

Resolution options can make a lookup optional or lazy. Optional lookups resolve undefined instead of throwing. Lazy lookups return a thunk that resolves on first call.

Type Parameters
Type Parameter
T
Parameters
ParameterTypeDescription
tokenServiceToken<T>Token to resolve.
options{ lazy: true; optional: true; }-
options.lazytrue-
options.optionaltrue-
Returns

The resolved value, thunk, or undefined for optional misses.

() => Optional<T>

Throws

WirestateError If the token is not bound and not optional, or if a circular dependency is detected while constructing the value. Errors thrown by a binding's constructor or factory propagate unchanged.

Inherited from
ts
ContainerKernel.get

Call Signature

ts
get<T>(token: ServiceToken<T>, options?: {
  lazy?: false;
  optional?: boolean;
}): Optional<T>;

Defined in: src/wirestate-core/container/container-kernel.ts:130

Retrieves a service from this container.

Resolution options can make a lookup optional or lazy. Optional lookups resolve undefined instead of throwing. Lazy lookups return a thunk that resolves on first call.

Type Parameters
Type Parameter
T
Parameters
ParameterTypeDescription
tokenServiceToken<T>Token to resolve.
options?{ lazy?: false; optional?: boolean; }-
options.lazy?false-
options.optional?boolean-
Returns

Optional<T>

The resolved value, thunk, or undefined for optional misses.

Throws

WirestateError If the token is not bound and not optional, or if a circular dependency is detected while constructing the value. Errors thrown by a binding's constructor or factory propagate unchanged.

Inherited from
ts
ContainerKernel.get

Call Signature

ts
get<T>(token: ServiceToken<T>, options?: {
  lazy?: boolean;
  optional?: boolean;
}): Optional<T> | (() => Optional<T>);

Defined in: src/wirestate-core/container/container-kernel.ts:131

Retrieves a service from this container.

Resolution options can make a lookup optional or lazy. Optional lookups resolve undefined instead of throwing. Lazy lookups return a thunk that resolves on first call.

Type Parameters
Type Parameter
T
Parameters
ParameterTypeDescription
tokenServiceToken<T>Token to resolve.
options?{ lazy?: boolean; optional?: boolean; }-
options.lazy?boolean-
options.optional?boolean-
Returns

Optional<T> | (() => Optional<T>)

The resolved value, thunk, or undefined for optional misses.

Throws

WirestateError If the token is not bound and not optional, or if a circular dependency is detected while constructing the value. Errors thrown by a binding's constructor or factory propagate unchanged.

Inherited from
ts
ContainerKernel.get

getActiveInstances()

ts
getActiveInstances(): readonly object[];

Defined in: src/wirestate-core/container/container-kernel.ts:207

Returns the service instances this container constructed for singleton instance bindings, in creation order. Values constructed for value and factory bindings are not service instances and are not included. Transient instances are excluded too. They are construct-and-forget and never owned or tracked by the container.

Returns

readonly object[]

Snapshot of this container's active service instances.

Inherited from

ts
ContainerKernel.getActiveInstances

getOwnBindings()

ts
getOwnBindings(): readonly BindingDescriptor<unknown>[];

Defined in: src/wirestate-core/container/container-kernel.ts:195

Returns the binding descriptors registered on this container in registration order, ignoring parent containers.

Returns

readonly BindingDescriptor<unknown>[]

Snapshot of this container's own binding descriptors.

Inherited from

ts
ContainerKernel.getOwnBindings

has()

ts
has<T>(token: ServiceToken<T>): boolean;

Defined in: src/wirestate-core/container/container-kernel.ts:174

Returns whether this container or one of its parents has a binding for this token.

Type Parameters

Type Parameter
T

Parameters

ParameterTypeDescription
tokenServiceToken<T>Token to check.

Returns

boolean

Whether the token can be resolved from this container.

Inherited from

ts
ContainerKernel.has

hasOwn()

ts
hasOwn<T>(token: ServiceToken<T>): boolean;

Defined in: src/wirestate-core/container/container-kernel.ts:185

Returns whether this container itself has a binding for this token, ignoring parent containers.

Type Parameters

Type Parameter
T

Parameters

ParameterTypeDescription
tokenServiceToken<T>Token to check.

Returns

boolean

Whether this container owns a binding for the token.

Inherited from

ts
ContainerKernel.hasOwn

provision()

ts
provision(): this;

Defined in: src/wirestate-core/container/container.ts:210

Provisions this container for a framework provider.

Returns

this

The same container for chaining.

Remarks

Resolves provider lifecycle participants and runs @OnProvision once for this provision cycle. A container is provisioned by at most one provider at a time. Provisioning an already provisioned container throws. Deprovision it first.

Throws

WirestateError If the container is already provisioned.


unbind()

ts
unbind<T>(token: ServiceToken<T>): this;

Defined in: src/wirestate-core/container/container.ts:241

Unbinds a local token and deactivates values created from it.

Type Parameters

Type Parameter
T

Parameters

ParameterTypeDescription
tokenServiceToken<T>Token to unbind.

Returns

this

The same container for chaining.

Remarks

If the binding owns a provisioned provider lifecycle instance, @OnDeprovision runs before @OnDeactivation.

Overrides

ts
ContainerKernel.unbind

unbindAll()

ts
unbindAll(): this;

Defined in: src/wirestate-core/container/container.ts:258

Unbinds every local binding and deactivates this container's instances.

Returns

this

The same container for chaining.

Remarks

Provider lifecycle instances are deprovisioned before they deactivate. Parent bindings and parent instances are not changed.

Overrides

ts
ContainerKernel.unbindAll

Properties

parent?

ts
readonly optional parent?: ContainerKernel;

Defined in: src/wirestate-core/container/container-kernel.ts:31

Parent container when this container was created as a child container.

Inherited from

ts
ContainerKernel.parent