Skip to content

Utility Helpers

Two helpers ship with @requence/service. Both exist for the same reason: a continuous generator has to stop when the task is stopped, and wiring ctx.terminated into every loop by hand is easy to forget.

Wraps any Iterable or AsyncIterable so that iteration stops automatically when ctx.terminated resolves.

import { createService, wrapIterable } from '@requence/service'
createService('1.0.0', async function* (ctx) {
for await (const event of wrapIterable(eventStream)) {
yield processEvent(event)
}
})

Creates an async-iterable emitter that integrates with ctx.terminated. Push values in from outside — an event listener, a callback, a webhook — and iterate them inside the generator. The iterator exits by itself when the task is terminated.

import { asyncEventEmitter, createService } from '@requence/service'
createService('1.0.0', async function* (ctx) {
const emitter = asyncEventEmitter<string>()
externalSource.on('data', (value) => emitter.push(value))
for await (const value of emitter) {
yield { value }
}
})

It is also the usual way to hold a act() actor open while it waits for a surface interaction.