theatre/packages/dataverse/src/derivations/iterateOver.ts
Aria Minaei f2bb24ef99 Change the signature of Derivation.changes() to Derivation.onChange()
This way, we no longer expose the `Tappable` interface so we can remove it later.
2023-01-04 20:49:43 +01:00

33 lines
823 B
TypeScript

import {valueDerivation} from '../Atom'
import type {Pointer} from '../pointer'
import {isPointer} from '../pointer'
import Ticker from '../Ticker'
import type {IDerivation} from './IDerivation'
import {isDerivation} from './IDerivation'
export default function* iterateOver<V>(
pointerOrDerivation: IDerivation<V> | Pointer<V>,
): Generator<V, void, void> {
let d
if (isPointer(pointerOrDerivation)) {
d = valueDerivation(pointerOrDerivation) as IDerivation<V>
} else if (isDerivation(pointerOrDerivation)) {
d = pointerOrDerivation
} else {
throw new Error(`Only pointers and derivations are supported`)
}
const ticker = new Ticker()
const untap = d.onChange(ticker, (v) => {})
try {
while (true) {
ticker.tick()
yield d.getValue()
}
} finally {
untap()
}
}