-
Notifications
You must be signed in to change notification settings - Fork 51
Expand file tree
/
Copy pathdownloadStore.js
More file actions
121 lines (102 loc) · 2.49 KB
/
downloadStore.js
File metadata and controls
121 lines (102 loc) · 2.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import { defineStore } from 'pinia'
import { ref, computed } from 'vue'
import { downloadService } from '@/services/downloadService'
export const useDownloadStore = defineStore('download', () => {
// 任务列表
const tasks = ref([])
// 加载状态
const loading = ref(false)
// 从downloadService加载任务
const loadTasks = () => {
tasks.value = downloadService.getAllTasks()
}
// 保存任务到downloadService
const saveTasks = () => {
downloadService.saveTasksToStorage()
}
// 添加下载任务
const addTask = (taskData) => {
const task = downloadService.createTask(taskData)
loadTasks() // 重新加载任务列表
return task
}
// 开始任务
const startTask = (taskId) => {
downloadService.startTask(taskId)
loadTasks()
}
// 暂停任务
const pauseTask = (taskId) => {
downloadService.pauseTask(taskId)
loadTasks()
}
// 恢复任务
const resumeTask = (taskId) => {
downloadService.startTask(taskId)
loadTasks()
}
// 取消任务
const cancelTask = (taskId) => {
downloadService.cancelTask(taskId)
loadTasks()
}
// 删除任务
const deleteTask = (taskId) => {
downloadService.deleteTask(taskId)
loadTasks()
}
// 重试任务
const retryTask = (taskId) => {
downloadService.startTask(taskId)
loadTasks()
}
// 重试章节
const retryChapter = (taskId, chapterIndex) => {
downloadService.retryChapter(taskId, chapterIndex)
loadTasks()
}
// 导出任务
const exportTask = (taskId) => {
return downloadService.exportToTxt(taskId)
}
// 清理已完成的任务
const clearCompleted = () => {
const completedTasks = tasks.value.filter(task => task.status === 'completed')
completedTasks.forEach(task => {
downloadService.deleteTask(task.id)
})
loadTasks()
}
// 计算属性
const taskStats = computed(() => {
return downloadService.getTaskStats()
})
// 根据状态过滤任务
const getTasksByStatus = (status) => {
return downloadService.getTasksByStatus(status)
}
// 设置任务更新回调
downloadService.setTaskUpdateCallback(() => {
loadTasks()
})
// 初始化时加载任务
loadTasks()
return {
tasks,
loading,
taskStats,
loadTasks,
saveTasks,
addTask,
startTask,
pauseTask,
resumeTask,
cancelTask,
deleteTask,
retryTask,
retryChapter,
exportTask,
clearCompleted,
getTasksByStatus
}
})