Skip to content

Commit

Permalink
🤝 Add method for setting customizable UI state
Browse files Browse the repository at this point in the history
  • Loading branch information
kierandrewett committed Dec 18, 2023
1 parent 81d059a commit c4e8cdb
Showing 1 changed file with 40 additions and 8 deletions.
48 changes: 40 additions & 8 deletions components/customizableui/BrowserCustomizable.sys.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,9 @@ BrowserCustomizable.prototype = {

/**
* The current stored customizable state
* @type {object}
*/
state: {},
state: null,

/**
* The element to render the customizable interface to
Expand All @@ -45,11 +46,46 @@ BrowserCustomizable.prototype = {
return this._root;
},

/**
* Updates the customizable UI state
* @param {object} data
* @param {boolean} [permanent]
*/
async setState(data, permanent = false) {
const newState = await this.internal.parseConfig(data);

if (!newState) {
throw new Error("Failed to parse customizable state.");
}

this.state = data;

if (permanent) {
Services.prefs.setStringPref(
Shared.customizableStatePref,
JSON.stringify(data)
);
}

return this.state;
},

/**
* Refetches and validates the customizable state
*/
async _updateState() {
const newState = await this.internal.parseConfig();
const serialized = Services.prefs.getStringPref(
Shared.customizableStatePref,
"{}"
);

try {
this.state = JSON.parse(serialized);
} catch (e) {
throw new Error("Failed to parse customizable state.");
}

const newState = await this.internal.parseConfig(this.state);

if (!newState) {
throw new Error("Failed to parse customizable state.");
Expand Down Expand Up @@ -110,7 +146,8 @@ BrowserCustomizable.prototype = {
*/
async _update(boot = false, reset = false) {
try {
await this._updateState();
if (!this.state) this._updateState();

await this._paint();
} catch (e) {
Shared.logger.error("Failure reading customizable state:", e);
Expand All @@ -123,11 +160,6 @@ BrowserCustomizable.prototype = {
e.toString().replace(/^Error: /, "")
);
}

if (boot) {
await this.internal.resetConfig();
await this._update(false, true);
}
}
},

Expand Down

0 comments on commit c4e8cdb

Please sign in to comment.