SingleShotGen<T, A>Yields its wrapped value exactly once through an IterableIterator.
When to use
Use to implement [Symbol.iterator]() on Effect-like types so they can be
yield*-ed inside generator functions, such as Effect.gen and
Option.gen.
Details
The first call to next() returns { value: self, done: false }. Every
subsequent call returns { value: a, done: true } where a is the argument
passed to next(). [Symbol.iterator]() returns a new SingleShotGen
wrapping the same value, so the outer type can be iterated multiple times.
Example (Yielding a wrapped value in a generator)
import { Utils } from "effect"
const gen = new Utils.SingleShotGen<string, number>("hello")
// First call yields the wrapped value
console.log(gen.next(0))
// { value: "hello", done: false }
// Second call signals completion with the provided value
console.log(gen.next(42))
// { value: 42, done: true }export class class SingleShotGen<T, A>class SingleShotGen {
called: boolean;
self: T;
next: (a: A) => IteratorResult<T, A>;
}
Yields its wrapped value exactly once through an IterableIterator.
When to use
Use to implement [Symbol.iterator]() on Effect-like types so they can be
yield*-ed inside generator functions, such as Effect.gen and
Option.gen.
Details
The first call to next() returns { value: self, done: false }. Every
subsequent call returns { value: a, done: true } where a is the argument
passed to next(). [Symbol.iterator]() returns a new SingleShotGen
wrapping the same value, so the outer type can be iterated multiple times.
Example (Yielding a wrapped value in a generator)
import { Utils } from "effect"
const gen = new Utils.SingleShotGen<string, number>("hello")
// First call yields the wrapped value
console.log(gen.next(0))
// { value: "hello", done: false }
// Second call signals completion with the provided value
console.log(gen.next(42))
// { value: 42, done: true }
SingleShotGen<function (type parameter) T in SingleShotGen<T, A>T, function (type parameter) A in SingleShotGen<T, A>A> implements interface IterableIterator<T, TReturn = any, TNext = any>Describes a user-defined
Iterator
that is also iterable.
IterableIterator<function (type parameter) T in SingleShotGen<T, A>T, function (type parameter) A in SingleShotGen<T, A>A> {
private SingleShotGen<T, A>.called: booleancalled = false
readonly SingleShotGen<T, A>.self: Tself: function (type parameter) T in SingleShotGen<T, A>T
constructor(self: Tself: function (type parameter) T in SingleShotGen<T, A>T) {
this.SingleShotGen<T, A>.self: Tself = self: Tself
}
/**
* Yields the stored value once, then completes with the value sent back in.
*
* **When to use**
*
* Use to advance a `SingleShotGen` through its single yield and completion
* step.
*
* @since 2.0.0
*/
SingleShotGen<T, A>.next(a: A): IteratorResult<T, A>Yields the stored value once, then completes with the value sent back in.
When to use
Use to advance a SingleShotGen through its single yield and completion
step.
next(a: Aa: function (type parameter) A in SingleShotGen<T, A>A): type IteratorResult<T, TReturn = any> =
| IteratorYieldResult<T>
| IteratorReturnResult<TReturn>
IteratorResult<function (type parameter) T in SingleShotGen<T, A>T, function (type parameter) A in SingleShotGen<T, A>A> {
return this.SingleShotGen<T, A>.called: booleancalled ?
({
IteratorReturnResult<A>.value: Avalue: a: Aa,
IteratorReturnResult<A>.done: truedone: true
}) :
(this.SingleShotGen<T, A>.called: booleancalled = true,
({
IteratorYieldResult<T>.value: Tvalue: this.SingleShotGen<T, A>.self: Tself,
IteratorYieldResult<T>.done?: false | undefineddone: false
}))
}
/**
* Creates a fresh single-shot iterator over the stored value.
*
* **When to use**
*
* Use to iterate the wrapped value again without reusing the consumed
* iterator state.
*
* @since 2.0.0
*/
[var Symbol: SymbolConstructorSymbol.SymbolConstructor.iterator: typeof Symbol.iteratorA method that returns the default iterator for an object. Called by the semantics of the
for-of statement.
iterator](): interface IterableIterator<T, TReturn = any, TNext = any>Describes a user-defined
Iterator
that is also iterable.
IterableIterator<function (type parameter) T in SingleShotGen<T, A>T, function (type parameter) A in SingleShotGen<T, A>A> {
return new constructor SingleShotGen<T, A>(self: T): SingleShotGen<T, A>Yields its wrapped value exactly once through an IterableIterator.
When to use
Use to implement [Symbol.iterator]() on Effect-like types so they can be
yield*-ed inside generator functions, such as Effect.gen and
Option.gen.
Details
The first call to next() returns { value: self, done: false }. Every
subsequent call returns { value: a, done: true } where a is the argument
passed to next(). [Symbol.iterator]() returns a new SingleShotGen
wrapping the same value, so the outer type can be iterated multiple times.
Example (Yielding a wrapped value in a generator)
import { Utils } from "effect"
const gen = new Utils.SingleShotGen<string, number>("hello")
// First call yields the wrapped value
console.log(gen.next(0))
// { value: "hello", done: false }
// Second call signals completion with the provided value
console.log(gen.next(42))
// { value: 42, done: true }
SingleShotGen<function (type parameter) T in SingleShotGen<T, A>T, function (type parameter) A in SingleShotGen<T, A>A>(this.SingleShotGen<T, A>.self: Tself)
}
}