Loop Engine

Examples

Event Streaming

Event streaming exposes loop lifecycle data for audit logs, notifications, and observability pipelines.

Event subscriptions

1import { LOOP_EVENT_TYPES } from "@loop-engine/events"
2 
3const unsubscribe = eventBus.subscribe(async (event) => {
4 switch (event.type) {
5 case LOOP_EVENT_TYPES.LOOP_STARTED:
6 case LOOP_EVENT_TYPES.TRANSITION_EXECUTED:
7 case LOOP_EVENT_TYPES.GUARD_FAILED:
8 case LOOP_EVENT_TYPES.LOOP_COMPLETED:
9 case LOOP_EVENT_TYPES.LOOP_ERROR:
10 case LOOP_EVENT_TYPES.OUTCOME_RECORDED:
11 console.log(event.type, event.aggregateId)
12 break
13 }
14})

Audit log accumulation

1const audit: Array<{ at: string; type: string; aggregateId: string }> = []
2 
3eventBus.subscribe(async (event) => {
4 audit.push({
5 at: event.occurredAt,
6 type: event.type,
7 aggregateId: String(event.aggregateId)
8 })
9})

Timeline and replay

1import { buildTimeline, replayLoop } from "@loop-engine/observability"
2 
3const instance = await engine.getState(aggregateId("EXP-1"))
4const history = await engine.getHistory(aggregateId("EXP-1"))
5 
6if (instance) {
7 const timeline = buildTimeline(instance, history)
8 const replay = replayLoop(definition, history)
9 console.log(timeline.durationMs, replay.valid)
10}

Full source

  • https://github.com/loopengine/loop-examples/tree/main/event-streaming