| 104 | } |
| 105 | |
| 106 | export class CastFrameQueue implements IFrameQueue { |
| 107 | private _endFrame: IFrame |
| 108 | private _frames: Array<IFrame> = [] |
| 109 | |
| 110 | constructor( |
| 111 | cast: ICastObject, |
| 112 | step: number = DEFAULT_FRAME_EVENTS_STEP, |
| 113 | snapshotFn: FrameSnapshotFn = DEFAULT_FRAME_SNAPSHOT_FN |
| 114 | ) { |
| 115 | const duration = cast.header.duration |
| 116 | const events = cast.events |
| 117 | |
| 118 | this._frames = new Array<IFrame>(2 + Math.ceil(events.length / step)) |
| 119 | this._frames[0] = START_FRAME |
| 120 | this._frames[this._frames.length - 1] = this._endFrame = new NullFrame(duration, duration) |
| 121 | |
| 122 | for (let start = 0, n = 1, prev: IFrame = START_FRAME; start < events.length; start += step) { |
| 123 | const end = start + step |
| 124 | const slice = new Slice<ICastEvent>(cast.events, start, end) // TODO: Do a benchmark of [].slice vs Slice |
| 125 | const startTime = slice.get(0).time |
| 126 | const endTime = end < events.length ? events[end].time : duration |
| 127 | const f = new CastEventsFrame(startTime, endTime, slice, snapshotFn) |
| 128 | f.prev = prev |
| 129 | this._frames[n++] = prev = f |
| 130 | } |
| 131 | |
| 132 | this._endFrame.prev = this._frames[this._frames.length - 2] |
| 133 | } |
| 134 | public isEnd(frame: IFrame): boolean { return frame === this._endFrame } |
| 135 | public len(): number { return this._frames.length - 2 } |
| 136 | public frame(time: number): IFrame { |
| 137 | if (time < 0) { throw new Error('Time must not be negative') } |
| 138 | if (!this.len()) { throw new Error('Empty frames') } |
| 139 | // bisearch |
| 140 | const frames = this._frames |
| 141 | let min = 1, mid = 0, max = this.len() |
| 142 | if (time >= this._frames[max].endTime) { return this._endFrame } |
| 143 | while (max >= min) { |
| 144 | mid = (min + max) >> 1 |
| 145 | const f = frames[mid] |
| 146 | if (time >= f.endTime) { |
| 147 | min = mid + 1 |
| 148 | } else if (time < f.startTime) { |
| 149 | max = mid - 1 |
| 150 | } else { |
| 151 | return f |
| 152 | } |
| 153 | } |
| 154 | return NULL_FRAME |
| 155 | } |
| 156 | public dispose(): void { this._frames = [] } |
| 157 | } |
nothing calls this directly
no outgoing calls
no test coverage detected