Running Loops
createLoopSystem
createLoopSystem(options)
Factory from @loop-engine/sdk:
1createLoopSystem(options: {2 loops: LoopDefinition[]3 store?: LoopStore4 guards?: GuardRegistry5 signals?: boolean6 registry?: LoopRegistry7}): Promise<{8 engine: LoopEngine9 store: LoopStore10 eventBus: InMemoryEventBus11 signals?: SignalRegistry12}>loopsis required and becomes the in-memory set of definitions.storedefaults tomemoryStore().guardsdefaults todefaultRegistryfrom@loop-engine/guards.signals: truematerializes aSignalRegistryfor spec registration/validation; use the returnedeventBusto observeloop.*andloop.signal.receivedevents in this release.registryis the optional loop catalog client (LoopRegistry); ifregistry.list()fails, startup falls back toloops[].
Pattern 1: zero-config
1"cmt">// @no-typecheck2import { createLoopSystem } from '@loop-engine/sdk'3 4const { engine, eventBus } = await createLoopSystem({ loops: [definition] })Pattern 2: custom store (PostgreSQL adapter)
1"cmt">// @no-typecheck2import { createLoopSystem } from '@loop-engine/sdk'3import { postgresStore } from '@loop-engine/adapter-postgres'4import { Pool } from 'pg'5 6const pool = new Pool({ connectionString: process.env.DATABASE_URL })7const { engine } = await createLoopSystem({8 loops: [definition],9 store: postgresStore(pool)10})Pattern 3: signals enabled
SignalRegistry in 1.0.0-rc.0 is for spec registration/validation. React to live signal flow through the returned eventBus (for example, loop.signal.received).
1"cmt">// @no-typecheck2import { createLoopSystem } from '@loop-engine/sdk'3 4const { signals, eventBus } = await createLoopSystem({5 loops: [definition],6 signals: true7})8 9"cmt">// Optional: register or inspect specs on `signals` (instance of `SignalRegistry`).10void signals?.list()11 12eventBus.subscribe(async (event) => {13 if (event.type === "loop.signal.received") {14 console.log(event.signal, event.aggregateId)15 }16})