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

use watchers and data instead of computed props to memoize results #4372

Merged
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
81 changes: 51 additions & 30 deletions kolibri/core/assets/src/mixins/responsive-window.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,29 +115,30 @@ export default {
return {
windowWidth: undefined,
windowHeight: undefined,

/*
Implementing these as data controlled by watchers to work around
optimization issue: https://github.com/vuejs/vue/issues/8540

If that issue ever gets addressed, we should make them computed props.
*/
windowBreakpoint: undefined,
windowGutter: 16,
};
},
computed: {
windowBreakpoint() {
const SCROLL_BAR = 16;
if (this.windowWidth < 480) {
return 0;
} else if (this.windowWidth < 600) {
return 1;
} else if (this.windowWidth < 840) {
return 2;
} else if (this.windowWidth < 960 - SCROLL_BAR) {
return 3;
} else if (this.windowWidth < 1280 - SCROLL_BAR) {
return 4;
} else if (this.windowWidth < 1440 - SCROLL_BAR) {
return 5;
} else if (this.windowWidth < 1600 - SCROLL_BAR) {
return 6;
} else {
return 7;
}
watch: {
windowWidth() {
this._updateBreakpoint();
this._updateGutter();
},
windowHeight() {
this._updateGutter();
},
},
computed: {
/*
CAUTION: do not reference windowWidth or windowHeight in computed props.
*/
windowIsLarge() {
return this.windowBreakpoint > 2;
},
Expand All @@ -157,22 +158,42 @@ export default {
// windowIsLarge
return 12;
},
windowGutter() {
if (this.windowIsSmall) {
return 16;
}
// 16px when the smallest width of the device is < 600
if (this.windowBreakpoint < 4 && Math.min(this.windowWidth, this.windowHeight) < 600) {
return 16;
}
return 24;
},
},
methods: {
_updateWindow(metrics) {
this.windowWidth = metrics.width;
this.windowHeight = metrics.height;
},
_updateBreakpoint() {
const SCROLL_BAR = 16;
if (this.windowWidth < 480) {
this.windowBreakpoint = 0;
} else if (this.windowWidth < 600) {
this.windowBreakpoint = 1;
} else if (this.windowWidth < 840) {
this.windowBreakpoint = 2;
} else if (this.windowWidth < 960 - SCROLL_BAR) {
this.windowBreakpoint = 3;
} else if (this.windowWidth < 1280 - SCROLL_BAR) {
this.windowBreakpoint = 4;
} else if (this.windowWidth < 1440 - SCROLL_BAR) {
this.windowBreakpoint = 5;
} else if (this.windowWidth < 1600 - SCROLL_BAR) {
this.windowBreakpoint = 6;
} else {
this.windowBreakpoint = 7;
}
},
_updateGutter() {
if (this.windowIsSmall) {
this.windowGutter = 16;
} else if (this.windowBreakpoint < 4 && Math.min(this.windowWidth, this.windowHeight) < 600) {
// 16px when the smallest dimension of the window is < 600
this.windowGutter = 16;
} else {
this.windowGutter = 24;
}
},
},
mounted() {
addWindowListener(this._updateWindow);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

<div>
<KTextbox
:autofocus="true"
v-model="facilityName"
@blur="validateFacilityName"
:invalid="facilityNameIsInvalid"
Expand Down