mirror of
https://github.com/hicccc77/WeFlow.git
synced 2026-04-22 15:09:04 +00:00
图片解密再次优化
This commit is contained in:
@@ -9,10 +9,12 @@ type BackgroundTaskListener = (tasks: BackgroundTaskRecord[]) => void
|
||||
|
||||
const tasks = new Map<string, BackgroundTaskRecord>()
|
||||
const cancelHandlers = new Map<string, () => void | Promise<void>>()
|
||||
const pauseHandlers = new Map<string, () => void | Promise<void>>()
|
||||
const resumeHandlers = new Map<string, () => void | Promise<void>>()
|
||||
const listeners = new Set<BackgroundTaskListener>()
|
||||
let taskSequence = 0
|
||||
|
||||
const ACTIVE_STATUSES = new Set<BackgroundTaskStatus>(['running', 'cancel_requested'])
|
||||
const ACTIVE_STATUSES = new Set<BackgroundTaskStatus>(['running', 'pause_requested', 'paused', 'cancel_requested'])
|
||||
const MAX_SETTLED_TASKS = 24
|
||||
|
||||
const buildTaskId = (): string => {
|
||||
@@ -34,6 +36,9 @@ const pruneSettledTasks = () => {
|
||||
|
||||
for (const staleTask of settledTasks.slice(MAX_SETTLED_TASKS)) {
|
||||
tasks.delete(staleTask.id)
|
||||
cancelHandlers.delete(staleTask.id)
|
||||
pauseHandlers.delete(staleTask.id)
|
||||
resumeHandlers.delete(staleTask.id)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -64,7 +69,9 @@ export const registerBackgroundTask = (input: BackgroundTaskInput): string => {
|
||||
detail: input.detail,
|
||||
progressText: input.progressText,
|
||||
cancelable: input.cancelable !== false,
|
||||
resumable: input.resumable === true,
|
||||
cancelRequested: false,
|
||||
pauseRequested: false,
|
||||
status: 'running',
|
||||
startedAt: now,
|
||||
updatedAt: now
|
||||
@@ -72,6 +79,12 @@ export const registerBackgroundTask = (input: BackgroundTaskInput): string => {
|
||||
if (input.onCancel) {
|
||||
cancelHandlers.set(taskId, input.onCancel)
|
||||
}
|
||||
if (input.onPause) {
|
||||
pauseHandlers.set(taskId, input.onPause)
|
||||
}
|
||||
if (input.onResume) {
|
||||
resumeHandlers.set(taskId, input.onResume)
|
||||
}
|
||||
pruneSettledTasks()
|
||||
notifyListeners()
|
||||
return taskId
|
||||
@@ -87,6 +100,9 @@ export const updateBackgroundTask = (taskId: string, patch: BackgroundTaskUpdate
|
||||
...patch,
|
||||
status: nextStatus,
|
||||
updatedAt: nextUpdatedAt,
|
||||
pauseRequested: nextStatus === 'paused' || nextStatus === 'pause_requested'
|
||||
? true
|
||||
: (nextStatus === 'running' ? false : existing.pauseRequested),
|
||||
finishedAt: ACTIVE_STATUSES.has(nextStatus) ? undefined : (existing.finishedAt || nextUpdatedAt)
|
||||
})
|
||||
pruneSettledTasks()
|
||||
@@ -107,9 +123,12 @@ export const finishBackgroundTask = (
|
||||
status,
|
||||
updatedAt: now,
|
||||
finishedAt: now,
|
||||
cancelRequested: status === 'canceled' ? true : existing.cancelRequested
|
||||
cancelRequested: status === 'canceled' ? true : existing.cancelRequested,
|
||||
pauseRequested: false
|
||||
})
|
||||
cancelHandlers.delete(taskId)
|
||||
pauseHandlers.delete(taskId)
|
||||
resumeHandlers.delete(taskId)
|
||||
pruneSettledTasks()
|
||||
notifyListeners()
|
||||
}
|
||||
@@ -121,6 +140,7 @@ export const requestCancelBackgroundTask = (taskId: string): boolean => {
|
||||
...existing,
|
||||
status: 'cancel_requested',
|
||||
cancelRequested: true,
|
||||
pauseRequested: false,
|
||||
detail: existing.detail || '停止请求已发出,当前查询完成后会结束后续加载',
|
||||
updatedAt: Date.now()
|
||||
})
|
||||
@@ -132,6 +152,46 @@ export const requestCancelBackgroundTask = (taskId: string): boolean => {
|
||||
return true
|
||||
}
|
||||
|
||||
export const requestPauseBackgroundTask = (taskId: string): boolean => {
|
||||
const existing = tasks.get(taskId)
|
||||
if (!existing || !existing.resumable) return false
|
||||
if (existing.status !== 'running' && existing.status !== 'pause_requested') return false
|
||||
tasks.set(taskId, {
|
||||
...existing,
|
||||
status: 'pause_requested',
|
||||
pauseRequested: true,
|
||||
detail: existing.detail || '中断请求已发出,当前处理完成后会暂停',
|
||||
updatedAt: Date.now()
|
||||
})
|
||||
const pauseHandler = pauseHandlers.get(taskId)
|
||||
if (pauseHandler) {
|
||||
void Promise.resolve(pauseHandler()).catch(() => {})
|
||||
}
|
||||
notifyListeners()
|
||||
return true
|
||||
}
|
||||
|
||||
export const requestResumeBackgroundTask = (taskId: string): boolean => {
|
||||
const existing = tasks.get(taskId)
|
||||
if (!existing || !existing.resumable) return false
|
||||
if (existing.status !== 'paused' && existing.status !== 'pause_requested') return false
|
||||
tasks.set(taskId, {
|
||||
...existing,
|
||||
status: 'running',
|
||||
cancelRequested: false,
|
||||
pauseRequested: false,
|
||||
detail: existing.detail || '任务已继续',
|
||||
updatedAt: Date.now(),
|
||||
finishedAt: undefined
|
||||
})
|
||||
const resumeHandler = resumeHandlers.get(taskId)
|
||||
if (resumeHandler) {
|
||||
void Promise.resolve(resumeHandler()).catch(() => {})
|
||||
}
|
||||
notifyListeners()
|
||||
return true
|
||||
}
|
||||
|
||||
export const requestCancelBackgroundTasks = (predicate: (task: BackgroundTaskRecord) => boolean): number => {
|
||||
let canceledCount = 0
|
||||
for (const task of tasks.values()) {
|
||||
@@ -147,3 +207,8 @@ export const isBackgroundTaskCancelRequested = (taskId: string): boolean => {
|
||||
const task = tasks.get(taskId)
|
||||
return Boolean(task?.cancelRequested)
|
||||
}
|
||||
|
||||
export const isBackgroundTaskPauseRequested = (taskId: string): boolean => {
|
||||
const task = tasks.get(taskId)
|
||||
return Boolean(task?.pauseRequested)
|
||||
}
|
||||
|
||||
@@ -689,11 +689,17 @@ const normalizeAutomationTask = (raw: unknown): ExportAutomationTask | null => {
|
||||
if (scheduleType === 'interval') {
|
||||
const rawDays = Math.max(0, normalizeAutomationNumeric(scheduleObj.intervalDays, 0))
|
||||
const rawHours = Math.max(0, normalizeAutomationNumeric(scheduleObj.intervalHours, 0))
|
||||
const rawFirstTriggerAt = Math.max(0, normalizeAutomationNumeric(scheduleObj.firstTriggerAt, 0))
|
||||
const totalHours = (rawDays * 24) + rawHours
|
||||
if (totalHours <= 0) return null
|
||||
const intervalDays = Math.floor(totalHours / 24)
|
||||
const intervalHours = totalHours % 24
|
||||
schedule = { type: 'interval', intervalDays, intervalHours }
|
||||
schedule = {
|
||||
type: 'interval',
|
||||
intervalDays,
|
||||
intervalHours,
|
||||
firstTriggerAt: rawFirstTriggerAt > 0 ? rawFirstTriggerAt : undefined
|
||||
}
|
||||
}
|
||||
if (!schedule) return null
|
||||
|
||||
|
||||
Reference in New Issue
Block a user