Skip to content

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

typescript
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

ts
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

ts
new WireScope(container: Optional<Container>): WireScope;

Defined in: src/wirestate-core/container/wire-scope.ts:64

Parameters

ParameterType
containerOptional<Container>

Returns

WireScope

Methods

emitEvent()

ts
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 ParameterDefault typeDescription
P-Type of the event payload.
T extends EventTypeEventTypeType of the event identifier.

Parameters

ParameterTypeDescription
typeTEvent identifier.
payload?POptional data associated with the event.
from?unknownOptional source identifier (defaults to current scope).

Returns

void

Throws

WirestateError If accessed before activation or after disposal.

Example

typescript
scope.emitEvent("VALUE_CHANGED", { value: "abcd" });

executeCommand()

ts
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 ParameterDefault typeDescription
RunknownType of the command result.
DunknownType of the command payload.
T extends CommandTypeCommandTypeType of the command identifier.

Parameters

ParameterTypeDescription
typeTCommand identifier.
data?DPayload for the command.

Returns

CommandDescriptor<R>

A descriptor with status and task.

Throws

WirestateError If accessed before activation or after disposal.

Throws

WirestateError If no command handler is registered.

Example

typescript
const descriptor: CommandDescriptor = scope.executeCommand("LOGOUT");

await descriptor.task;

executeOptionalCommand()

ts
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 ParameterDefault typeDescription
RunknownType of the command result.
DunknownType of the command payload.
T extends CommandTypeCommandTypeType of the command identifier.

Parameters

ParameterTypeDescription
typeTCommand identifier.
data?DPayload for the command.

Returns

Optional<CommandDescriptor<R>>

A CommandDescriptor or null.

Throws

WirestateError If accessed before activation or after disposal.

Example

typescript
const descriptor: CommandDescriptor | null = scope.executeOptionalCommand("CLEANUP_CACHE");

if (descriptor) {
  await descriptor.task;
}

getContainer()

ts
getContainer(): Container;

Defined in: src/wirestate-core/container/wire-scope.ts:93

Provides direct access to the underlying Inversify Container.

Returns

Container

The active Container.

Throws

WirestateError If accessed before activation or after disposal.

Example

typescript
const container: Container = scope.getContainer();

container.bind("TOKEN").toConstantValue(42);

getSeed()

Call Signature

ts
getSeed<T>(): T;

Defined in: src/wirestate-core/container/wire-scope.ts:515

Reads the shared seed object.

Type Parameters
Type ParameterDescription
T extends AnyObjectExpected 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
typescript
interface GlobalSeed {
  apiUrl: string;
}

const seeds: GlobalSeed = scope.getSeed();

Call Signature

ts
getSeed<T>(seed: SeedKey): Optional<T>;

Defined in: src/wirestate-core/container/wire-scope.ts:534

Reads a targeted seed value.

Type Parameters
Type ParameterDescription
TExpected type of the seed value.
Parameters
ParameterTypeDescription
seedSeedKeyLookup 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
typescript
const apiUrl: string = scope.getSeed("API_URL");

queryData()

ts
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 ParameterDefault typeDescription
RunknownType of the query result.
DunknownType of the query data (payload).
T extends QueryTypeQueryTypeType of the query identifier.

Parameters

ParameterTypeDescription
typeTQuery identifier.
data?DInput 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

typescript
const user: User = scope.queryData("GET_USER", { id: 1 });

queryDataAsync()

ts
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 ParameterDefault typeDescription
RunknownType of the query result.
DunknownType of the query data (payload).
T extends QueryTypeQueryTypeType of the query identifier.

Parameters

ParameterTypeDescription
typeTQuery identifier.
data?DInput 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

typescript
const user: User = await scope.queryDataAsync("GET_USER", { id: 1 });

queryOptionalData()

ts
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 ParameterDefault typeDescription
RunknownType of the query result.
DunknownType of the query data (payload).
T extends QueryTypeQueryTypeType of the query identifier.

Parameters

ParameterTypeDescription
typeTQuery identifier.
data?DInput data for the query handler.

