class Saga
Defines a long-running business process that coordinates across events and time.
Sagas implement the Saga pattern for managing complex workflows that span multiple aggregates, external services, and time-based operations. They provide stateful event processing with support for timers, error handling, and process coordination.
TimeoutNames - Union type of timer names this saga can schedule InstanceData - Type of the saga's persistent state data
Key Features: - *\*Stateful Processing\**: Maintains state across multiple events - \**Timer Support\**: Schedule delays and recurring operations - \**Error Handling\**: Custom error handling and compensation logic - \**Event Correlation\**: Match events to specific saga instances - \**Parallel Processing\*\*: Configurable concurrency for high throughput
Properties
errorHandler
errorHandler: (saga: any, event: EventicleEvent, error: Error) => Promise<void>;
eventHandler
eventHandler: Map<string, {
config: HandlerConfig<any, InstanceData, TimeoutNames>;
handle: (saga: SagaInstance<TimeoutNames, InstanceData>, event: EventicleEvent) => Promise<void>;
}>;
Methods
on(eventName, config, handler)
Registers an event handler for existing saga instances.
When the specified event type is received, the handler will be called on any existing saga instances that match the event based on the matchInstance configuration.
| Name | Type | Description |
|---|---|---|
|
|
The event type to handle |
|
|
Configuration for instance matching and locking |
|
|
The function to execute for matching instances |
Returns |
|
on<T extends EventicleEvent>(eventName: string, config: HandlerConfig<T, InstanceData, TimeoutNames>, handler: (saga: SagaInstance<TimeoutNames, InstanceData>, event: T) => Promise<void>): Saga<TimeoutNames, InstanceData>;
onError(handler)
| Name | Type | Description |
|---|---|---|
|
|
|
Returns |
|
onError(handler: (saga: any, event: EventicleEvent, error: Error) => Promise<void>): Saga<TimeoutNames, InstanceData>;
onTimer(name, handle)
Register a handler for a timer triggered saga step.
This will be called on the timer.
No event is present.
| Name | Type | Description |
|---|---|---|
|
|
The name of the timer |
|
|
the async function to execute. |
Returns |
|
onTimer(name: TimeoutNames, handle: (saga: SagaInstance<TimeoutNames, InstanceData>) => Promise<void>): Saga<TimeoutNames, InstanceData>;
parallelEvents(val)
| Name | Type | Description |
|---|---|---|
|
|
|
Returns |
|
parallelEvents(val: number): Saga<TimeoutNames, InstanceData>;
startOn(eventName, config, handler)
Registers an event handler that can start new saga instances.
When the specified event type is received and no existing saga instance matches, a new saga instance will be created and the handler called. Only one startOn handler per event type is allowed.
| Name | Type | Description |
|---|---|---|
|
|
The event type that can start new saga instances |
|
|
Configuration for instance creation |
|
|
The function to execute when starting a new instance |
Returns |
|
startOn<T extends EventicleEvent>(eventName: string, config: StartHandlerConfig<T, InstanceData, TimeoutNames>, handler: (saga: SagaInstance<TimeoutNames, InstanceData>, event: T) => Promise<void>): Saga<TimeoutNames, InstanceData>;
subscribeStreams(streams)
| Name | Type | Description |
|---|---|---|
|
|
|
Returns |
|
subscribeStreams(streams: string[]): Saga<TimeoutNames, InstanceData>;