theatre/packages/dataverse/src/prisms/iterateOver.ts

34 lines
789 B
TypeScript
Raw Normal View History

2022-12-01 14:26:17 +01:00
import {pointerToPrism} from '../Atom'
2021-06-18 13:05:06 +02:00
import type {Pointer} from '../pointer'
import {isPointer} from '../pointer'
2021-06-18 13:05:06 +02:00
import Ticker from '../Ticker'
2022-12-01 14:24:09 +01:00
import type {Prism} from './Interface'
import {isPrism} from './Interface'
2021-06-18 13:05:06 +02:00
export default function* iterateOver<V>(
2022-12-01 14:20:50 +01:00
pointerOrDerivation: Prism<V> | Pointer<V>,
2021-06-18 13:05:06 +02:00
): Generator<V, void, void> {
let d
if (isPointer(pointerOrDerivation)) {
2022-12-01 14:26:17 +01:00
d = pointerToPrism(pointerOrDerivation) as Prism<V>
2022-12-01 14:22:49 +01:00
} else if (isPrism(pointerOrDerivation)) {
2021-06-18 13:05:06 +02:00
d = pointerOrDerivation
} else {
throw new Error(`Only pointers and derivations are supported`)
}
const ticker = new Ticker()
const untap = d.onChange(ticker, (v) => {})
2021-06-18 13:05:06 +02:00
try {
while (true) {
ticker.tick()
yield d.getValue()
}
} finally {
untap()
}
}