diff options
Diffstat (limited to 'app/assets/javascripts/ci/mixins/delayed_job_mixin.js')
-rw-r--r-- | app/assets/javascripts/ci/mixins/delayed_job_mixin.js | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/app/assets/javascripts/ci/mixins/delayed_job_mixin.js b/app/assets/javascripts/ci/mixins/delayed_job_mixin.js new file mode 100644 index 00000000000..7b17dc7f693 --- /dev/null +++ b/app/assets/javascripts/ci/mixins/delayed_job_mixin.js @@ -0,0 +1,53 @@ +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?.scheduled || this.job?.scheduledAt; + }, + scheduledTime() { + return this.job.scheduled_at || this.job.scheduledAt; + }, + }, + + 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.scheduledTime); + this.remainingTime = formatTime(remainingMilliseconds); + }, + }, +}; |