Skip to content

Commit

Permalink
Cache settings in local storage
Browse files Browse the repository at this point in the history
  • Loading branch information
DzikuVx committed May 12, 2024
1 parent b1f5f32 commit e1c3024
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 6 deletions.
3 changes: 0 additions & 3 deletions js/fc.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -570,8 +569,6 @@ var FC = {

this.OUTPUT_MAPPING = new OutputMappingCollection();

this.SETTINGS = {};

this.SAFEHOMES = new SafehomeCollection();

this.RATE_DYNAMICS = {
Expand Down
10 changes: 7 additions & 3 deletions js/msp/MSPHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {};
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -3109,7 +3113,7 @@ var mspHelper = (function () {
}
setting.table = { values: values };
}
FC.SETTINGS[name] = setting;
settingsCache.set(name, setting);
return setting;
});
}
Expand Down
46 changes: 46 additions & 0 deletions js/settingsCache.js
Original file line number Diff line number Diff line change
@@ -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;

0 comments on commit e1c3024

Please sign in to comment.