// m08 stream: an event stream is a pure automaton (a Mealy machine). // Stream = { step: input => [outputs, nextStream] } // There is no hidden state: whatever a stream "remembers" lives in the next stream it returns. /** @import { Point, Input, Stream } from './types' */ // Sources may return themselves as the next stream, since they remember nothing. /** @type {Stream} */ export const keyDowns = { step: (i) => [i.tag === 'KeyDown' ? [i.key] : [], keyDowns] } /** @type {Stream} */ export const keyUps = { step: (i) => [i.tag === 'KeyUp' ? [i.key] : [], keyUps] } // (m13, given in src/) The position of every Click input, in canvas pixels. Like the key sources it remembers nothing. /** @type {Stream} */ export const clicks = { step: (i) => [i.tag === 'Click' ? [{ x: i.x, y: i.y }] : [], clicks] } // `last` is the time of the previous Time input. Multiples of ms in (last, t] fire, ascending. /** @type {(ms: number, last: number) => Stream} */ const intervalFrom = (ms, last) => ({ step: (i) => { if (i.tag !== 'Time') return [[], intervalFrom(ms, last)] const first = Math.floor(last / ms) + 1 const count = Math.max(0, Math.floor(i.t / ms) - first + 1) return [Array.from({ length: count }, (_, n) => (first + n) * ms), intervalFrom(ms, i.t)] }, }) /** @type {(ms: number) => Stream} */ export const interval = (ms) => intervalFrom(ms, 0) // Each combinator steps its inputs and wraps the *next* streams in the same combinator. /** @type {(s: Stream, f: (a: A) => B) => Stream} */ export const map = (s, f) => ({ step: (i) => { const [out, next] = s.step(i) return [out.map(f), map(next, f)] }, }) /** @type {(s: Stream, p: (a: A) => boolean) => Stream} */ export const filter = (s, p) => ({ step: (i) => { const [out, next] = s.step(i) return [out.filter(p), filter(next, p)] }, }) /** @type {(s: Stream, f: (a: A) => B | null) => Stream} */ export const filterMap = (s, f) => /** @type {any} */ (filter(map(s, f), (b) => b !== null)) /** @type {(a: Stream, b: Stream) => Stream} */ export const merge = (a, b) => ({ step: (i) => { const [outA, nextA] = a.step(i) const [outB, nextB] = b.step(i) return [[...outA, ...outB], merge(nextA, nextB)] }, }) // The accumulator travels in the arguments of the next scan, not in a variable. /** @type {(s: Stream, f: (acc: B, a: A) => B, z: B) => Stream} */ export const scan = (s, f, z) => ({ step: (i) => { const [out, next] = s.step(i) const accs = out.reduce((xs, o) => [...xs, f(xs.length === 0 ? z : xs[xs.length - 1], o)], []) return [accs, scan(next, f, accs.length === 0 ? z : accs[accs.length - 1])] }, }) // Only the first step is special; afterwards we are just `next`. /** @type {(s: Stream, v: A) => Stream} */ export const startWith = (s, v) => ({ step: (i) => { const [out, next] = s.step(i) return [[v, ...out], next] }, }) // (m10) `last` is the previous real time, `clock` the time the inner stream has been told. A long silence // (a background tab) moves `clock` by at most `maxGap`, so the inner stream never has to catch up on minutes of beats. /** @type {(s: Stream, maxGap: number, last: number, clock: number) => Stream} */ const clampFrom = (s, maxGap, last, clock) => ({ step: (i) => { if (i.tag !== 'Time') { const [out, next] = s.step(i) return [out, clampFrom(next, maxGap, last, clock)] } const now = clock + Math.min(Math.max(i.t - last, 0), maxGap) const [out, next] = s.step({ tag: 'Time', t: now }) return [out, clampFrom(next, maxGap, i.t, now)] }, }) /** @type {(s: Stream, maxGap: number) => Stream} */ export const clampTime = (s, maxGap) => clampFrom(s, maxGap, 0, 0) /** @type {(s: Stream, inputs: Input[]) => A[]} */ export const run = (s, inputs) => inputs.reduce( /** @type {(acc: [any[], typeof s], i: Input) => [any[], typeof s]} */ ([outs, stream], i) => { const [out, next] = stream.step(i) return [[...outs, ...out], next] }, [[], s], )[0]