// m11 pong app: the same wiring as app.js (streams in, drawings out), reusing the course's modules. import { initialPong, PongAction } from './model.js' import { pongUpdate } from './rules.js' import { pongView } from './view.js' import { map as mapIO, randomSeed, drawScene } from '../io.js' import { keyDowns, keyUps, interval, map, filterMap, merge, scan, startWith } from '../stream.js' export const FRAME_MS_PONG = 30 const keys = { ArrowUp: 'Up', w: 'Up', W: 'Up', ArrowDown: 'Down', s: 'Down', S: 'Down', } // hasOwn, not `key in keys`: "constructor" and "toString" are inherited, not keys we mapped. export const pongKey = (isDown, key) => Object.hasOwn(keys, key) ? (isDown ? PongAction.Press : PongAction.Release)(keys[key]) : (key === 'r' || key === 'R') && isDown ? PongAction.Restart : null export const pongActions = merge( map(interval(FRAME_MS_PONG), () => PongAction.Frame), merge( filterMap(keyDowns, (key) => pongKey(true, key)), filterMap(keyUps, (key) => pongKey(false, key)))) export const pongStates = (seed, actions) => startWith(scan(actions, pongUpdate, initialPong(seed)), initialPong(seed)) export const pongMain = mapIO(randomSeed, (seed) => map(pongStates(seed, pongActions), (state) => drawScene(pongView(state))))