tyneq / createGeneratorOperator
Function: createGeneratorOperator()
createGeneratorOperator<
TSource,TArgs,TResult>(config):void
Defined in: src/plugin/registration/createGeneratorOperator.ts:59
Registers an operator using a generator function.
The generator receives the source as an Iterable<T> and any user arguments. This is the simplest functional registration path and works for both streaming and buffering use cases:
- Streaming: yield elements one at a time as they arrive. The downstream sequence only pulls the next element when needed.
- Buffering: collect all input first, then yield the transformed output. Pass
category: "buffer"to tell the query plan that this operator materialises the entire upstream before yielding.
For class-based enumerators with custom stateful logic, prefer createOperator.
Type Parameters
| Type Parameter |
|---|
TSource |
TArgs extends unknown[] |
TResult |
Parameters
| Parameter | Type | Description |
|---|---|---|
config | { name: string; category?: "streaming" | "buffer"; generator: (source, ...args) => IterableIterator<TResult>; validate?: (...args) => void; source?: OperatorSource; } | - |
config.name | string | Method name to expose on every sequence. |
config.category? | "streaming" | "buffer" | "streaming" (default) or "buffer". |
config.generator | (source, ...args) => IterableIterator<TResult> | Generator that yields transformed elements from source. |
config.validate? | (...args) => void | Optional eager validation for user-supplied arguments. |
config.source? | OperatorSource | - |
Returns
void
Examples
ts
import { createGeneratorOperator } from "tyneq/plugin";
createGeneratorOperator({
name: "everyOther",
*generator(source) {
let skip = false;
for (const item of source) {
if (!skip) yield item;
skip = !skip;
}
}
});ts
import { createGeneratorOperator } from "tyneq/plugin";
createGeneratorOperator({
name: "sortedBy",
category: "buffer",
*generator(source, keyFn: (x: number) => number) {
const items = [...source].sort((a, b) => keyFn(a) - keyFn(b));
for (const item of items) yield item;
}
});