Add simple cache for json settings

This commit is contained in:
Charles Gagnon
2025-02-05 12:15:31 -05:00
parent fcb1a39708
commit 3ad58f17d6
3 changed files with 33 additions and 5 deletions

View File

@@ -210,12 +210,18 @@ export const PanelManager = class {
'changed::panel-anchors',
'changed::stockgs-keep-top-panel',
],
() => this._reset(),
(settings,settingChanged) => {
PanelSettings.clearCache(settingChanged)
this._reset()
}
],
[
SETTINGS,
'changed::panel-element-positions',
() => this._updatePanelElementPositions(),
() => {
PanelSettings.clearCache('panel-element-positions')
this._updatePanelElementPositions()
}
],
[
SETTINGS,

View File

@@ -17,10 +17,30 @@
import * as Pos from './panelPositions.js'
// cache is a different object in the settings dialog (gjs process)
// and in gnome-shell (gnome-shell process)
let cache = {}
export function clearCache(setting) {
if (setting) {
cache[setting] = null
return
}
cache = {}
}
/** Return object representing a settings value that is stored as JSON. */
export function getSettingsJson(settings, setting) {
try {
return JSON.parse(settings.get_string(setting))
if (cache[setting])
return cache[setting]
let res = JSON.parse(settings.get_string(setting))
cache[setting] = res
return res
} catch (e) {
console.log('Error parsing positions: ' + e.message)
}
@@ -30,6 +50,7 @@ export function setSettingsJson(settings, setting, value) {
try {
const json = JSON.stringify(value)
settings.set_string(setting, json)
cache[setting] = value
} catch (e) {
console.log('Error serializing setting: ' + e.message)
}

View File

@@ -473,9 +473,10 @@ const Preferences = class {
monitors.forEach(
(m) => (panelElementPositionsSettings[m] = newPanelElementPositions),
)
this._settings.set_string(
PanelSettings.setSettingsJson(
this._settings,
'panel-element-positions',
JSON.stringify(panelElementPositionsSettings),
panelElementPositionsSettings,
)
}