fix(tracker):fix in webworker messages handle

This commit is contained in:
Alex Kaminskii 2022-08-02 18:31:55 +02:00
parent 64d481aa4c
commit f14d341de9
2 changed files with 14 additions and 12 deletions

View file

@ -3,14 +3,14 @@ import * as Messages from '../common/messages.js';
import MessageEncoder from './MessageEncoder.js';
import PrimitiveEncoder from './PrimitiveEncoder.js';
const SIZE_RESERVED = 2;
const MAX_M_SIZE = (1 << (SIZE_RESERVED * 8)) - 1;
const SIZE_BYTES = 2;
const MAX_M_SIZE = (1 << (SIZE_BYTES * 8)) - 1;
export default class BatchWriter {
private nextIndex = 0;
private beaconSize = 2 * 1e5; // Default 200kB
private encoder = new MessageEncoder(this.beaconSize);
private readonly sizeEncoder = new PrimitiveEncoder(SIZE_RESERVED);
private readonly sizeEncoder = new PrimitiveEncoder(SIZE_BYTES);
private isEmpty = true;
constructor(
@ -36,11 +36,13 @@ export default class BatchWriter {
this.url,
];
this.encoder.encode(batchMetadata);
this.isEmpty = true;
}
private write(message: Message): boolean {
const e = this.encoder;
if (!e.uint(message[0]) || !e.skip(SIZE_RESERVED)) {
if (!e.uint(message[0]) || !e.skip(SIZE_BYTES)) {
// TODO: app.debug.log
return false;
}
const startOffset = e.getCurrentOffset();
@ -53,12 +55,13 @@ export default class BatchWriter {
return false;
}
this.sizeEncoder.checkpoint(); // TODO: separate checkpoint logic to an Encoder-inherit class
e.set(this.sizeEncoder.flush(), startOffset - SIZE_RESERVED);
e.set(this.sizeEncoder.flush(), startOffset - SIZE_BYTES);
e.checkpoint();
this.isEmpty = false;
this.nextIndex++;
}
// app.debug.log
return wasWritten;
}
@ -74,20 +77,21 @@ export default class BatchWriter {
if (message[0] === Messages.Type.SetPageLocation) {
this.url = message[1]; // .url
}
if (this.write(message)) {
return;
}
this.finaliseBatch();
while (!this.write(message)) {
this.finaliseBatch();
if (this.beaconSize === this.beaconSizeLimit) {
console.warn('OpenReplay: beacon size overflow. Skipping large message.');
console.warn('OpenReplay: beacon size overflow. Skipping large message.', message);
this.encoder.reset();
this.prepare();
this.isEmpty = true;
return;
}
// MBTODO: tempWriter for one message?
this.beaconSize = Math.min(this.beaconSize * 2, this.beaconSizeLimit);
this.encoder = new MessageEncoder(this.beaconSize);
this.prepare();
this.isEmpty = true;
}
}
@ -97,7 +101,6 @@ export default class BatchWriter {
}
this.onBatch(this.encoder.flush());
this.prepare();
this.isEmpty = true;
}
clean() {

View file

@ -61,13 +61,12 @@ self.onmessage = ({ data }: MessageEvent<WorkerMessageData>): any => {
}
if (Array.isArray(data)) {
// Message[]
if (!writer) {
throw new Error('WebWorker: writer not initialised. Service Should be Started.');
}
const w = writer;
// Message[]
data.forEach((message) => {
Object.assign(message, data);
if (message[0] === MType.SetPageVisibility) {
if (message[1]) {
// .hidden