Class: WireScope
Defined in: src/wirestate-core/container/wire-scope.ts:44
Per-service handle for container work.
Remarks
Inject WireScope when a service needs buses, seeds, or lazy resolution.
Each bound service gets its own transient scope. The scope is valid after service activation and before deactivation. After disposal it throws instead of letting dead services keep talking to the container.
Example
import { Inject, Injectable, WireScope } from "@wirestate/core";
@Injectable()
class CartService {
public constructor(@Inject(WireScope) private readonly scope: WireScope) {}
public addItem(item: CartItem): void {
this.scope.emitEvent("CART_ITEM_ADDED", item);
}
}Accessors
isInactive
Get Signature
get isInactive(): boolean;Defined in: src/wirestate-core/container/wire-scope.ts:75
Whether this scope should stop user work because its service or provider lifecycle ended.
Remarks
Use this as the default async-work guard. It is true when either isDisposed is true or isDeprovisioned is true.
Returns
boolean
True when the scope was disposed or fully deprovisioned.
Constructors
Constructor
new WireScope(container: Optional<Container>): WireScope;Defined in: src/wirestate-core/container/wire-scope.ts:64
Parameters
| Parameter | Type |
|---|---|
container | Optional<Container> |
Returns
WireScope
Methods
emitEvent()
emitEvent<P, T>(
type: T,
payload?: P,
from?: unknown): void;Defined in: src/wirestate-core/container/wire-scope.ts:187
Dispatches an event to the EventBus.
Type Parameters
| Type Parameter | Default type | Description |
|---|---|---|
P | - | Type of the event payload. |
T extends EventType | EventType | Type of the event identifier. |
Parameters
| Parameter | Type | Description |
|---|---|---|
type | T | Event identifier. |
payload? | P | Optional data associated with the event. |
from? | unknown | Optional source identifier (defaults to current scope). |
Returns
void
Throws
WirestateError If accessed before activation or after disposal.
Example
scope.emitEvent("VALUE_CHANGED", { value: "abcd" });executeCommand()
executeCommand<R, D, T>(type: T, data?: D): CommandDescriptor<R>;Defined in: src/wirestate-core/container/wire-scope.ts:405
Dispatches a command and returns progress.
Type Parameters
| Type Parameter | Default type | Description |
|---|---|---|
R | unknown | Type of the command result. |
D | unknown | Type of the command payload. |
T extends CommandType | CommandType | Type of the command identifier. |
Parameters
| Parameter | Type | Description |
|---|---|---|
type | T | Command identifier. |
data? | D | Payload for the command. |
Returns
A descriptor with status and task.
Throws
WirestateError If accessed before activation or after disposal.
Throws
WirestateError If no command handler is registered.
Example
const descriptor: CommandDescriptor = scope.executeCommand("LOGOUT");
await descriptor.task;executeOptionalCommand()
executeOptionalCommand<R, D, T>(type: T, data?: D): Optional<CommandDescriptor<R>>;Defined in: src/wirestate-core/container/wire-scope.ts:436
Dispatches a command if a handler is registered, otherwise returns null.
Type Parameters
| Type Parameter | Default type | Description |
|---|---|---|
R | unknown | Type of the command result. |
D | unknown | Type of the command payload. |
T extends CommandType | CommandType | Type of the command identifier. |
Parameters
| Parameter | Type | Description |
|---|---|---|
type | T | Command identifier. |
data? | D | Payload for the command. |
Returns
Optional<CommandDescriptor<R>>
A CommandDescriptor or null.
Throws
WirestateError If accessed before activation or after disposal.
Example
const descriptor: CommandDescriptor | null = scope.executeOptionalCommand("CLEANUP_CACHE");
if (descriptor) {
await descriptor.task;
}getContainer()
getContainer(): Container;Defined in: src/wirestate-core/container/wire-scope.ts:93
Provides direct access to the underlying Inversify Container.
Returns
The active Container.
Throws
WirestateError If accessed before activation or after disposal.
Example
const container: Container = scope.getContainer();
container.bind("TOKEN").toConstantValue(42);getSeed()
Call Signature
getSeed<T>(): T;Defined in: src/wirestate-core/container/wire-scope.ts:515
Reads the shared seed object.
Type Parameters
| Type Parameter | Description |
|---|---|
T extends AnyObject | Expected type of the global seed object. |
Returns
T
The global seed object.
Remarks
Call without a key to get the one shared seed for this container.
Throws
WirestateError If accessed before activation or after disposal.
Example
interface GlobalSeed {
apiUrl: string;
}
const seeds: GlobalSeed = scope.getSeed();Call Signature
getSeed<T>(seed: SeedKey): Optional<T>;Defined in: src/wirestate-core/container/wire-scope.ts:534
Reads a targeted seed value.
Type Parameters
| Type Parameter | Description |
|---|---|
T | Expected type of the seed value. |
Parameters
| Parameter | Type | Description |
|---|---|---|
seed | SeedKey | Lookup key (identifier or token) for the seed. |
Returns
Optional<T>
The seed value or null if not found.
Remarks
Targeted seeds are keyed by service class, string, or symbol.
Throws
WirestateError If accessed before activation or after disposal.
Example
const apiUrl: string = scope.getSeed("API_URL");queryData()
queryData<R, D, T>(type: T, data?: D): R;Defined in: src/wirestate-core/container/wire-scope.ts:257
Dispatches a query and returns the handler result as-is.
Type Parameters
| Type Parameter | Default type | Description |
|---|---|---|
R | unknown | Type of the query result. |
D | unknown | Type of the query data (payload). |
T extends QueryType | QueryType | Type of the query identifier. |
Parameters
| Parameter | Type | Description |
|---|---|---|
type | T | Query identifier. |
data? | D | Input data for the query handler. |
Returns
R
The query result.
Throws
WirestateError If accessed before activation or after disposal.
Throws
WirestateError If no query handler is registered.
Example
const user: User = scope.queryData("GET_USER", { id: 1 });queryDataAsync()
queryDataAsync<R, D, T>(type: T, data?: D): Promise<R>;Defined in: src/wirestate-core/container/wire-scope.ts:282
Dispatches a query and returns the result as a Promise.
Type Parameters
| Type Parameter | Default type | Description |
|---|---|---|
R | unknown | Type of the query result. |
D | unknown | Type of the query data (payload). |
T extends QueryType | QueryType | Type of the query identifier. |
Parameters
| Parameter | Type | Description |
|---|---|---|
type | T | Query identifier. |
data? | D | Input data for the query handler. |
Returns
Promise<R>
A Promise resolving to the query result.
Throws
WirestateError If accessed before activation or after disposal.
Throws
WirestateError If no query handler is registered.
Example
const user: User = await scope.queryDataAsync("GET_USER", { id: 1 });queryOptionalData()
queryOptionalData<R, D, T>(type: T, data?: D): Optional<R>;Defined in: src/wirestate-core/container/wire-scope.ts:306
Dispatches a synchronous query and returns the result, or null if no handler is registered.
Type Parameters
| Type Parameter | Default type | Description |
|---|---|---|
R | unknown | Type of the query result. |
D | unknown | Type of the query data (payload). |
T extends QueryType | QueryType | Type of the query identifier. |
Parameters
| Parameter | Type | Description |
|---|---|---|
type | T | Query identifier. |
data? | D | Input data for the query handler. |
Returns
Optional<R>
The query result or null.
Throws
WirestateError If accessed before activation or after disposal.
Example
const config: Config | null = scope.queryOptionalData("GET_CONFIG");queryOptionalDataAsync()
queryOptionalDataAsync<R, D, T>(type: T, data?: D): Promise<Optional<R>>;Defined in: src/wirestate-core/container/wire-scope.ts:330
Dispatches a query and returns the result as a Promise, or null if no handler is registered.
Type Parameters
| Type Parameter | Default type | Description |
|---|---|---|
R | unknown | Type of the query result. |
D | unknown | Type of the query data (payload). |
T extends QueryType | QueryType | Type of the query identifier. |
Parameters
| Parameter | Type | Description |
|---|---|---|
type | T | Query identifier. |
data? | D | Input data for the query handler. |
Returns
Promise<Optional<R>>
A Promise resolving to the query result or null.
Throws
WirestateError If accessed before activation or after disposal.
Example
const config: Config | null = await scope.queryOptionalDataAsync("GET_CONFIG");registerCommandHandler()
registerCommandHandler<D, R>(type: CommandType, handler: CommandHandler<D, R>): CommandUnregister;Defined in: src/wirestate-core/container/wire-scope.ts:464
Registers a handler for a specific command type.
Type Parameters
| Type Parameter | Default type | Description |
|---|---|---|
D | unknown | Type of the command payload. |
R | unknown | Type of the command result. |
Parameters
| Parameter | Type | Description |
|---|---|---|
type | CommandType | Command identifier. |
handler | CommandHandler<D, R> | The handler function. |
Returns
A function to unregister the handler.
Throws
WirestateError If accessed before activation or after disposal.
Example
scope.registerCommandHandler("LOG_ERROR", (error) => {
console.error(error);
});registerQueryHandler()
registerQueryHandler<D, R>(type: QueryType, handler: QueryHandler<D, R>): QueryUnregister;Defined in: src/wirestate-core/container/wire-scope.ts:356
Registers a handler for a specific query type.
Type Parameters
| Type Parameter | Default type | Description |
|---|---|---|
D | unknown | Type of the query data (payload). |
R | unknown | Type of the query result. |
Parameters
| Parameter | Type | Description |
|---|---|---|
type | QueryType | Query identifier. |
handler | QueryHandler<D, R> | The handler function. |
Returns
A function to unregister the handler.
Throws
WirestateError If accessed before activation or after disposal.
Example
scope.registerQueryHandler("GET_DATE_NOW", () => new Date());resolve()
resolve<T>(injectionId: ServiceIdentifier<T>): T;Defined in: src/wirestate-core/container/wire-scope.ts:133
Resolves a service or value from the container.
Type Parameters
| Type Parameter | Description |
|---|---|
T | Type of the service or value to resolve. |
Parameters
| Parameter | Type | Description |
|---|---|---|
injectionId | ServiceIdentifier<T> | Service token (class constructor, symbol, or string). |
Returns
T
The resolved instance or value.
Remarks
Use this for lazy work or to soften a circular dependency. Constructor injection is a handshake at startup; resolve is knocking only when you actually need the other service.
Throws
WirestateError If accessed before activation or after disposal.
Throws
If the service cannot be resolved from the container.
Example
const service: MyService = scope.resolve(MyService);resolveOptional()
resolveOptional<T>(injectionId: ServiceIdentifier<T>): Optional<T>;Defined in: src/wirestate-core/container/wire-scope.ts:159
Lazily resolves a service if it is bound, otherwise returns null.
Type Parameters
| Type Parameter | Description |
|---|---|
T | Type of the service or value to resolve. |
Parameters
| Parameter | Type | Description |
|---|---|---|
injectionId | ServiceIdentifier<T> | Service token (class constructor, symbol, or string). |
Returns
Optional<T>
The resolved instance, value, or null if not bound.
Throws
WirestateError If accessed before activation or after disposal.
Example
const logger: Logger | null = scope.resolveOptional(Logger);
logger?.info("Resolved optionally");subscribeToEvent()
subscribeToEvent(handler: EventHandler): EventUnsubscriber;Defined in: src/wirestate-core/container/wire-scope.ts:214
Subscribes to all events on the EventBus.
Parameters
| Parameter | Type | Description |
|---|---|---|
handler | EventHandler | Function called for every emitted event. |
Returns
A function to unsubscribe.
Throws
WirestateError If accessed before activation or after disposal.
Example
const unsubscribe: EventUnsubscriber = scope.subscribeToEvent((event) => {
console.log("Event received:", event);
});unregisterCommandHandler()
unregisterCommandHandler<D, R>(type: CommandType, handler: CommandHandler<D, R>): void;Defined in: src/wirestate-core/container/wire-scope.ts:489
Removes a specific command handler registration.
Type Parameters
| Type Parameter | Default type | Description |
|---|---|---|
D | unknown | Type of the command payload. |
R | unknown | Type of the command result. |
Parameters
| Parameter | Type | Description |
|---|---|---|
type | CommandType | Command identifier. |
handler | CommandHandler<D, R> | The handler instance to remove. |
Returns
void
Throws
WirestateError If accessed before activation or after disposal.
Example
scope.unregisterCommandHandler("LOG_ERROR", this.handleLogError);unregisterQueryHandler()
unregisterQueryHandler<D, R>(type: QueryType, handler: QueryHandler<D, R>): void;Defined in: src/wirestate-core/container/wire-scope.ts:378
Removes a specific query handler registration.
Type Parameters
| Type Parameter | Default type | Description |
|---|---|---|
D | unknown | Type of the query data (payload). |
R | unknown | Type of the query result. |
Parameters
| Parameter | Type | Description |
|---|---|---|
type | QueryType | Query identifier. |
handler | QueryHandler<D, R> | The handler instance to remove. |
Returns
void
Throws
WirestateError If accessed before activation or after disposal.
Example
scope.unregisterQueryHandler("GET_DATE_NOW", this.onGetDateNow);unsubscribeFromEvent()
unsubscribeFromEvent(handler: EventHandler): void;Defined in: src/wirestate-core/container/wire-scope.ts:232
Unsubscribes a specific handler from the EventBus.
Parameters
| Parameter | Type | Description |
|---|---|---|
handler | EventHandler | The handler instance to remove. |
Returns
void
Throws
WirestateError If accessed before activation or after disposal.
Example
scope.unsubscribeFromEvent(this.onEvent);Properties
isDeprovisioned
readonly isDeprovisioned: Optional<boolean> = null;Defined in: src/wirestate-core/container/wire-scope.ts:62
Whether the scope has been removed from provider ownership.
Remarks
null means the scope has not reached a provider provision cycle yet. false means the scope is currently owned by a provider. true means the provider deprovisioned it.
isDisposed
readonly isDisposed: boolean = false;Defined in: src/wirestate-core/container/wire-scope.ts:52
Whether the scope was deactivated and disposed from the container.
Remarks
This becomes true after service deactivation, usually when an owned container is disposed. It remains false when only provider ownership ends.