Skip to content
This repository has been archived by the owner on May 27, 2022. It is now read-only.

Commit

Permalink
DEV: Don't check the defaultState every time a widget is rendered
Browse files Browse the repository at this point in the history
This only happens in development mode but still ends up calling the
method unnecessarily.
  • Loading branch information
eviltrout committed Sep 9, 2021
1 parent 0532a5a commit 1205db8
Showing 1 changed file with 8 additions and 10 deletions.
18 changes: 8 additions & 10 deletions app/assets/javascripts/discourse/app/widgets/widget.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,16 +155,6 @@ export default class Widget {

this.init(this.attrs);

// Helps debug widgets
if (!isProduction()) {
const ds = this.defaultState(attrs);
if (typeof ds !== "object") {
throw new Error(`defaultState must return an object`);
} else if (Object.keys(ds).length > 0 && !this.key) {
throw new Error(`you need a key when using state in ${this.name}`);
}
}

if (this.name) {
const custom = _customSettings[this.name];
if (custom) {
Expand Down Expand Up @@ -195,7 +185,15 @@ export default class Widget {
if (prev && prev.key && prev.key === this.key) {
this.state = prev.state;
} else {
// Helps debug widgets
this.state = this.defaultState(this.attrs, this.state);
if (!isProduction()) {
if (typeof this.state !== "object") {
throw new Error(`defaultState must return an object`);
} else if (Object.keys(this.state).length > 0 && !this.key) {
throw new Error(`you need a key when using state in ${this.name}`);
}
}
}

// Sometimes we pass state down from the parent
Expand Down

0 comments on commit 1205db8

Please sign in to comment.