mirror of
https://github.com/Comfy-Org/ComfyUI_frontend.git
synced 2026-02-22 15:54:09 +00:00
[Refactor] Task execution into task runner class
This commit is contained in:
@@ -5,12 +5,12 @@
|
||||
>
|
||||
<Card
|
||||
class="max-w-48 relative h-full overflow-hidden"
|
||||
:class="{ 'opacity-65': state.state !== 'error' }"
|
||||
:class="{ 'opacity-65': runner.state !== 'error' }"
|
||||
v-bind="(({ onClick, ...rest }) => rest)($attrs)"
|
||||
>
|
||||
<template #header>
|
||||
<i
|
||||
v-if="state.state === 'error'"
|
||||
v-if="runner.state === 'error'"
|
||||
class="pi pi-exclamation-triangle text-red-500 absolute m-2 top-0 -right-14 opacity-15"
|
||||
style="font-size: 10rem"
|
||||
/>
|
||||
@@ -38,7 +38,7 @@
|
||||
</Card>
|
||||
|
||||
<i
|
||||
v-if="!isLoading && state.state === 'OK'"
|
||||
v-if="!isLoading && runner.state === 'OK'"
|
||||
class="task-card-ok pi pi-check"
|
||||
/>
|
||||
</div>
|
||||
@@ -54,7 +54,7 @@ import type { MaintenanceTask } from '@/types/desktop/maintenanceTypes'
|
||||
import { useMinLoadingDurationRef } from '@/utils/refUtil'
|
||||
|
||||
const taskStore = useMaintenanceTaskStore()
|
||||
const state = computed(() => taskStore.getState(props.task))
|
||||
const runner = computed(() => taskStore.getRunner(props.task))
|
||||
|
||||
// Properties
|
||||
const props = defineProps<{
|
||||
@@ -68,14 +68,14 @@ defineEmits<{
|
||||
|
||||
// Bindings
|
||||
const description = computed(() =>
|
||||
state.value.state === 'error'
|
||||
runner.value.state === 'error'
|
||||
? props.task.errorDescription ?? props.task.shortDescription
|
||||
: props.task.shortDescription
|
||||
)
|
||||
|
||||
// Use a minimum run time to ensure tasks "feel" like they have run
|
||||
const reactiveLoading = computed(() => state.value.refreshing)
|
||||
const reactiveExecuting = computed(() => state.value.executing)
|
||||
const reactiveLoading = computed(() => runner.value.refreshing)
|
||||
const reactiveExecuting = computed(() => runner.value.executing)
|
||||
|
||||
const isLoading = useMinLoadingDurationRef(reactiveLoading, 250)
|
||||
const isExecuting = useMinLoadingDurationRef(reactiveExecuting, 250)
|
||||
|
||||
@@ -2,12 +2,12 @@
|
||||
<tr
|
||||
class="border-neutral-700 border-solid border-y"
|
||||
:class="{
|
||||
'opacity-50': state.state === 'resolved',
|
||||
'opacity-75': isLoading && state.state !== 'resolved'
|
||||
'opacity-50': runner.state === 'resolved',
|
||||
'opacity-75': isLoading && runner.state !== 'resolved'
|
||||
}"
|
||||
>
|
||||
<td class="text-center w-16">
|
||||
<TaskListStatusIcon :state="state.state" :loading="isLoading" />
|
||||
<TaskListStatusIcon :state="runner.state" :loading="isLoading" />
|
||||
</td>
|
||||
<td>
|
||||
<p class="inline-block">{{ task.name }}</p>
|
||||
@@ -51,7 +51,7 @@ import { useMinLoadingDurationRef } from '@/utils/refUtil'
|
||||
import TaskListStatusIcon from './TaskListStatusIcon.vue'
|
||||
|
||||
const taskStore = useMaintenanceTaskStore()
|
||||
const state = computed(() => taskStore.getState(props.task))
|
||||
const runner = computed(() => taskStore.getRunner(props.task))
|
||||
|
||||
// Properties
|
||||
const props = defineProps<{
|
||||
@@ -65,14 +65,14 @@ defineEmits<{
|
||||
|
||||
// Binding
|
||||
const severity = computed<VueSeverity>(() =>
|
||||
state.value.state === 'error' || state.value.state === 'warning'
|
||||
runner.value.state === 'error' || runner.value.state === 'warning'
|
||||
? 'primary'
|
||||
: 'secondary'
|
||||
)
|
||||
|
||||
// Use a minimum run time to ensure tasks "feel" like they have run
|
||||
const reactiveLoading = computed(() => state.value.refreshing)
|
||||
const reactiveExecuting = computed(() => state.value.executing)
|
||||
const reactiveLoading = computed(() => runner.value.refreshing)
|
||||
const reactiveExecuting = computed(() => runner.value.executing)
|
||||
|
||||
const isLoading = useMinLoadingDurationRef(reactiveLoading, 250)
|
||||
const isExecuting = useMinLoadingDurationRef(reactiveExecuting, 250)
|
||||
|
||||
@@ -3,12 +3,40 @@ import { defineStore } from 'pinia'
|
||||
import { computed, ref } from 'vue'
|
||||
|
||||
import { DESKTOP_MAINTENANCE_TASKS } from '@/constants/desktopMaintenanceTasks'
|
||||
import type {
|
||||
MaintenanceTask,
|
||||
MaintenanceTaskState
|
||||
} from '@/types/desktop/maintenanceTypes'
|
||||
import type { MaintenanceTask } from '@/types/desktop/maintenanceTypes'
|
||||
import { electronAPI } from '@/utils/envUtil'
|
||||
|
||||
type MaintenanceTaskState = 'warning' | 'error' | 'resolved' | 'OK' | 'skipped'
|
||||
|
||||
/** State of a maintenance task, managed by the maintenance task store. */
|
||||
export class MaintenanceTaskRunner {
|
||||
/** The current state of the task. */
|
||||
state?: 'warning' | 'error' | 'resolved' | 'OK' | 'skipped'
|
||||
/** Whether the task state is currently being refreshed. */
|
||||
refreshing?: boolean
|
||||
/** Whether the task is currently running. */
|
||||
executing?: boolean
|
||||
/** The error message that occurred when the task failed. */
|
||||
error?: string
|
||||
|
||||
/** Wraps the execution of a maintenance task, updating state and rethrowing errors. */
|
||||
async execute(task: MaintenanceTask) {
|
||||
try {
|
||||
this.executing = true
|
||||
const success = await task.execute()
|
||||
if (!success) return false
|
||||
|
||||
this.error = undefined
|
||||
return true
|
||||
} catch (error) {
|
||||
this.error = (error as Error)?.message
|
||||
throw error
|
||||
} finally {
|
||||
this.executing = false
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* User-initiated maintenance tasks. Currently only used by the desktop app maintenance view.
|
||||
*
|
||||
@@ -24,53 +52,34 @@ export const useMaintenanceTaskStore = defineStore('maintenanceTask', () => {
|
||||
const isRunningTerminalCommand = computed(() =>
|
||||
tasks.value
|
||||
.filter((task) => task.usesTerminal)
|
||||
.some((task) => getState(task)?.executing)
|
||||
.some((task) => getRunner(task)?.executing)
|
||||
)
|
||||
const isRunningInstallationFix = computed(() =>
|
||||
tasks.value
|
||||
.filter((task) => task.isInstallationFix)
|
||||
.some((task) => getState(task)?.executing)
|
||||
.some((task) => getRunner(task)?.executing)
|
||||
)
|
||||
|
||||
// Task list
|
||||
const tasks = ref(DESKTOP_MAINTENANCE_TASKS)
|
||||
|
||||
const taskStates = ref(
|
||||
new Map<MaintenanceTask['id'], MaintenanceTaskState>(
|
||||
DESKTOP_MAINTENANCE_TASKS.map((x) => [x.id, {}])
|
||||
new Map<MaintenanceTask['id'], MaintenanceTaskRunner>(
|
||||
DESKTOP_MAINTENANCE_TASKS.map((x) => [x.id, new MaintenanceTaskRunner()])
|
||||
)
|
||||
)
|
||||
|
||||
/** True if any tasks are in an error state. */
|
||||
const anyErrors = computed(() =>
|
||||
tasks.value.some((task) => getState(task).state === 'error')
|
||||
tasks.value.some((task) => getRunner(task).state === 'error')
|
||||
)
|
||||
|
||||
/** Wraps the execution of a maintenance task, updating state and rethrowing errors. */
|
||||
const execute = async (task: MaintenanceTask) => {
|
||||
const state = getState(task)
|
||||
|
||||
try {
|
||||
state.executing = true
|
||||
const success = await task.execute()
|
||||
if (!success) return false
|
||||
|
||||
state.error = undefined
|
||||
return true
|
||||
} catch (error) {
|
||||
state.error = (error as Error)?.message
|
||||
throw error
|
||||
} finally {
|
||||
state.executing = false
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the matching state object for a task.
|
||||
* @param task Task to get the matching state object for
|
||||
* @returns The state object for this task
|
||||
*/
|
||||
const getState = (task: MaintenanceTask) => taskStates.value.get(task.id)!
|
||||
const getRunner = (task: MaintenanceTask) => taskStates.value.get(task.id)!
|
||||
|
||||
/**
|
||||
* Updates the task list with the latest validation state.
|
||||
@@ -87,15 +96,15 @@ export const useMaintenanceTaskStore = defineStore('maintenanceTask', () => {
|
||||
|
||||
// Update each task state
|
||||
for (const task of tasks.value) {
|
||||
const state = getState(task)
|
||||
const runner = getRunner(task)
|
||||
|
||||
state.refreshing = update[task.id] === undefined
|
||||
runner.refreshing = update[task.id] === undefined
|
||||
// Mark resolved
|
||||
if (state.state === 'error' && update[task.id] === 'OK')
|
||||
state.state = 'resolved'
|
||||
if (update[task.id] === 'OK' && state.state === 'resolved') continue
|
||||
if (runner.state === 'error' && update[task.id] === 'OK')
|
||||
runner.state = 'resolved'
|
||||
if (update[task.id] === 'OK' && runner.state === 'resolved') continue
|
||||
|
||||
if (update[task.id]) state.state = update[task.id]
|
||||
if (update[task.id]) runner.state = update[task.id]
|
||||
}
|
||||
|
||||
// Final update
|
||||
@@ -103,11 +112,11 @@ export const useMaintenanceTaskStore = defineStore('maintenanceTask', () => {
|
||||
isRefreshing.value = false
|
||||
|
||||
for (const task of tasks.value) {
|
||||
const state = getState(task)
|
||||
state.refreshing = false
|
||||
if (state.state === 'resolved') continue
|
||||
const runner = getRunner(task)
|
||||
runner.refreshing = false
|
||||
if (runner.state === 'resolved') continue
|
||||
|
||||
state.state = update[task.id] ?? 'skipped'
|
||||
runner.state = update[task.id] ?? 'skipped'
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -115,8 +124,8 @@ export const useMaintenanceTaskStore = defineStore('maintenanceTask', () => {
|
||||
/** Clears the resolved status of tasks (when changing filters) */
|
||||
const clearResolved = () => {
|
||||
for (const task of tasks.value) {
|
||||
const state = getState(task)
|
||||
if (state?.state === 'resolved') state.state = 'OK'
|
||||
const runner = getRunner(task)
|
||||
if (runner?.state === 'resolved') runner.state = 'OK'
|
||||
}
|
||||
}
|
||||
|
||||
@@ -127,13 +136,17 @@ export const useMaintenanceTaskStore = defineStore('maintenanceTask', () => {
|
||||
await electron.Validation.validateInstallation(processUpdate)
|
||||
}
|
||||
|
||||
const execute = async (task: MaintenanceTask) => {
|
||||
return getRunner(task).execute(task)
|
||||
}
|
||||
|
||||
return {
|
||||
tasks,
|
||||
isRefreshing,
|
||||
isRunningTerminalCommand,
|
||||
isRunningInstallationFix,
|
||||
execute,
|
||||
getState,
|
||||
getRunner,
|
||||
processUpdate,
|
||||
clearResolved,
|
||||
/** True if any tasks are in an error state. */
|
||||
|
||||
@@ -39,18 +39,6 @@ export interface MaintenanceTask {
|
||||
isInstallationFix?: boolean
|
||||
}
|
||||
|
||||
/** State of a maintenance task, managed by the maintenance task store. */
|
||||
export interface MaintenanceTaskState {
|
||||
/** The current state of the task. */
|
||||
state?: 'warning' | 'error' | 'resolved' | 'OK' | 'skipped'
|
||||
/** Whether the task state is currently being refreshed. */
|
||||
refreshing?: boolean
|
||||
/** Whether the task is currently running. */
|
||||
executing?: boolean
|
||||
/** The error message that occurred when the task failed. */
|
||||
error?: string
|
||||
}
|
||||
|
||||
/** The filter options for the maintenance task list. */
|
||||
export interface MaintenanceFilter {
|
||||
/** CSS classes used for the filter button icon, e.g. 'pi pi-cross' */
|
||||
|
||||
@@ -125,7 +125,7 @@ const displayAsList = ref(PrimeIcons.TH_LARGE)
|
||||
|
||||
const errorFilter = computed(() =>
|
||||
taskStore.tasks.filter((x) => {
|
||||
const { state } = taskStore.getState(x)
|
||||
const { state } = taskStore.getRunner(x)
|
||||
return state === 'error' || state === 'resolved'
|
||||
})
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user