Returns

Optional<R>

The query result or null.

Throws

WirestateError If accessed before activation or after disposal.

Example

typescript
const config: Config | null = scope.queryOptionalData("GET_CONFIG");

queryOptionalDataAsync()

ts
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 ParameterDefault typeDescription
RunknownType of the query result.
DunknownType of the query data (payload).
T extends QueryTypeQueryTypeType of the query identifier.

Parameters

ParameterTypeDescription
typeTQuery identifier.
data?DInput 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

typescript
const config: Config | null = await scope.queryOptionalDataAsync("GET_CONFIG");

registerCommandHandler()

ts
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 ParameterDefault typeDescription
DunknownType of the command payload.
RunknownType of the command result.

Parameters

ParameterTypeDescription
typeCommandTypeCommand identifier.
handlerCommandHandler<D, R>The handler function.

Returns

CommandUnregister

A function to unregister the handler.

Throws

WirestateError If accessed before activation or after disposal.

Example

typescript
scope.registerCommandHandler("LOG_ERROR", (error) => {
  console.error(error);
});

registerQueryHandler()

ts
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 ParameterDefault typeDescription
DunknownType of the query data (payload).
RunknownType of the query result.

Parameters

ParameterTypeDescription
typeQueryTypeQuery identifier.
handlerQueryHandler<D, R>The handler function.

Returns

QueryUnregister

A function to unregister the handler.

Throws

WirestateError If accessed before activation or after disposal.

Example

typescript
scope.registerQueryHandler("GET_DATE_NOW", () => new Date());

resolve()

ts
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 ParameterDescription
TType of the service or value to resolve.

Parameters

ParameterTypeDescription
injectionIdServiceIdentifier<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

typescript
const service: MyService = scope.resolve(MyService);

resolveOptional()

ts
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 ParameterDescription
TType of the service or value to resolve.

Parameters

ParameterTypeDescription
injectionIdServiceIdentifier<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

typescript
const logger: Logger | null = scope.resolveOptional(Logger);

logger?.info("Resolved optionally");

subscribeToEvent()

ts
subscribeToEvent(handler: EventHandler): EventUnsubscriber;

Defined in: src/wirestate-core/container/wire-scope.ts:214

Subscribes to all events on the EventBus.

Parameters

ParameterTypeDescription
handlerEventHandlerFunction called for every emitted event.

Returns

EventUnsubscriber

A function to unsubscribe.

Throws

WirestateError If accessed before activation or after disposal.

Example

typescript
const unsubscribe: EventUnsubscriber = scope.subscribeToEvent((event) => {
  console.log("Event received:", event);
});

unregisterCommandHandler()

ts
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 ParameterDefault typeDescription
DunknownType of the command payload.
RunknownType of the command result.

Parameters

ParameterTypeDescription
typeCommandTypeCommand identifier.
handlerCommandHandler<D, R>The handler instance to remove.

Returns

void

Throws

WirestateError If accessed before activation or after disposal.

Example

typescript
scope.unregisterCommandHandler("LOG_ERROR", this.handleLogError);

unregisterQueryHandler()

ts
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 ParameterDefault typeDescription
DunknownType of the query data (payload).
RunknownType of the query result.

Parameters

ParameterTypeDescription
typeQueryTypeQuery identifier.
handlerQueryHandler<D, R>The handler instance to remove.

Returns

void

Throws

WirestateError If accessed before activation or after disposal.

Example

typescript
scope.unregisterQueryHandler("GET_DATE_NOW", this.onGetDateNow);

unsubscribeFromEvent()

ts
unsubscribeFromEvent(handler: EventHandler): void;

Defined in: src/wirestate-core/container/wire-scope.ts:232

Unsubscribes a specific handler from the EventBus.

Parameters

ParameterTypeDescription
handlerEventHandlerThe handler instance to remove.

Returns

void

Throws

WirestateError If accessed before activation or after disposal.

Example

typescript
scope.unsubscribeFromEvent(this.onEvent);

Properties

isDeprovisioned

ts
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

ts
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.