diff --git a/js/fc.js b/js/fc.js index 9f56d5a31..0d5d4861b 100644 --- a/js/fc.js +++ b/js/fc.js @@ -78,7 +78,6 @@ var FC = { MIXER_CONFIG: null, BATTERY_CONFIG: null, OUTPUT_MAPPING: null, - SETTINGS: null, BRAKING_CONFIG: null, SAFEHOMES: null, BOARD_ALIGNMENT: null, @@ -570,8 +569,6 @@ var FC = { this.OUTPUT_MAPPING = new OutputMappingCollection(); - this.SETTINGS = {}; - this.SAFEHOMES = new SafehomeCollection(); this.RATE_DYNAMICS = { diff --git a/js/msp/MSPHelper.js b/js/msp/MSPHelper.js index 3e0c66067..0c672b25b 100644 --- a/js/msp/MSPHelper.js +++ b/js/msp/MSPHelper.js @@ -20,6 +20,7 @@ const { FwApproach } = require('./../fwApproach'); const Waypoint = require('./../waypoint'); const mspDeduplicationQueue = require('./mspDeduplicationQueue'); const mspStatistics = require('./mspStatistics'); +const settingsCache = require('./../settingsCache'); var mspHelper = (function () { var self = {}; @@ -3060,9 +3061,12 @@ var mspHelper = (function () { }; self._getSetting = function (name) { - if (FC.SETTINGS[name]) { - return Promise.resolve(FC.SETTINGS[name]); + + const storedSetting = settingsCache.get(name); + if (typeof storedSetting !== 'undefined') { + return Promise.resolve(storedSetting); } + var data = []; self._encodeSettingReference(name, null, data); return MSP.promise(MSPCodes.MSP2_COMMON_SETTING_INFO, data).then(function (result) { @@ -3109,7 +3113,7 @@ var mspHelper = (function () { } setting.table = { values: values }; } - FC.SETTINGS[name] = setting; + settingsCache.set(name, setting); return setting; }); } diff --git a/js/settingsCache.js b/js/settingsCache.js new file mode 100644 index 000000000..fcd17957e --- /dev/null +++ b/js/settingsCache.js @@ -0,0 +1,46 @@ +'use strict'; + +const Store = require('electron-store'); +const store = new Store(); +const FC = require('./fc'); + +var settingsCache = (function() { + + let publicScope = {}; + let privateScope = {}; + + const SETTINGS_KEY = 'settings'; + + privateScope.getSetingKey = function(settingName) { + return FC.CONFIG.target + '_' + FC.CONFIG.flightControllerVersion + '_' + settingName; + } + + publicScope.flush = function() { + store.delete(SETTINGS_KEY); + }; + + publicScope.get = function(settingName) { + let settings = store.get(SETTINGS_KEY, null); + + if (settings === null) { + return undefined; + } + let setting = settings[privateScope.getSetingKey(settingName)]; + return setting; + }; + + publicScope.set = function(settingName, value) { + let settings = store.get(SETTINGS_KEY, null); + + if (settings === null) { + settings = {}; + } + + settings[privateScope.getSetingKey(settingName)] = value; + store.set(SETTINGS_KEY, settings); + }; + + return publicScope; +}()); + +module.exports = settingsCache; \ No newline at end of file