Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cache settings in local storage #2073

Merged
merged 2 commits into from
May 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions js/configurator_main.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ const update = require('./globalUpdates');
const appUpdater = require('./appUpdater');
const CliAutoComplete = require('./CliAutoComplete');
const { SITLProcess } = require('./sitl');
const settingsCache = require('./settingsCache');

process.on('uncaughtException', function (error) {
if (process.env.NODE_ENV !== 'development') {
Expand Down Expand Up @@ -385,6 +386,9 @@ $(function() {
$('#demoModeReset').on('click', function () {
SITLProcess.deleteEepromFile('demo.bin');
});
$('#maintenanceFlushSettingsCache').on('click', function () {
settingsCache.flush();
});
function close_and_cleanup(e) {
if (e.type == 'click' && !$.contains($('div#options-window')[0], e.target) || e.type == 'keyup' && e.keyCode == 27) {
$(document).unbind('click keyup', close_and_cleanup);
Expand Down
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
47 changes: 47 additions & 0 deletions js/settingsCache.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
'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);
console.log('Settings cache flushed');
};

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;
6 changes: 6 additions & 0 deletions locale/en/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -5812,5 +5812,11 @@
},
"gsTelemetrySpeed": {
"message": "Speed"
},
"maintenance": {
"message": "Maintenance"
},
"maintenanceFlushSettingsCache": {
"message": "Flush settings cache"
}
}
10 changes: 10 additions & 0 deletions tabs/options.html
Original file line number Diff line number Diff line change
Expand Up @@ -87,5 +87,15 @@
</div>
</div>
</div>
<div class="options-section gui_box grey">
<div class="gui_box_titlebar">
<div class="spacer_box_title" data-i18n="maintenance"></div>
</div>
<div class="spacer_box settings">
<div class="default_btn" style="float: none; width: 200px;">
<a id="maintenanceFlushSettingsCache" href="#" i18n="maintenanceFlushSettingsCache"></a>
</div>
</div>
</div>
</div>
</div>
Loading