Datasets:
Download code/evaluation-code/ent400-a06a1576/apps/console/web/src/clock.ts from VEHwang/cocogni4d: direct link, hf CLI and curl.
- Browser
- Download file 4.93 kB
-
https://huggingface.co/datasets/VEHwang/cocogni4d/resolve/main/code/evaluation-code/ent400-a06a1576/apps/console/web/src/clock.ts
- Command line
-
hf download hf://datasets/VEHwang/cocogni4d/code/evaluation-code/ent400-a06a1576/apps/console/web/src/clock.ts
-
curl -L -o clock.ts https://huggingface.co/datasets/VEHwang/cocogni4d/resolve/main/code/evaluation-code/ent400-a06a1576/apps/console/web/src/clock.ts
4.93 kB
| export type ClockSnapshot = {time: number; playing: boolean; speed: number}; | |
| export function seekFromInput(clock: ReviewClock, input: {value: string}) { | |
| // Pausing publishes synchronously, and Controls writes the current clock | |
| // time back into the range. Capture the user's value before that write. | |
| const requestedTime = Number(input.value); | |
| clock.setPlaying(false); | |
| clock.seek(requestedTime); | |
| } | |
| export class ReviewClock { | |
| private time: number; | |
| private playing = false; | |
| private speed = 1; | |
| private listeners = new Set<(value: ClockSnapshot) => void>(); | |
| private holds = new Set<object>(); | |
| private pendingAdvanceMs = 0; | |
| private driver: object | null = null; | |
| private driverListeners = new Set<() => void>(); | |
| readonly start: number; | |
| readonly end: number; | |
| readonly controlHz: number; | |
| constructor(span: [number, number], controlHz: number) { | |
| if (!(span[1] > span[0]) || !(controlHz > 0)) throw Error('无效时钟'); | |
| this.start = this.time = span[0]; this.end = span[1]; this.controlHz = controlHz; | |
| } | |
| snapshot(): ClockSnapshot { return {time: this.time, playing: this.playing, speed: this.speed}; } | |
| subscribe(callback: (value: ClockSnapshot) => void): () => void { | |
| this.listeners.add(callback); callback(this.snapshot()); | |
| return () => {this.listeners.delete(callback);}; | |
| } | |
| private publish() { for (const callback of this.listeners) callback(this.snapshot()); } | |
| seek(t: number) { | |
| if (!Number.isFinite(t)) return; | |
| this.pendingAdvanceMs = 0; | |
| this.time = Math.min(this.end, Math.max(this.start, t)); this.publish(); | |
| } | |
| setPlaying(playing: boolean) { | |
| if (playing && this.time === this.end) this.time = this.start; | |
| if (!playing) this.pendingAdvanceMs = 0; | |
| this.playing = playing; this.publish(); | |
| } | |
| hold(owner: object) { this.holds.add(owner); } | |
| release(owner: object) { this.holds.delete(owner); } | |
| isDriven(): boolean { return this.driver !== null; } | |
| subscribeDriver(callback: () => void): () => void { | |
| this.driverListeners.add(callback); | |
| return () => {this.driverListeners.delete(callback);}; | |
| } | |
| drive(owner: object) { | |
| this.driver = owner; this.pendingAdvanceMs = 0; | |
| for (const callback of this.driverListeners) callback(); | |
| } | |
| undrive(owner: object) { | |
| if (this.driver !== owner) return; | |
| this.driver = null; | |
| for (const callback of this.driverListeners) callback(); | |
| } | |
| present(owner: object, time: number) { | |
| if (this.driver !== owner || !Number.isFinite(time)) return; | |
| this.time = Math.min(this.end, Math.max(this.start, time)); | |
| this.publish(); | |
| } | |
| setSpeed(speed: number) { | |
| if (![1, 4, 10].includes(speed)) throw Error('无效倍速'); | |
| this.speed = speed; this.publish(); | |
| } | |
| step(seconds: number) { this.seek(this.time + seconds); } | |
| frame(direction: number) { | |
| this.setPlaying(false); | |
| const current = (this.time - this.start) * this.controlHz; | |
| // From a dragged off-grid time, go to the next/previous control tick; | |
| // rounding first would skip a tick on one side of the midpoint. | |
| const tick = direction > 0 ? Math.floor(current + 1e-8) + 1 : Math.ceil(current - 1e-8) - 1; | |
| this.seek(this.start + tick / this.controlHz); | |
| } | |
| advance(milliseconds: number) { | |
| if (!this.playing || this.driver || !Number.isFinite(milliseconds) || milliseconds < 0) return; | |
| this.pendingAdvanceMs += milliseconds * this.speed; | |
| if (this.holds.size) return; | |
| const delta = this.pendingAdvanceMs; this.pendingAdvanceMs = 0; | |
| this.time = Math.min(this.end, this.time + delta / 1000); | |
| if (this.time === this.end) this.playing = false; | |
| this.publish(); | |
| } | |
| } | |
| export type FrameScheduler = {request: (callback: (now: number) => void) => number; cancel: (id: number) => void}; | |
| export function animate(clock: ReviewClock, scheduler: FrameScheduler = { | |
| request: callback => requestAnimationFrame(callback), cancel: id => cancelAnimationFrame(id), | |
| }): () => void { | |
| let id: number | null = null, previous: number | null = null, active = false; | |
| const tick = (now: number) => { | |
| if (!active) return; | |
| id = null; | |
| if (previous !== null) clock.advance(now - previous); | |
| if (active) {previous = now; id = scheduler.request(tick);} | |
| }; | |
| const update = () => { | |
| const playing = clock.snapshot().playing && !clock.isDriven(); | |
| if (playing && !active) { | |
| active = true; previous = null; id = scheduler.request(tick); | |
| } else if (!playing && active) { | |
| active = false; previous = null; | |
| if (id !== null) scheduler.cancel(id); | |
| id = null; | |
| } | |
| }; | |
| // A video driver publishes presented times itself. Keep ownership changes | |
| // separate from time publications: its constructor may still be in flight. | |
| const unsubscribeDriver = clock.subscribeDriver(update); | |
| const unsubscribe = clock.subscribe(update); | |
| return () => {unsubscribe(); unsubscribeDriver(); active = false; if (id !== null) scheduler.cancel(id);}; | |
| } | |