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
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
new Container(config?: ContainerConfig): Container;Defined in: src/wirestate-core/container/container.ts:130
Creates a Wirestate container.
Parameters
| Parameter | Type | Description |
|---|---|---|
config | ContainerConfig | Container setup config. |
Returns
Container
Throws
WirestateError If the config is invalid.
Overrides
ContainerKernel.constructorMethods
bind()
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
| Parameter | Type | Description |
|---|---|---|
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
ContainerKernel.binddeprovision()
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
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
| Parameter | Type | Description |
|---|---|---|
token | ServiceToken<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
ContainerKernel.getCall Signature
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
| Parameter | Type | Description |
|---|---|---|
token | ServiceToken<T> | Token to resolve. |
options | { optional: true; } | - |
options.optional | true | - |
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
ContainerKernel.getCall Signature
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
| Parameter | Type | Description |
|---|---|---|
token | ServiceToken<T> | Token to resolve. |
options | { lazy: true; } | - |
options.lazy | true | - |
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
ContainerKernel.getCall Signature
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
| Parameter | Type | Description |
|---|---|---|
token | ServiceToken<T> | Token to resolve. |
options | { lazy: true; optional: true; } | - |
options.lazy | true | - |
options.optional | true | - |
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
ContainerKernel.getCall Signature
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
| Parameter | Type | Description |
|---|---|---|
token | ServiceToken<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
ContainerKernel.getCall Signature
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
| Parameter | Type | Description |
|---|---|---|
token | ServiceToken<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
ContainerKernel.getgetActiveInstances()
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
ContainerKernel.getActiveInstancesgetOwnBindings()
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
ContainerKernel.getOwnBindingshas()
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
| Parameter | Type | Description |
|---|---|---|
token | ServiceToken<T> | Token to check. |
Returns
boolean
Whether the token can be resolved from this container.
Inherited from
ContainerKernel.hashasOwn()
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
| Parameter | Type | Description |
|---|---|---|
token | ServiceToken<T> | Token to check. |
Returns
boolean
Whether this container owns a binding for the token.
Inherited from
ContainerKernel.hasOwnprovision()
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()
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
| Parameter | Type | Description |
|---|---|---|
token | ServiceToken<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
ContainerKernel.unbindunbindAll()
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
ContainerKernel.unbindAllProperties
parent?
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
ContainerKernel.parent