From cb33301cc748c49a71c615652d9bbfdabdbc4b47 Mon Sep 17 00:00:00 2001 From: Jonas Malaco Date: Wed, 28 Apr 2021 05:05:44 -0300 Subject: [PATCH 1/2] commandLineUtil: toggle _updated after the callback returns The callback can be used to process the received data a single time, and in that case this._updated should only be toggled after the callback has completed, since other methods may depend on its side effects. In other scenarios the order does not matter (either way). --- freon@UshakovVasilii_Github.yahoo.com/commandLineUtil.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/freon@UshakovVasilii_Github.yahoo.com/commandLineUtil.js b/freon@UshakovVasilii_Github.yahoo.com/commandLineUtil.js index 0a288b2..0d970d0 100644 --- a/freon@UshakovVasilii_Github.yahoo.com/commandLineUtil.js +++ b/freon@UshakovVasilii_Github.yahoo.com/commandLineUtil.js @@ -26,8 +26,8 @@ var CommandLineUtil = class { } catch (e) { logError(e); } finally { - this._updated = true; callback(); + this._updated = true; } }); } catch(e){ From 824282e0d60da1127d114ba33a3944511f0a1769 Mon Sep 17 00:00:00 2001 From: Jonas Malaco Date: Tue, 20 Apr 2021 19:31:29 -0300 Subject: [PATCH 2/2] sensorsUtil: prevent re-parsing of the JSON output --- .../sensorsUtil.js | 43 +++++++++++-------- 1 file changed, 26 insertions(+), 17 deletions(-) diff --git a/freon@UshakovVasilii_Github.yahoo.com/sensorsUtil.js b/freon@UshakovVasilii_Github.yahoo.com/sensorsUtil.js index 50ff2bc..62fa580 100644 --- a/freon@UshakovVasilii_Github.yahoo.com/sensorsUtil.js +++ b/freon@UshakovVasilii_Github.yahoo.com/sensorsUtil.js @@ -12,6 +12,30 @@ var SensorsUtil = class extends CommandLineUtil.CommandLineUtil { this._argv = path ? [path, '-A', '-j'] : null; } + // Avoid parsing the data more than once. + execute(callback) { + super.execute(() => { + let data = []; + try { + data = JSON.parse(this._output.join('')); + } catch (e) { + try { + // fix for wrong lm_sensors output + // https://github.com/UshakovVasilii/gnome-shell-extension-freon/issues/114#issuecomment-491613545 + let lineRemoved = this._output.filter(l => l.trim() !== ',').join('\n'); + let errorRemoved = lineRemoved.replace(/ERROR.*Can't read/, ""); + errorRemoved = errorRemoved.replace(/ERROR.*I\/O error/, ""); + data = JSON.parse(errorRemoved); + } catch (e) { + global.log(e.toString()); + return []; + } + } + this._data = data; + callback(); + }); + } + get temp() { return this._parseGenericSensorsOutput(/^temp\d+_input/, 'temp'); } @@ -37,25 +61,10 @@ var SensorsUtil = class extends CommandLineUtil.CommandLineUtil { } _parseSensorsOutput(sensorFilter, sensorType, gpuFlag) { - if(!this._output) + if(!this._data) return []; - let data = [] - try { - data = JSON.parse(this._output.join('')); - } catch (e) { - try { - // fix for wrong lm_sensors output - // https://github.com/UshakovVasilii/gnome-shell-extension-freon/issues/114#issuecomment-491613545 - let lineRemoved = this._output.filter(l => l.trim() !== ',').join('\n'); - let errorRemoved = lineRemoved.replace(/ERROR.*Can't read/, ""); - errorRemoved = errorRemoved.replace(/ERROR.*I\/O error/, ""); - data = JSON.parse(errorRemoved); - } catch (e) { - global.log(e.toString()); - return []; - } - } + const data = this._data; let sensors = []; for (var chipset in data) {