class SagaInstance
Represents a single running instance of a saga workflow.
SagaInstance contains the runtime state and execution context for a specific saga execution. Each instance tracks its data, manages timers, and provides methods for saga lifecycle management.
TimeoutNames - Union type of timer names this saga can schedule T - Type of the saga's persistent data
Properties
Methods
endSaga(preserveInstanceData)
Marks the saga instance as completed and schedules it for cleanup.
Once a saga is ended, it will not receive any more events or timer callbacks. The instance data can optionally be preserved for debugging or audit purposes.
| Name | Type | Description |
|---|---|---|
|
|
Whether to keep instance data after completion |
Returns |
|
endSaga(preserveInstanceData?: boolean): void;
get(name)
Retrieves a piece of data from the saga instance state.
| Name | Type | Description |
|---|---|---|
|
|
The key of the data to retrieve |
Returns |
|
The value associated with the key |
get<K extends keyof T>(name: K): T[K];
removeTimer(name)
| Name | Type | Description |
|---|---|---|
|
|
|
Returns |
|
removeTimer(name: TimeoutNames): void;
set(name, value)
Sets a piece of data in the saga instance state.
The value must be JSON-serializable as saga state is persisted. The 'id' field is protected and cannot be modified.
| Name | Type | Description |
|---|---|---|
|
|
The key to set |
|
|
The value to store (must be JSON-serializable) |
Returns |
|
set(name: keyof T, value: any): void;
upsertTimer(name, config)
Create (or overwrite) a timer to call. Can be either a simple timer (millis to wait), or a cron timer.
If the timer is no longer wanted, it must be removed by calling
| Name | Type | Description |
|---|---|---|
|
|
The timer to call |
|
|
|
Returns |
|
upsertTimer(name: TimeoutNames, config: {
isCron: true;
crontab: string;
} | {
isCron: false;
timeout: number;
}): void;