| 33 | export const START_FRAME: IFrame = NULL_FRAME |
| 34 | |
| 35 | export class CastEventsFrame implements IFrame { |
| 36 | private _prev: IFrame | null = null |
| 37 | private _snapshotCache: string | null = null |
| 38 | |
| 39 | constructor( |
| 40 | public readonly startTime: number, |
| 41 | public readonly endTime: number, |
| 42 | private _events: Slice<ICastEvent>, |
| 43 | private _snapshotFn: FrameSnapshotFn = DEFAULT_FRAME_SNAPSHOT_FN |
| 44 | ) { |
| 45 | if (!_events.len()) { throw new Error('Invalid frame: empty events') } |
| 46 | if ((startTime < 0) || ((endTime - startTime) <= 0)) { // TODO: re-evaluate if endTime - startTime can be ZERO |
| 47 | throw new Error('Invalid frame: inccorrect time or size') |
| 48 | } |
| 49 | if (_events.get(0).time >= endTime) { throw new Error('Invalid frame: invalid events') } |
| 50 | } |
| 51 | public set prev(f: IFrame | null) { |
| 52 | if (f !== this._prev) { |
| 53 | this._prev = f |
| 54 | this._snapshotCache = null |
| 55 | } |
| 56 | } |
| 57 | public get prev(): IFrame | null { |
| 58 | return this._prev |
| 59 | } |
| 60 | public duration(): number { |
| 61 | return this.endTime - this.startTime |
| 62 | } |
| 63 | data(endTime: number, startTime: number = -1): string { |
| 64 | if ((endTime < this.startTime) || (endTime >= this.endTime)) { |
| 65 | throw new Error(`Cannot get data of time(${endTime})`) |
| 66 | } |
| 67 | const tmp: string[] = [] |
| 68 | for (let i = 0; i < this._events.len(); i++) { |
| 69 | const ev = this._events.get(i) |
| 70 | if (ev.time > endTime) { break } |
| 71 | if (startTime < ev.time && ev.time <= endTime) { |
| 72 | tmp.push(ev.data) |
| 73 | } |
| 74 | } |
| 75 | return tmp.join('') |
| 76 | } |
| 77 | snapshot(): string { |
| 78 | if (this._snapshotCache !== null) { |
| 79 | return this._snapshotCache |
| 80 | } |
| 81 | const tmp: string[] = new Array<string>(this._events.len()) |
| 82 | for (let i = 0; i < this._events.len(); i++) { |
| 83 | tmp[i] = this._events.get(i).data |
| 84 | } |
| 85 | const ret = (this.prev ? this._snapshotFn(this.prev.snapshot() + tmp.join('')) : tmp.join('')) |
| 86 | return this._snapshotCache = ret |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | const DEFAULT_FRAME_EVENTS_STEP = 30 |
| 91 |
nothing calls this directly
no outgoing calls
no test coverage detected