From df25ec7f2ed6a1499c3bbf640a2324402d130e56 Mon Sep 17 00:00:00 2001 From: Tomas Chmelevskij Date: Wed, 4 Jan 2023 13:10:56 +0100 Subject: [PATCH] Use esm modules for session storage (#3191) * use esm for session storage * refactor release checker and loader to modules --- src/js/SessionStorage.js | 100 ++++++++++++++++++-------------- src/js/main.js | 1 + src/js/release_checker.js | 10 +++- src/js/release_loader.js | 10 ++-- src/js/tabs/firmware_flasher.js | 12 ++-- src/main.html | 3 - 6 files changed, 78 insertions(+), 58 deletions(-) diff --git a/src/js/SessionStorage.js b/src/js/SessionStorage.js index b25f395e..d4ce1af0 100644 --- a/src/js/SessionStorage.js +++ b/src/js/SessionStorage.js @@ -1,45 +1,59 @@ -'use strict'; +/** + * Gets one or more items from sessionStorage + * @param {string | string[]} key string or array of strings + * @returns {object} + */ +export function get(key) { + let result = {}; + if (Array.isArray(key)) { + key.forEach(function (element) { + try { + result = { ...result, ...JSON.parse(sessionStorage.getItem(element)) }; + } catch (e) { + console.error(e); + } + }); + } else { + const keyValue = sessionStorage.getItem(key); + if (keyValue) { + try { + result = JSON.parse(keyValue); + } catch (e) { + console.error(e); + } + } + } -const SessionStorage = { - // key can be one string, or array of strings - get: function(key) { - let result = {}; - if (Array.isArray(key)) { - key.forEach(function (element) { - try { - result = {...result, ...JSON.parse(sessionStorage.getItem(element))}; - } catch (e) { - console.error(e); - } - }); - } else { - const keyValue = sessionStorage.getItem(key); - if (keyValue) { - try { - result = JSON.parse(keyValue); - } catch (e) { - console.error(e); - } - } - } + return result; +} - return result; - }, - set: function(input) { - Object.keys(input).forEach(function (element) { - const tmpObj = {}; - tmpObj[element] = input[element]; - try { - sessionStorage.setItem(element, JSON.stringify(tmpObj)); - } catch (e) { - console.error(e); - } - }); - }, - remove: function(item) { - sessionStorage.removeItem(item); - }, - clear: function() { - sessionStorage.clear(); - }, -}; +/** + * Save dictionary of key/value pairs to sessionStorage + * @param {object} input object which keys are strings and values are serializable objects + */ +export function set(input) { + Object.keys(input).forEach(function (element) { + const tmpObj = {}; + tmpObj[element] = input[element]; + try { + sessionStorage.setItem(element, JSON.stringify(tmpObj)); + } catch (e) { + console.error(e); + } + }); +} + +/** + * Remove item from sessionStorage + * @param {string} item key to remove from storage + */ +export function remove(item) { + sessionStorage.removeItem(item); +} + +/** + * Clear sessionStorage + */ +export function clear() { + sessionStorage.clear(); +} diff --git a/src/js/main.js b/src/js/main.js index 2d363b79..9daf36e6 100644 --- a/src/js/main.js +++ b/src/js/main.js @@ -2,6 +2,7 @@ import '../components/init.js'; import { i18n } from './localization.js'; import GUI from './gui.js'; import { get as getConfig, set as setConfig } from './ConfigStorage.js'; +import ReleaseChecker from './release_checker.js'; $(document).ready(function () { diff --git a/src/js/release_checker.js b/src/js/release_checker.js index 73f08aef..d9818597 100644 --- a/src/js/release_checker.js +++ b/src/js/release_checker.js @@ -1,4 +1,6 @@ -'use strict'; +import GUI from "./gui"; +import { i18n } from "./localization"; +import { get as getStorage, set as setStorage } from "./SessionStorage"; const ReleaseChecker = function (releaseName, releaseUrl) { const self = this; @@ -11,7 +13,7 @@ const ReleaseChecker = function (releaseName, releaseUrl) { ReleaseChecker.prototype.loadReleaseData = function (processFunction) { const self = this; - const result = SessionStorage.get([self._releaseLastUpdateTag, self._releaseDataTag]); + const result = getStorage([self._releaseLastUpdateTag, self._releaseDataTag]); const releaseDataTimestamp = $.now(); const cacheReleaseData = result[self._releaseDataTag]; const cachedReleaseLastUpdate = result[self._releaseLastUpdateTag]; @@ -23,7 +25,7 @@ ReleaseChecker.prototype.loadReleaseData = function (processFunction) { const data = {}; data[self._releaseDataTag] = releaseData; data[self._releaseLastUpdateTag] = releaseDataTimestamp; - SessionStorage.set(data); + setStorage(data); self._processReleaseData(releaseData, processFunction); }).fail(function (data) { @@ -54,3 +56,5 @@ ReleaseChecker.prototype._processReleaseData = function (releaseData, processFun processFunction(); } }; + +export default ReleaseChecker; diff --git a/src/js/release_loader.js b/src/js/release_loader.js index 075e7a24..faa722f0 100644 --- a/src/js/release_loader.js +++ b/src/js/release_loader.js @@ -1,6 +1,8 @@ -'use strict'; +import GUI from "./gui"; +import { i18n } from "./localization"; +import { get as getStorage, set as setStorage } from "./SessionStorage"; -class ReleaseLoader { +export default class ReleaseLoader { constructor (url) { this._url = url; @@ -12,7 +14,7 @@ class ReleaseLoader { const dataTag = `${url}_Data`; const cacheLastUpdateTag = `${url}_LastUpdate`; - const result = SessionStorage.get([cacheLastUpdateTag, dataTag]); + const result = getStorage([cacheLastUpdateTag, dataTag]); const dataTimestamp = $.now(); const cachedData = result[dataTag]; const cachedLastUpdate = result[cacheLastUpdateTag]; @@ -33,7 +35,7 @@ class ReleaseLoader { const object = {}; object[dataTag] = info; object[cacheLastUpdateTag] = $.now(); - SessionStorage.set(object); + setStorage(object); onSuccess(info); }).fail(xhr => { GUI.log(i18n.getMessage('buildServerLoadFailed', [url, `HTTP ${xhr.status}`])); diff --git a/src/js/tabs/firmware_flasher.js b/src/js/tabs/firmware_flasher.js index 3c870560..e1e791a9 100644 --- a/src/js/tabs/firmware_flasher.js +++ b/src/js/tabs/firmware_flasher.js @@ -1,6 +1,8 @@ import { i18n } from '../localization'; import GUI from '../gui'; import { get as getConfig, set as setConfig } from '../ConfigStorage'; +import { get as getStorage, set as setStorage } from '../SessionStorage'; +import ReleaseLoader from '../release_loader'; const firmware_flasher = { targets: null, @@ -142,7 +144,7 @@ firmware_flasher.initialize = function (callback) { TABS.firmware_flasher.targets = targets; - result = SessionStorage.get('selected_board'); + result = getStorage('selected_board'); if (result.selected_board) { const selected = targets.find(t => t.target === result.selected_board); $('select[name="board"]').val(selected ? result.selected_board : 0).trigger('change'); @@ -373,7 +375,7 @@ firmware_flasher.initialize = function (callback) { if (!GUI.connect_lock) { if (target !== '0') { - SessionStorage.set({'selected_board': target}); + setStorage({'selected_board': target}); } self.selectedBoard = target; @@ -940,10 +942,10 @@ firmware_flasher.initialize = function (callback) { function setAcknowledgementTimestamp() { const storageObj = {}; storageObj[storageTag] = Date.now(); - SessionStorage.set(storageObj); + setStorage(storageObj); } - result = SessionStorage.get(storageTag); + result = getStorage(storageTag); if (!result[storageTag] || Date.now() - result[storageTag] > DAY_MS) { showAcknowledgementDialog(setAcknowledgementTimestamp); @@ -1153,7 +1155,7 @@ firmware_flasher.showDialogVerifyBoard = function (selected, verified, onAbort, if (!dialogVerifyBoard.hasAttribute('open')) { dialogVerifyBoard.showModal(); $('#dialog-verify-board-abort-confirmbtn').click(function() { - SessionStorage.set({'selected_board': FC.CONFIG.boardName}); + setStorage({'selected_board': FC.CONFIG.boardName}); dialogVerifyBoard.close(); onAbort(); }); diff --git a/src/main.html b/src/main.html index 26c70739..55c8757b 100644 --- a/src/main.html +++ b/src/main.html @@ -82,7 +82,6 @@ - @@ -106,8 +105,6 @@ - -