function createCountdown(
name,
{start, abort = createEvent(`${name}Reset`), timeout = 1000},
) {
const $working = createStore(true, {name: `${name}Working`})
const tick = createEvent(`${name}Tick`)
const timerFx = createEffect(`${name}Timer`).use(() => wait(timeout))
$working.on(abort, () => false).on(start, () => true)
guard({
source: start,
filter: timerFx.pending.map(is => !is),
target: tick,
})
forward({
from: tick,
to: timerFx,
})
const willTick = guard({
source: timerFx.done.map(({params}) => params - 1),
filter: seconds => seconds >= 0,
})
guard({
source: willTick,
filter: $working,
target: tick,
})
return {tick}
}
function wait(ms) {
return new Promise(resolve => {
setTimeout(resolve, ms)
})
}