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'
|
2021-06-27 13:37:10 +02:00
|
|
|
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:59:03 +01:00
|
|
|
pointerOrPrism: Prism<V> | Pointer<V>,
|
2021-06-18 13:05:06 +02:00
|
|
|
): Generator<V, void, void> {
|
|
|
|
let d
|
2022-12-01 14:59:03 +01:00
|
|
|
if (isPointer(pointerOrPrism)) {
|
|
|
|
d = pointerToPrism(pointerOrPrism) as Prism<V>
|
|
|
|
} else if (isPrism(pointerOrPrism)) {
|
|
|
|
d = pointerOrPrism
|
2021-06-18 13:05:06 +02:00
|
|
|
} else {
|
2022-12-01 14:59:03 +01:00
|
|
|
throw new Error(`Only pointers and prisms are supported`)
|
2021-06-18 13:05:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const ticker = new Ticker()
|
|
|
|
|
2022-12-01 13:40:02 +01:00
|
|
|
const untap = d.onChange(ticker, (v) => {})
|
2021-06-18 13:05:06 +02:00
|
|
|
|
|
|
|
try {
|
|
|
|
while (true) {
|
|
|
|
ticker.tick()
|
|
|
|
|
|
|
|
yield d.getValue()
|
|
|
|
}
|
|
|
|
} finally {
|
|
|
|
untap()
|
|
|
|
}
|
|
|
|
}
|