From 1574a9bec9c20b02049559dda6b6018dbcf38618 Mon Sep 17 00:00:00 2001 From: Bryan Roe Date: Mon, 16 Aug 2021 12:57:17 -0700 Subject: [PATCH] 1. Added polyfill for Array.findIndex() if missing 2. Added try/catch around indentiers module --- agents/meshcore.js | 72 ++++++++++++++++++++++++++++++++-------------- 1 file changed, 50 insertions(+), 22 deletions(-) diff --git a/agents/meshcore.js b/agents/meshcore.js index 7a18bb05..b5d3a1a7 100644 --- a/agents/meshcore.js +++ b/agents/meshcore.js @@ -46,6 +46,27 @@ var MESHRIGHT_CHATNOTIFY = 16384; var MESHRIGHT_UNINSTALL = 32768; var MESHRIGHT_NODESKTOP = 65536; +try +{ + Object.defineProperty(Array.prototype, 'findIndex', { + value: function (func) + { + var i = 0; + for (i = 0; i < this.length; ++i) + { + if (func(this[i], i, this)) + { + return (i); + } + } + return (-1); + } + }); +} +catch (x) +{ +} + if (require('MeshAgent').ARCHID == null) { var id = null; switch (process.platform) { @@ -409,28 +430,35 @@ if ((require('fs').existsSync(process.cwd() + 'batterystate.txt')) && (require(' }, 1000); }); } -else { - // Setup normal battery monitoring - if (require('identifiers').isBatteryPowered && require('identifiers').isBatteryPowered()) { - require('MeshAgent')._battLevelChanged = function _battLevelChanged(val) { - _battLevelChanged.self._currentBatteryLevel = val; - _battLevelChanged.self.SendCommand({ action: 'battery', state: _battLevelChanged.self._currentPowerState, level: val }); - }; - require('MeshAgent')._battLevelChanged.self = require('MeshAgent'); - require('MeshAgent')._powerChanged = function _powerChanged(val) { - _powerChanged.self._currentPowerState = (val == 'AC' ? 'ac' : 'dc'); - _powerChanged.self.SendCommand({ action: 'battery', state: (val == 'AC' ? 'ac' : 'dc'), level: _powerChanged.self._currentBatteryLevel }); - }; - require('MeshAgent')._powerChanged.self = require('MeshAgent'); - require('MeshAgent').on('Connected', function (status) { - if (status == 0) { - require('power-monitor').removeListener('acdc', this._powerChanged); - require('power-monitor').removeListener('batteryLevel', this._battLevelChanged); - } else { - require('power-monitor').on('acdc', this._powerChanged); - require('power-monitor').on('batteryLevel', this._battLevelChanged); - } - }); +else +{ + try + { + // Setup normal battery monitoring + if (require('identifiers').isBatteryPowered && require('identifiers').isBatteryPowered()) { + require('MeshAgent')._battLevelChanged = function _battLevelChanged(val) { + _battLevelChanged.self._currentBatteryLevel = val; + _battLevelChanged.self.SendCommand({ action: 'battery', state: _battLevelChanged.self._currentPowerState, level: val }); + }; + require('MeshAgent')._battLevelChanged.self = require('MeshAgent'); + require('MeshAgent')._powerChanged = function _powerChanged(val) { + _powerChanged.self._currentPowerState = (val == 'AC' ? 'ac' : 'dc'); + _powerChanged.self.SendCommand({ action: 'battery', state: (val == 'AC' ? 'ac' : 'dc'), level: _powerChanged.self._currentBatteryLevel }); + }; + require('MeshAgent')._powerChanged.self = require('MeshAgent'); + require('MeshAgent').on('Connected', function (status) { + if (status == 0) { + require('power-monitor').removeListener('acdc', this._powerChanged); + require('power-monitor').removeListener('batteryLevel', this._battLevelChanged); + } else { + require('power-monitor').on('acdc', this._powerChanged); + require('power-monitor').on('batteryLevel', this._battLevelChanged); + } + }); + } + } + catch(e) + { } }