From 67d4b06a1d1b65074c52759e197f2b369f9ede15 Mon Sep 17 00:00:00 2001 From: Mykola Mokhnach Date: Tue, 2 Jul 2024 18:32:20 +0200 Subject: [PATCH] fix: Fix recent entry retrieval (#753) --- lib/logcat.js | 25 ++++++++++--------------- package.json | 2 +- test/unit/logcat-specs.js | 6 +++--- 3 files changed, 14 insertions(+), 19 deletions(-) diff --git a/lib/logcat.js b/lib/logcat.js index d0b588f2..7f50b3cf 100644 --- a/lib/logcat.js +++ b/lib/logcat.js @@ -159,21 +159,17 @@ class Logcat extends EventEmitter { resolve(); } }); - this.proc.on('lines-stderr', (lines) => { - for (const line of lines) { - if (!started && EXECVP_ERR_PATTERN.test(line)) { - log.error('Logcat process failed to start'); - return reject(new Error(`Logcat process failed to start. stderr: ${line}`)); - } - this.outputHandler(line, 'STDERR: '); + this.proc.on('line-stderr', (line) => { + if (!started && EXECVP_ERR_PATTERN.test(line)) { + log.error('Logcat process failed to start'); + return reject(new Error(`Logcat process failed to start. stderr: ${line}`)); } + this.outputHandler(line, 'STDERR: '); resolve(); }); - this.proc.on('lines-stdout', (lines) => { + this.proc.on('line-stdout', (line) => { + this.outputHandler(line); resolve(); - for (const line of lines) { - this.outputHandler(line); - } }); await this.proc.start(0); // resolve after a timeout, even if no output was recorded @@ -189,9 +185,8 @@ class Logcat extends EventEmitter { */ outputHandler (logLine, prefix = '') { const timestamp = Date.now(); - /** @type {number} */ let recentIndex = -1; - for (const key of this.logs.rkeys()) { + for (const key of this.logs.keys()) { recentIndex = key; break; } @@ -228,7 +223,7 @@ class Logcat extends EventEmitter { const result = []; /** @type {number?} */ let recentLogIndex = null; - for (const [index, [message, timestamp]] of this.logs.entries()) { + for (const [index, [message, timestamp]] of /** @type {Generator} */ (this.logs.rentries())) { if (this.logIndexSinceLastRequest && index > this.logIndexSinceLastRequest || !this.logIndexSinceLastRequest) { recentLogIndex = index; @@ -247,7 +242,7 @@ class Logcat extends EventEmitter { getAllLogs () { /** @type {LogEntry[]} */ const result = []; - for (const [message, timestamp] of this.logs.values()) { + for (const [message, timestamp] of /** @type {Generator} */ (this.logs.rvalues())) { result.push(toLogEntry(message, timestamp)); } return result; diff --git a/package.json b/package.json index 925efbe2..8fc13a2f 100644 --- a/package.json +++ b/package.json @@ -57,7 +57,7 @@ "lru-cache": "^10.0.0", "semver": "^7.0.0", "source-map-support": "^0.x", - "teen_process": "^2.1.10" + "teen_process": "^2.2.0" }, "devDependencies": { "@appium/eslint-config-appium-ts": "^0.x", diff --git a/test/unit/logcat-specs.js b/test/unit/logcat-specs.js index 769d2f03..2ff9c31c 100644 --- a/test/unit/logcat-specs.js +++ b/test/unit/logcat-specs.js @@ -29,7 +29,7 @@ describe('logcat', withMocks({teen_process}, function (mocks) { .onFirstCall() .returns(conn); setTimeout(function () { - conn.emit('lines-stdout', ['- beginning of system\r']); + conn.emit('line-stdout', '- beginning of system\r'); }, 0); await logcat.startCapture({ format: 'brief', @@ -46,7 +46,7 @@ describe('logcat', withMocks({teen_process}, function (mocks) { .onFirstCall() .returns(conn); setTimeout(function () { - conn.emit('lines-stderr', ['execvp()']); + conn.emit('line-stderr', 'execvp()'); }, 0); await logcat.startCapture().should.eventually.be.rejectedWith('Logcat'); }); @@ -58,7 +58,7 @@ describe('logcat', withMocks({teen_process}, function (mocks) { .onFirstCall() .returns(conn); setTimeout(function () { - conn.emit('lines-stderr', ['something']); + conn.emit('line-stderr', 'something'); }, 0); await logcat.startCapture().should.eventually.not.be.rejectedWith('Logcat'); });