From 3aa4fab4887d203eb5784758e89fce7ffe7c4a37 Mon Sep 17 00:00:00 2001 From: davelopez <46503462+davelopez@users.noreply.github.com> Date: Thu, 14 Sep 2023 16:29:01 +0200 Subject: [PATCH] Convert taskMonitor to Typescript + refactoring --- .../{taskMonitor.js => taskMonitor.ts} | 46 ++++++++++--------- 1 file changed, 25 insertions(+), 21 deletions(-) rename client/src/composables/{taskMonitor.js => taskMonitor.ts} (67%) diff --git a/client/src/composables/taskMonitor.js b/client/src/composables/taskMonitor.ts similarity index 67% rename from client/src/composables/taskMonitor.js rename to client/src/composables/taskMonitor.ts index 01a5a591a4b0..6b94e9c9e05b 100644 --- a/client/src/composables/taskMonitor.js +++ b/client/src/composables/taskMonitor.ts @@ -1,57 +1,61 @@ -import axios from "axios"; import { computed, readonly, ref } from "vue"; +import { fetcher } from "@/schema"; +import { errorMessageAsString } from "@/utils/simple-error"; + const SUCCESS_STATE = "SUCCESS"; const FAILURE_STATE = "FAILURE"; const TASK_READY_STATES = [SUCCESS_STATE, FAILURE_STATE]; const DEFAULT_POLL_DELAY = 10000; +const getTaskStatus = fetcher.path("/api/tasks/{task_id}/state").method("get").create(); + /** * Composable for waiting on Galaxy background tasks. */ export function useTaskMonitor() { - let timeout = null; + let timeout: NodeJS.Timeout | null = null; let pollDelay = DEFAULT_POLL_DELAY; const isRunning = ref(false); - const status = ref(null); - const currentTaskId = ref(null); + const status = ref(); + const currentTaskId = ref(); const requestHasFailed = ref(false); const isCompleted = computed(() => status.value === SUCCESS_STATE); const hasFailed = computed(() => status.value === FAILURE_STATE); - const queryStateUrl = computed(() => `/api/tasks/${currentTaskId.value}/state`); - function waitForTask(taskId, pollDelayInMs = DEFAULT_POLL_DELAY) { + async function waitForTask(taskId: string, pollDelayInMs = DEFAULT_POLL_DELAY) { pollDelay = pollDelayInMs; resetState(); currentTaskId.value = taskId; isRunning.value = true; - fetchTaskStatus(); - } - - function fetchTaskStatus() { - axios.get(queryStateUrl.value).then(handleStatusResponse).catch(handleError); + return fetchTaskStatus(taskId); } - function handleStatusResponse(response) { - status.value = response.data; - const isReady = TASK_READY_STATES.includes(status.value); - if (isReady) { - isRunning.value = false; - } else { - pollAfterDelay(); + async function fetchTaskStatus(taskId: string) { + try { + const { data } = await getTaskStatus({ task_id: taskId }); + status.value = data; + const isReady = TASK_READY_STATES.includes(status.value); + if (isReady) { + isRunning.value = false; + } else { + pollAfterDelay(taskId); + } + } catch (err) { + handleError(errorMessageAsString(err)); } } - function pollAfterDelay(taskId) { + function pollAfterDelay(taskId: string) { resetTimeout(); timeout = setTimeout(() => { fetchTaskStatus(taskId); }, pollDelay); } - function handleError(err) { + function handleError(err: string) { status.value = err; requestHasFailed.value = true; isRunning.value = false; @@ -66,7 +70,7 @@ export function useTaskMonitor() { function resetState() { resetTimeout(); - status.value = null; + status.value = undefined; } return {