Skip to content

Function: bindService()

ts
function bindService<T>(
   container: Container, 
   entry: Newable<T>, 
   options?: BindServiceOptions): void;

Defined in: src/wirestate-core/bind/bind-service.ts:92

Binds a service class as a Wirestate singleton.

Type Parameters

Type ParameterDescription
T extends objectService instance type.

Parameters

ParameterTypeDescription
containerContainerContainer to bind into.
entryNewable<T>Service class.
options?BindServiceOptionsBinding options.

Returns

void

Remarks

Use this for classes that should participate in Wirestate lifecycle.

The binding does four jobs:

  • Resolves one service instance per container.
  • Runs @OnActivated and @OnDeactivation.
  • Registers @OnEvent, @OnCommand, and @OnQuery handlers.
  • Tracks injected WireScope instances so stale async work can stop.

Example

typescript
import { Injectable, OnCommand, bindService, createContainer } from "@wirestate/core";

@Injectable()
class SessionService {
  private active = false;

  @OnCommand("LOGIN")
  public login(): void {
    this.active = true;
  }

  public isActive(): boolean {
    return this.active;
  }
}

const container = createContainer();

bindService(container, SessionService);

const service = container.get(SessionService);