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

34 lines
769 B
TypeScript
Raw Normal View History

2023-01-21 21:57:28 +01:00
import {pointerToPrism} from '../pointerToPrism'
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>(
pointerOrPrism: Prism<V> | Pointer<V>,
2021-06-18 13:05:06 +02:00
): Generator<V, void, void> {
let d
if (isPointer(pointerOrPrism)) {
d = pointerToPrism(pointerOrPrism) as Prism<V>
} else if (isPrism(pointerOrPrism)) {
d = pointerOrPrism
2021-06-18 13:05:06 +02:00
} else {
throw new Error(`Only pointers and prisms are supported`)
2021-06-18 13:05:06 +02:00
}
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()
}
}