mirror of
https://github.com/nasa/openmct.git
synced 2025-01-18 18:57:01 +00:00
* created a throttle util and using it in timer plugin to throttle refreshing the timer domain object * Simplify timer logic * Clarify code a little * refactor: lint:fix * Fix linting issue --------- Co-authored-by: Andrew Henry <akhenry@gmail.com> Co-authored-by: Jamie V <jamie.j.vigliotta@nasa.gov>
This commit is contained in:
parent
796616fe3f
commit
492e8055e5
@ -43,9 +43,11 @@
|
|||||||
|
|
||||||
<script>
|
<script>
|
||||||
import raf from 'utils/raf';
|
import raf from 'utils/raf';
|
||||||
|
import throttle from '../../../utils/throttle';
|
||||||
|
|
||||||
const moment = require('moment-timezone');
|
const moment = require('moment-timezone');
|
||||||
const momentDurationFormatSetup = require('moment-duration-format');
|
const momentDurationFormatSetup = require('moment-duration-format');
|
||||||
|
const refreshRateSeconds = 2;
|
||||||
|
|
||||||
momentDurationFormatSetup(moment);
|
momentDurationFormatSetup(moment);
|
||||||
|
|
||||||
@ -68,38 +70,21 @@ export default {
|
|||||||
};
|
};
|
||||||
},
|
},
|
||||||
computed: {
|
computed: {
|
||||||
relativeTimestamp() {
|
|
||||||
let relativeTimestamp;
|
|
||||||
if (this.configuration && this.configuration.timestamp) {
|
|
||||||
relativeTimestamp = moment(this.configuration.timestamp).toDate();
|
|
||||||
} else if (this.configuration && this.configuration.timestamp === undefined) {
|
|
||||||
relativeTimestamp = undefined;
|
|
||||||
}
|
|
||||||
|
|
||||||
return relativeTimestamp;
|
|
||||||
},
|
|
||||||
timeDelta() {
|
timeDelta() {
|
||||||
return this.lastTimestamp - this.relativeTimestamp;
|
if (this.configuration.pausedTime) {
|
||||||
|
return Date.parse(this.configuration.pausedTime) - this.startTimeMs;
|
||||||
|
} else {
|
||||||
|
return this.lastTimestamp - this.startTimeMs;
|
||||||
|
}
|
||||||
|
},
|
||||||
|
startTimeMs() {
|
||||||
|
return Date.parse(this.configuration.timestamp);
|
||||||
},
|
},
|
||||||
timeTextValue() {
|
timeTextValue() {
|
||||||
if (isNaN(this.timeDelta)) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
const toWholeSeconds = Math.abs(Math.floor(this.timeDelta / 1000) * 1000);
|
const toWholeSeconds = Math.abs(Math.floor(this.timeDelta / 1000) * 1000);
|
||||||
|
|
||||||
return moment.duration(toWholeSeconds, 'ms').format(this.format, { trim: false });
|
return moment.duration(toWholeSeconds, 'ms').format(this.format, { trim: false });
|
||||||
},
|
},
|
||||||
pausedTime() {
|
|
||||||
let pausedTime;
|
|
||||||
if (this.configuration && this.configuration.pausedTime) {
|
|
||||||
pausedTime = moment(this.configuration.pausedTime).toDate();
|
|
||||||
} else if (this.configuration && this.configuration.pausedTime === undefined) {
|
|
||||||
pausedTime = undefined;
|
|
||||||
}
|
|
||||||
|
|
||||||
return pausedTime;
|
|
||||||
},
|
|
||||||
timerState() {
|
timerState() {
|
||||||
let timerState = 'started';
|
let timerState = 'started';
|
||||||
if (this.configuration && this.configuration.timerState) {
|
if (this.configuration && this.configuration.timerState) {
|
||||||
@ -179,13 +164,9 @@ export default {
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
mounted() {
|
mounted() {
|
||||||
this.unobserve = this.openmct.objects.observe(
|
this.unobserve = this.openmct.objects.observe(this.domainObject, '*', (domainObject) => {
|
||||||
this.domainObject,
|
this.configuration = domainObject.configuration;
|
||||||
'configuration',
|
});
|
||||||
(configuration) => {
|
|
||||||
this.configuration = configuration;
|
|
||||||
}
|
|
||||||
);
|
|
||||||
this.$nextTick(() => {
|
this.$nextTick(() => {
|
||||||
if (!this.configuration?.timerState) {
|
if (!this.configuration?.timerState) {
|
||||||
const timerAction = !this.relativeTimestamp ? 'stop' : 'start';
|
const timerAction = !this.relativeTimestamp ? 'stop' : 'start';
|
||||||
@ -193,6 +174,7 @@ export default {
|
|||||||
}
|
}
|
||||||
|
|
||||||
this.handleTick = raf(this.handleTick);
|
this.handleTick = raf(this.handleTick);
|
||||||
|
this.refreshTimerObject = throttle(this.refreshTimerObject, refreshRateSeconds * 1000);
|
||||||
this.openmct.time.on('tick', this.handleTick);
|
this.openmct.time.on('tick', this.handleTick);
|
||||||
|
|
||||||
this.viewActionsCollection = this.openmct.actions.getActionsCollection(
|
this.viewActionsCollection = this.openmct.actions.getActionsCollection(
|
||||||
@ -210,15 +192,11 @@ export default {
|
|||||||
},
|
},
|
||||||
methods: {
|
methods: {
|
||||||
handleTick() {
|
handleTick() {
|
||||||
const isTimerRunning = !['paused', 'stopped'].includes(this.timerState);
|
this.lastTimestamp = new Date(this.openmct.time.now());
|
||||||
|
this.refreshTimerObject();
|
||||||
if (isTimerRunning) {
|
},
|
||||||
this.lastTimestamp = new Date(this.openmct.time.now());
|
refreshTimerObject() {
|
||||||
}
|
this.openmct.objects.refresh(this.domainObject);
|
||||||
|
|
||||||
if (this.timerState === 'paused' && !this.lastTimestamp) {
|
|
||||||
this.lastTimestamp = this.pausedTime;
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
restartTimer() {
|
restartTimer() {
|
||||||
this.triggerAction('timer.restart');
|
this.triggerAction('timer.restart');
|
||||||
|
34
src/utils/throttle.js
Normal file
34
src/utils/throttle.js
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
/**
|
||||||
|
* Creates a throttled function that only invokes the provided function at most once every
|
||||||
|
* specified number of milliseconds. Subsequent calls within the waiting period will be ignored.
|
||||||
|
* @param {Function} func The function to throttle.
|
||||||
|
* @param {number} wait The number of milliseconds to wait between successive calls to the function.
|
||||||
|
* @return {Function} Returns the new throttled function.
|
||||||
|
*/
|
||||||
|
export default function throttle(func, wait) {
|
||||||
|
let timeout;
|
||||||
|
let result;
|
||||||
|
let previous = 0;
|
||||||
|
|
||||||
|
return function (...args) {
|
||||||
|
const now = new Date().getTime();
|
||||||
|
const remaining = wait - (now - previous);
|
||||||
|
|
||||||
|
if (remaining <= 0 || remaining > wait) {
|
||||||
|
if (timeout) {
|
||||||
|
clearTimeout(timeout);
|
||||||
|
timeout = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
previous = now;
|
||||||
|
result = func(...args);
|
||||||
|
} else if (!timeout) {
|
||||||
|
timeout = setTimeout(() => {
|
||||||
|
previous = new Date().getTime();
|
||||||
|
timeout = null;
|
||||||
|
result = func(...args);
|
||||||
|
}, remaining);
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
};
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user