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 Parameter | Description |
|---|---|
T extends object | Service instance type. |
Parameters
| Parameter | Type | Description |
|---|---|---|
container | Container | Container to bind into. |
entry | Newable<T> | Service class. |
options? | BindServiceOptions | Binding 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
@OnActivatedand@OnDeactivation. - Registers
@OnEvent,@OnCommand, and@OnQueryhandlers. - Tracks injected
WireScopeinstances 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);