From 91e793dbecdc32fe048bcba10fdb0a492c928cdc Mon Sep 17 00:00:00 2001 From: Piotr Rogowski Date: Mon, 24 Oct 2022 23:55:29 +0200 Subject: [PATCH] Add `onProgress` for msl parsing (#840) --- src/utils/ParserInterface.ts | 2 +- src/utils/logs/MslLogParser.ts | 9 ++++++--- src/workers/logParserWorker.ts | 10 +++++++++- 3 files changed, 16 insertions(+), 5 deletions(-) diff --git a/src/utils/ParserInterface.ts b/src/utils/ParserInterface.ts index b62ff89..ee584b2 100644 --- a/src/utils/ParserInterface.ts +++ b/src/utils/ParserInterface.ts @@ -1,5 +1,5 @@ export interface ParserInterface { - parse(): this; + parse(onProgress: (percent: number) => void): this; } export interface ParserConstructor { diff --git a/src/utils/logs/MslLogParser.ts b/src/utils/logs/MslLogParser.ts index 0d3c915..f23fcf4 100644 --- a/src/utils/logs/MslLogParser.ts +++ b/src/utils/logs/MslLogParser.ts @@ -22,12 +22,15 @@ class MslLogParser implements ParserInterface { this.raw = (new TextDecoder()).decode(buffer); } - public parse(): this { + public parse(onProgress: (percent: number) => void): this { let unitsIndex = 999; const lines = this.raw.trim().split('\n'); - for (let lineIndex = 0; lineIndex < lines.length; lineIndex++) { - const line = lines[lineIndex].trim(); + for (let lineIndex = 0; lineIndex < lines.length; lineIndex++) { + // eslint-disable-next-line no-bitwise + onProgress(~~(lineIndex / lines.length * 100)); + + const line = lines[lineIndex].trim(); if (line.startsWith('"')) { this.result.info += `${line.replaceAll('"', '').trim()}\n`; diff --git a/src/workers/logParserWorker.ts b/src/workers/logParserWorker.ts index a29f8cb..6178eb6 100644 --- a/src/workers/logParserWorker.ts +++ b/src/workers/logParserWorker.ts @@ -19,7 +19,15 @@ export interface WorkerOutput { // eslint-disable-next-line no-bitwise const elapsed = (t0: number): number => ~~(performance.now() - t0); -const parseMsl = (raw: ArrayBufferLike, t0: number): Result => new MslLogParser(raw).parse().getResult(); +const parseMsl = (raw: ArrayBufferLike, t0: number): Result => new MslLogParser(raw) + .parse((progress) => { + ctx.postMessage({ + type: 'progress', + progress, + elapsed: elapsed(t0), + } as WorkerOutput); + }) + .getResult(); const parseMlg = (raw: ArrayBufferLike, t0: number): Result => new Parser(raw).parse((progress) => { ctx.postMessage({