🎨 Moves widget update logic into mixin

This commit is contained in:
Alicia Sykes
2022-01-08 11:58:38 +00:00
parent 4a14b27cf3
commit e253a35b05
2 changed files with 33 additions and 27 deletions

View File

@@ -346,7 +346,6 @@ export default {
loading: false,
error: false,
errorMsg: null,
updater: null, // Stores interval
}),
computed: {
/* Returns the widget type, shows error if not specified */
@@ -361,27 +360,13 @@ export default {
widgetOptions() {
const options = this.widget.options || {};
const useProxy = !!this.widget.useProxy;
const updateInterval = this.widget.updateInterval || 0;
const updateInterval = this.widget.updateInterval || null;
return { useProxy, updateInterval, ...options };
},
/* A unique string to reference the widget by */
widgetRef() {
return `widget-${this.widgetType}-${this.index}`;
},
/* Returns either `false` or a number in ms to continuously update widget data */
updateInterval() {
const usersInterval = this.widget.updateInterval;
if (!usersInterval) return 0;
// If set to `true`, then default to 30 seconds
if (typeof usersInterval === 'boolean') return 30 * 1000;
// If set to a number, and within valid range, return user choice
if (typeof usersInterval === 'number'
&& usersInterval >= 10
&& usersInterval < 7200) {
return usersInterval * 1000;
}
return 0;
},
},
methods: {
/* Calls update data method on widget */
@@ -402,17 +387,6 @@ export default {
this.loading = loading;
},
},
mounted() {
// If continuous updates enabled, create interval
if (this.updateInterval) {
this.updater = setInterval(() => {
this.update();
}, this.updateInterval);
}
},
beforeDestroy() {
clearInterval(this.updater);
},
};
</script>