openreplay/frontend/app/player/MessageDistributor/messages/MFileReader.ts
Delirium 4ebcff74e1
feat(ui): make ui able to load unprocessed session files (#652)
* feat(ui): make ui able to load unprocessed session files

* feat(ui): some lgos

* feat(ui): connect api, rewrite old code

* feat(ui): create testing ui functions

* feat(ui/player): add ability to jump back in time for assist

* feat(ui/player): rewrite for better readability

* fix(ui/player): small refactor for better readability

* fix(ui/player): fix private prop

* fix(ui/player): add tooltip

* feat(ui/player): create time calculating tooltip

* fix(player): fix message timestamp

* fix(ui/player): cleanup

* fix(ui/player): handle errors for unprocessed files as well

* fix(player): fix logged message

* fix(player): code review fixes

* fix(ui): fix circle color, fix button text

* fix(tracker): code review

* fix(player): small style fixes
2022-08-11 12:07:34 +02:00

77 lines
1.9 KiB
TypeScript

import type { Message } from './message';
import type { RawMessage } from './raw';
import logger from 'App/logger';
import RawMessageReader from './RawMessageReader';
// TODO: composition instead of inheritance
// needSkipMessage() and next() methods here use buf and p protected properties,
// which should be probably somehow incapsulated
export default class MFileReader extends RawMessageReader {
private pLastMessageID: number = 0
private currentTime: number
public error: boolean = false
constructor(data: Uint8Array, private startTime?: number) {
super(data)
}
private needSkipMessage(): boolean {
if (this.p === 0) return false
for (let i = 7; i >= 0; i--) {
if (this.buf[ this.p + i ] !== this.buf[ this.pLastMessageID + i ]) {
return this.buf[ this.p + i ] - this.buf[ this.pLastMessageID + i ] < 0
}
}
return false
}
private readRawMessage(): RawMessage | null {
this.skip(8)
try {
const msg = super.readMessage()
if (!msg) {
this.skip(-8)
}
return msg
} catch (e) {
this.error = true
logger.error("Read message error:", e)
return null
}
}
next(): [ Message, number] | null {
if (this.error || !this.hasNextByte()) {
return null
}
while (this.needSkipMessage()) {
const skippedMessage = this.readRawMessage()
if (!skippedMessage) {
return null
}
logger.log("Skipping message: ", skippedMessage)
}
this.pLastMessageID = this.p
const rMsg = this.readRawMessage()
if (!rMsg) {
return null
}
if (rMsg.tp === "timestamp") {
if (!this.startTime) {
this.startTime = rMsg.timestamp
}
this.currentTime = rMsg.timestamp - this.startTime
return this.next()
}
const msg = Object.assign(rMsg, {
time: this.currentTime,
_index: this.pLastMessageID,
})
return [msg, this.pLastMessageID]
}
}