File size: 4,925 Bytes
5afd2f7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
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);};
}