// The token renderer's pure half (T8, #71). No Pixi import: it turns the store's event stream into // the exact nodes a renderer must create, update or remove, so the "only changed entities" rule of // DESIGN 7.2 is observable and unit-tested instead of hidden inside a WebGL module. // // It mirrors the token slice's parsing (one wire shape, one parser) but keeps its own view of what // is currently on screen, which is what a renderer actually needs: a `token_gone` is only a removal // if a node exists, and a `token` frame for an existing node is an update, not an entry. import type { ReducerEvent } from '../reducer.ts' import { isHello } from '../protocol.ts' import type { TokenView } from '../slices/tokens.ts' import { tokenViewFromFrame } from '../slices/tokens.ts' export interface TokenPatch { /** Everything on screen must go (a `hello` is a full reset, 6.3). */ reset: boolean entered: TokenView[] updated: TokenView[] left: string[] } export class TokenBoard { #views = new Map() #reset = false #entered = new Map() #updated = new Map() #left = new Set() get size(): number { return this.#views.size } /** What is on screen now, in entry order. Read-only for the renderer's dataset/teardown. */ get views(): ReadonlyMap { return this.#views } /** * Start from the reducer's current view. The stage is created asynchronously, so the resync may * have run before a render module mounts; without this a token that arrived during `app.init` * would stay invisible until it changed. Every seeded token is an entry. */ seed(views: readonly TokenView[]): void { this.#views.clear() this.#entered.clear() this.#updated.clear() this.#left.clear() this.#reset = false for (const view of views) { this.#views.set(view.id, view) this.#entered.set(view.id, view) } } /** Fold one store event into the pending patch. Nothing is drawn here. */ apply(event: ReducerEvent): void { if (event.type !== 'frame') return const frame = event.frame if (isHello(frame)) { // A `hello` means "start clean" (6.3): the nodes all die, and anything still pending for the // previous connection is dropped rather than replayed onto the new view. this.#views.clear() this.#entered.clear() this.#updated.clear() this.#left.clear() this.#reset = true return } if (frame.t === 'token') { const view = tokenViewFromFrame(frame) if (!view) return const existing = this.#views.has(view.id) this.#views.set(view.id, view) if (existing) { // A token that entered and changed between two drains is still one entry, drawn once. if (this.#entered.has(view.id)) { this.#entered.set(view.id, view) return } this.#updated.set(view.id, view) this.#left.delete(view.id) } else { this.#updated.delete(view.id) this.#entered.set(view.id, view) } return } if (frame.t === 'token_gone') { const id = frame['id'] if (typeof id !== 'string' || !this.#views.has(id)) return this.#views.delete(id) this.#entered.delete(id) this.#updated.delete(id) this.#left.add(id) } } /** Take the pending patch: the renderer touches exactly these entities, then waits. */ drain(): TokenPatch { const patch: TokenPatch = { reset: this.#reset, entered: [...this.#entered.values()], updated: [...this.#updated.values()], left: [...this.#left], } this.#reset = false this.#entered.clear() this.#updated.clear() this.#left.clear() return patch } }