🎨 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, loading: false,
error: false, error: false,
errorMsg: null, errorMsg: null,
updater: null, // Stores interval
}), }),
computed: { computed: {
/* Returns the widget type, shows error if not specified */ /* Returns the widget type, shows error if not specified */
@ -361,27 +360,13 @@ export default {
widgetOptions() { widgetOptions() {
const options = this.widget.options || {}; const options = this.widget.options || {};
const useProxy = !!this.widget.useProxy; const useProxy = !!this.widget.useProxy;
const updateInterval = this.widget.updateInterval || 0; const updateInterval = this.widget.updateInterval || null;
return { useProxy, updateInterval, ...options }; return { useProxy, updateInterval, ...options };
}, },
/* A unique string to reference the widget by */ /* A unique string to reference the widget by */
widgetRef() { widgetRef() {
return `widget-${this.widgetType}-${this.index}`; 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: { methods: {
/* Calls update data method on widget */ /* Calls update data method on widget */
@ -402,17 +387,6 @@ export default {
this.loading = loading; 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> </script>

View File

@ -17,11 +17,22 @@ const WidgetMixin = {
data: () => ({ data: () => ({
progress: new ProgressBar({ color: 'var(--progress-bar)' }), progress: new ProgressBar({ color: 'var(--progress-bar)' }),
overrideProxyChoice: false, overrideProxyChoice: false,
overrideUpdateInterval: null,
disableLoader: false, // Prevent ever showing the loader disableLoader: false, // Prevent ever showing the loader
updater: null, // Stores interval
}), }),
/* When component mounted, fetch initial data */ /* When component mounted, fetch initial data */
mounted() { mounted() {
this.fetchData(); this.fetchData();
if (this.updateInterval) {
this.continuousUpdates();
this.disableLoader = true;
}
},
beforeDestroy() {
if (this.updater) {
clearInterval(this.updater);
}
}, },
computed: { computed: {
proxyReqEndpoint() { proxyReqEndpoint() {
@ -31,6 +42,23 @@ const WidgetMixin = {
useProxy() { useProxy() {
return this.options.useProxy || this.overrideProxyChoice; return this.options.useProxy || this.overrideProxyChoice;
}, },
/* Returns either a number in ms to continuously update widget data. Or 0 for no updates */
updateInterval() {
const usersInterval = this.options.updateInterval;
if (usersInterval === null && this.overrideUpdateInterval) {
return this.overrideUpdateInterval * 1000;
}
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 >= 2
&& usersInterval <= 7200) {
return usersInterval * 1000;
}
return 0;
},
}, },
methods: { methods: {
/* Re-fetches external data, called by parent. Usually overridden by widget */ /* Re-fetches external data, called by parent. Usually overridden by widget */
@ -38,6 +66,10 @@ const WidgetMixin = {
this.startLoading(); this.startLoading();
this.fetchData(); this.fetchData();
}, },
/* If continuous updates enabled, create interval */
continuousUpdates() {
this.updater = setInterval(() => { this.update(); }, this.updateInterval);
},
/* Called when an error occurs. Logs to handler, and passes to parent component */ /* Called when an error occurs. Logs to handler, and passes to parent component */
error(msg, stackTrace) { error(msg, stackTrace) {
ErrorHandler(msg, stackTrace); ErrorHandler(msg, stackTrace);