@sozai/generator
v0.2.1
Published
Async generator utilities
Maintainers
Readme
@sozai/generator
Bridges EventEmitter channels and ReadableStreams into typed async generators.
Installation
pnpm add @sozai/generatorUsage
import { EventEmitter } from '@sozai/event'
import { fromEmitter, consume } from '@sozai/generator'
type Events = { tick: number }
const emitter = new EventEmitter<Events>()
const controller = new AbortController()
// Iterate an event channel as an async generator, then drive it to completion.
const ticks = fromEmitter(emitter, 'tick', { signal: controller.signal })
const done = consume(ticks, (n) => console.log('tick:', n), controller.signal)
await emitter.emit('tick', 1)
await emitter.emit('tick', 2)
controller.abort()
await done.catch(() => {})Also provides fromStream for consuming a ReadableStream as a generator — see the generator reference (part of the sozai:dataflow skill) for the full API.
