|
| 1 | +/** |
| 2 | + * OcrQueueManager Library |
| 3 | + * Part of CraftThingy Digital Innovation SDK |
| 4 | + * Licensed under Public-Source Corporate Royalty License (PSCRL) |
| 5 | + * Isomorphic: Runs in both Browser and Node.js environments. |
| 6 | + */ |
| 7 | + |
| 8 | +export class OcrQueueManager { |
| 9 | + constructor(config = {}) { |
| 10 | + this.getOcrClient = config.getOcrClient; |
| 11 | + this.onQueueUpdate = config.onQueueUpdate || (() => {}); |
| 12 | + this.onItemReady = config.onItemReady || (() => {}); |
| 13 | + this.onItemError = config.onItemError || (() => {}); |
| 14 | + this.onItemProcessing = config.onItemProcessing || (() => {}); |
| 15 | + |
| 16 | + this.queue = []; |
| 17 | + this.isProcessing = false; |
| 18 | + this.activeIndex = -1; |
| 19 | + } |
| 20 | + |
| 21 | + /** |
| 22 | + * Add new image to the queue and start background OCR runner |
| 23 | + * @param {Object} item { id, filename, url } (url can be a URL string, Base64, Node.js Buffer, or Canvas) |
| 24 | + */ |
| 25 | + enqueue(item = {}) { |
| 26 | + const queueItem = { |
| 27 | + id: item.id || this._generateId(), |
| 28 | + filename: item.filename || `ocr_file_${Date.now()}.jpg`, |
| 29 | + url: item.url, |
| 30 | + status: 'pending', |
| 31 | + results: [] |
| 32 | + }; |
| 33 | + |
| 34 | + this.queue.push(queueItem); |
| 35 | + this.onQueueUpdate(this.queue); |
| 36 | + |
| 37 | + // Start consuming queue in the background |
| 38 | + this._processNext(); |
| 39 | + } |
| 40 | + |
| 41 | + /** |
| 42 | + * Delete queue item by index |
| 43 | + * @param {Number} index |
| 44 | + */ |
| 45 | + dequeue(index) { |
| 46 | + if (index < 0 || index >= this.queue.length) return; |
| 47 | + |
| 48 | + this.queue.splice(index, 1); |
| 49 | + |
| 50 | + if (this.activeIndex === index) { |
| 51 | + this.activeIndex = this.queue.length > 0 ? 0 : -1; |
| 52 | + } else if (this.activeIndex > index) { |
| 53 | + this.activeIndex--; |
| 54 | + } |
| 55 | + |
| 56 | + this.onQueueUpdate(this.queue); |
| 57 | + this._processNext(); |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * Set active selected queue index |
| 62 | + * @param {Number} index |
| 63 | + */ |
| 64 | + selectItem(index) { |
| 65 | + if (index < 0 || index >= this.queue.length) return; |
| 66 | + this.activeIndex = index; |
| 67 | + this.onQueueUpdate(this.queue); |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * Get all queue items |
| 72 | + */ |
| 73 | + getItems() { |
| 74 | + return [...this.queue]; |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * Get active selected item |
| 79 | + */ |
| 80 | + getActiveItem() { |
| 81 | + if (this.activeIndex === -1 || this.activeIndex >= this.queue.length) return null; |
| 82 | + return this.queue[this.activeIndex]; |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * Get active selected index |
| 87 | + */ |
| 88 | + getActiveIndex() { |
| 89 | + return this.activeIndex; |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * Clear queue state |
| 94 | + */ |
| 95 | + clear() { |
| 96 | + this.queue = []; |
| 97 | + this.activeIndex = -1; |
| 98 | + this.isProcessing = false; |
| 99 | + this.onQueueUpdate(this.queue); |
| 100 | + } |
| 101 | + |
| 102 | + /** |
| 103 | + * Process next pending item in the queue sequentially |
| 104 | + */ |
| 105 | + async _processNext() { |
| 106 | + if (this.isProcessing) return; |
| 107 | + |
| 108 | + const pendingIndex = this.queue.findIndex(item => item.status === 'pending'); |
| 109 | + if (pendingIndex === -1) { |
| 110 | + this.isProcessing = false; |
| 111 | + return; |
| 112 | + } |
| 113 | + |
| 114 | + this.isProcessing = true; |
| 115 | + const item = this.queue[pendingIndex]; |
| 116 | + item.status = 'processing'; |
| 117 | + this.onQueueUpdate(this.queue); |
| 118 | + this.onItemProcessing(item); |
| 119 | + |
| 120 | + try { |
| 121 | + const client = await this.getOcrClient(); |
| 122 | + if (!client) { |
| 123 | + throw new Error("OCR engine client not initialized"); |
| 124 | + } |
| 125 | + |
| 126 | + const isBrowser = typeof window !== 'undefined' && typeof window.Image !== 'undefined'; |
| 127 | + const isUrlString = typeof item.url === 'string'; |
| 128 | + |
| 129 | + if (isBrowser && isUrlString) { |
| 130 | + // Browser environment: Load image URL via DOM Image element before sending to client |
| 131 | + const img = new Image(); |
| 132 | + img.onload = async () => { |
| 133 | + try { |
| 134 | + const result = await client.recognize(img); |
| 135 | + this._handleSuccess(result, item, pendingIndex); |
| 136 | + } catch (err) { |
| 137 | + this._handleError(item, err); |
| 138 | + } |
| 139 | + }; |
| 140 | + |
| 141 | + img.onerror = () => { |
| 142 | + this._handleError(item, new Error("Failed to load image element source")); |
| 143 | + }; |
| 144 | + |
| 145 | + img.src = item.url; |
| 146 | + } else { |
| 147 | + // Node.js or Buffer environment: Pass URL/Buffer/Canvas directly to the OCR client |
| 148 | + try { |
| 149 | + const result = await client.recognize(item.url); |
| 150 | + this._handleSuccess(result, item, pendingIndex); |
| 151 | + } catch (err) { |
| 152 | + this._handleError(item, err); |
| 153 | + } |
| 154 | + } |
| 155 | + } catch (err) { |
| 156 | + this._handleError(item, err); |
| 157 | + } |
| 158 | + } |
| 159 | + |
| 160 | + _handleSuccess(result, item, pendingIndex) { |
| 161 | + let words = []; |
| 162 | + if (result && result.lines) { |
| 163 | + result.lines.forEach(line => { |
| 164 | + if (line.words) { |
| 165 | + words.push(...line.words); |
| 166 | + } |
| 167 | + }); |
| 168 | + } |
| 169 | + |
| 170 | + item.results = words; |
| 171 | + item.status = 'ready'; |
| 172 | + |
| 173 | + // Auto-select first loaded item |
| 174 | + if (this.activeIndex === -1) { |
| 175 | + this.activeIndex = pendingIndex; |
| 176 | + } |
| 177 | + |
| 178 | + this.onItemReady(item, pendingIndex); |
| 179 | + this.isProcessing = false; |
| 180 | + this.onQueueUpdate(this.queue); |
| 181 | + this._processNext(); |
| 182 | + } |
| 183 | + |
| 184 | + _handleError(item, err) { |
| 185 | + item.status = 'error'; |
| 186 | + this.onItemError(item, err); |
| 187 | + this.isProcessing = false; |
| 188 | + this.onQueueUpdate(this.queue); |
| 189 | + this._processNext(); |
| 190 | + } |
| 191 | + |
| 192 | + _generateId() { |
| 193 | + return Math.random().toString(36).substring(2, 9); |
| 194 | + } |
| 195 | +} |
0 commit comments