Upload 2 files
Browse files- src/lib/imageQueue.js +114 -0
src/lib/imageQueue.js
ADDED
|
@@ -0,0 +1,114 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
/**
|
| 2 |
+
* 图片处理队列
|
| 3 |
+
* 确保图片严格按照顺序处理,避免异步操作导致的顺序混乱
|
| 4 |
+
*/
|
| 5 |
+
class ImageProcessingQueue {
|
| 6 |
+
constructor() {
|
| 7 |
+
this.queue = []
|
| 8 |
+
this.processing = false
|
| 9 |
+
this.results = new Map() // 存储处理结果,key为索引,value为结果
|
| 10 |
+
}
|
| 11 |
+
|
| 12 |
+
/**
|
| 13 |
+
* 添加图片处理任务到队列
|
| 14 |
+
* @param {number} index - 图片索引
|
| 15 |
+
* @param {Function} processor - 处理函数
|
| 16 |
+
* @returns {Promise} 处理结果的Promise
|
| 17 |
+
*/
|
| 18 |
+
async addTask(index, processor) {
|
| 19 |
+
return new Promise((resolve, reject) => {
|
| 20 |
+
const task = {
|
| 21 |
+
index,
|
| 22 |
+
processor,
|
| 23 |
+
resolve,
|
| 24 |
+
reject,
|
| 25 |
+
addTime: Date.now()
|
| 26 |
+
}
|
| 27 |
+
|
| 28 |
+
this.queue.push(task)
|
| 29 |
+
console.log(`[图片队列] 任务${index}已添加到队列,队列长度: ${this.queue.length}`)
|
| 30 |
+
|
| 31 |
+
// 如果队列没有在处理,开始处理
|
| 32 |
+
if (!this.processing) {
|
| 33 |
+
this.processQueue()
|
| 34 |
+
}
|
| 35 |
+
})
|
| 36 |
+
}
|
| 37 |
+
|
| 38 |
+
/**
|
| 39 |
+
* 处理队列中的任务
|
| 40 |
+
*/
|
| 41 |
+
async processQueue() {
|
| 42 |
+
if (this.processing || this.queue.length === 0) {
|
| 43 |
+
return
|
| 44 |
+
}
|
| 45 |
+
|
| 46 |
+
this.processing = true
|
| 47 |
+
console.log(`[图片队列] 开始处理队列,共${this.queue.length}个任务`)
|
| 48 |
+
|
| 49 |
+
while (this.queue.length > 0) {
|
| 50 |
+
const task = this.queue.shift()
|
| 51 |
+
const startTime = Date.now()
|
| 52 |
+
|
| 53 |
+
console.log(`[图片队列] 开始处理任务${task.index},等待时间: ${startTime - task.addTime}ms`)
|
| 54 |
+
|
| 55 |
+
try {
|
| 56 |
+
const result = await task.processor()
|
| 57 |
+
const endTime = Date.now()
|
| 58 |
+
const processingTime = endTime - startTime
|
| 59 |
+
|
| 60 |
+
console.log(`[图片队列] 任务${task.index}处理完成,耗时: ${processingTime}ms`)
|
| 61 |
+
|
| 62 |
+
// 存储结果
|
| 63 |
+
this.results.set(task.index, {
|
| 64 |
+
result,
|
| 65 |
+
processingTime,
|
| 66 |
+
completedAt: endTime
|
| 67 |
+
})
|
| 68 |
+
|
| 69 |
+
task.resolve(result)
|
| 70 |
+
} catch (error) {
|
| 71 |
+
console.error(`[图片队列] 任务${task.index}处理失败: ${error.message}`)
|
| 72 |
+
task.reject(error)
|
| 73 |
+
}
|
| 74 |
+
}
|
| 75 |
+
|
| 76 |
+
this.processing = false
|
| 77 |
+
console.log(`[图片队列] 队列处理完成`)
|
| 78 |
+
}
|
| 79 |
+
|
| 80 |
+
/**
|
| 81 |
+
* 获取所有结果,按索引顺序排列
|
| 82 |
+
* @returns {Array} 按顺序排列的结果数组
|
| 83 |
+
*/
|
| 84 |
+
getOrderedResults() {
|
| 85 |
+
const sortedKeys = Array.from(this.results.keys()).sort((a, b) => a - b)
|
| 86 |
+
return sortedKeys.map(key => ({
|
| 87 |
+
index: key,
|
| 88 |
+
...this.results.get(key)
|
| 89 |
+
}))
|
| 90 |
+
}
|
| 91 |
+
|
| 92 |
+
/**
|
| 93 |
+
* 清空队列和结果
|
| 94 |
+
*/
|
| 95 |
+
clear() {
|
| 96 |
+
this.queue = []
|
| 97 |
+
this.results.clear()
|
| 98 |
+
this.processing = false
|
| 99 |
+
console.log(`[图片队列] 队列已清空`)
|
| 100 |
+
}
|
| 101 |
+
|
| 102 |
+
/**
|
| 103 |
+
* 获取队列状态
|
| 104 |
+
*/
|
| 105 |
+
getStatus() {
|
| 106 |
+
return {
|
| 107 |
+
queueLength: this.queue.length,
|
| 108 |
+
processing: this.processing,
|
| 109 |
+
resultsCount: this.results.size
|
| 110 |
+
}
|
| 111 |
+
}
|
| 112 |
+
}
|
| 113 |
+
|
| 114 |
+
module.exports = ImageProcessingQueue
|