diff options
author | Winnie Hellmann <winnie@gitlab.com> | 2018-10-26 11:48:20 +0300 |
---|---|---|
committer | Winnie Hellmann <winnie@gitlab.com> | 2018-11-07 12:00:25 +0300 |
commit | 74f22d26a93a1d4088b7cc4cc7359ef5d956889e (patch) | |
tree | ea0ce4f9a0cc36e6f2cbb6adebcd1aff7aade64f /app/assets/javascripts/jobs/mixins | |
parent | 390abbf054e82cfc51707ec3e2f68017d22ac75d (diff) |
Add Vue mixin for delayed job timers
Diffstat (limited to 'app/assets/javascripts/jobs/mixins')
-rw-r--r-- | app/assets/javascripts/jobs/mixins/delayed_job_mixin.js | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/app/assets/javascripts/jobs/mixins/delayed_job_mixin.js b/app/assets/javascripts/jobs/mixins/delayed_job_mixin.js new file mode 100644 index 00000000000..8c7fb785a61 --- /dev/null +++ b/app/assets/javascripts/jobs/mixins/delayed_job_mixin.js @@ -0,0 +1,50 @@ +import { calculateRemainingMilliseconds, formatTime } from '~/lib/utils/datetime_utility'; + +export default { + data() { + return { + remainingTime: formatTime(0), + remainingTimeIntervalId: null, + }; + }, + + mounted() { + this.startRemainingTimeInterval(); + }, + + beforeDestroy() { + if (this.remainingTimeIntervalId) { + clearInterval(this.remainingTimeIntervalId); + } + }, + + computed: { + isDelayedJob() { + return this.job && this.job.scheduled; + }, + }, + + watch: { + isDelayedJob() { + this.startRemainingTimeInterval(); + }, + }, + + methods: { + startRemainingTimeInterval() { + if (this.remainingTimeIntervalId) { + clearInterval(this.remainingTimeIntervalId); + } + + if (this.isDelayedJob) { + this.updateRemainingTime(); + this.remainingTimeIntervalId = setInterval(() => this.updateRemainingTime(), 1000); + } + }, + + updateRemainingTime() { + const remainingMilliseconds = calculateRemainingMilliseconds(this.job.scheduled_at); + this.remainingTime = formatTime(remainingMilliseconds); + }, + }, +}; |