|
|
|
@ -9,20 +9,44 @@
@@ -9,20 +9,44 @@
|
|
|
|
|
*/ |
|
|
|
|
const adapter = require('../node/'); |
|
|
|
|
|
|
|
|
|
/** A global job counter as part of job id */ |
|
|
|
|
let jobCounter = 0; |
|
|
|
|
|
|
|
|
|
class TesseractJob { |
|
|
|
|
constructor(instance) { |
|
|
|
|
/** |
|
|
|
|
* constructor |
|
|
|
|
* |
|
|
|
|
* @name constructor |
|
|
|
|
* @function initial a TesseractJob |
|
|
|
|
* @access public |
|
|
|
|
* @param {object} worker - An instance of TesseractWorker |
|
|
|
|
*/ |
|
|
|
|
constructor(worker) { |
|
|
|
|
jobCounter += 1; |
|
|
|
|
this.id = `Job-${jobCounter}-${Math.random().toString(16).slice(3, 8)}`; |
|
|
|
|
|
|
|
|
|
this._instance = instance; |
|
|
|
|
this._worker = worker; |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* As all the callback functions are saved in an array. |
|
|
|
|
* Basically you can register more than callback function |
|
|
|
|
* for then, catch, progress and finally. |
|
|
|
|
*/ |
|
|
|
|
this._resolve = []; |
|
|
|
|
this._reject = []; |
|
|
|
|
this._progress = []; |
|
|
|
|
this._finally = []; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* then |
|
|
|
|
* |
|
|
|
|
* @name then |
|
|
|
|
* @function A function to chain like Promise |
|
|
|
|
* @access public |
|
|
|
|
* @param {function} resolve - called when the job succeeds |
|
|
|
|
* @param {function} reject - called when the job fails |
|
|
|
|
*/ |
|
|
|
|
then(resolve, reject) { |
|
|
|
|
if (this._resolve.push) { |
|
|
|
|
this._resolve.push(resolve); |
|
|
|
@ -34,6 +58,14 @@ class TesseractJob {
@@ -34,6 +58,14 @@ class TesseractJob {
|
|
|
|
|
return this; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* catch |
|
|
|
|
* |
|
|
|
|
* @name catch |
|
|
|
|
* @function register a function to call when there is an error |
|
|
|
|
* @access public |
|
|
|
|
* @param {function} reject - callback function for error |
|
|
|
|
*/ |
|
|
|
|
catch(reject) { |
|
|
|
|
if (this._reject.push) { |
|
|
|
|
this._reject.push(reject); |
|
|
|
@ -43,24 +75,58 @@ class TesseractJob {
@@ -43,24 +75,58 @@ class TesseractJob {
|
|
|
|
|
return this; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* progress |
|
|
|
|
* |
|
|
|
|
* @name progress |
|
|
|
|
* @function register a function to show progress of the recognition, |
|
|
|
|
* use res.progress to print the message |
|
|
|
|
* @access public |
|
|
|
|
* @param {function} fn - callback function for progress information |
|
|
|
|
*/ |
|
|
|
|
progress(fn) { |
|
|
|
|
this._progress.push(fn); |
|
|
|
|
return this; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* finally |
|
|
|
|
* |
|
|
|
|
* @name finally |
|
|
|
|
* @function registry a callback function for final |
|
|
|
|
* @access public |
|
|
|
|
* @param {function} fn - callback function for final |
|
|
|
|
*/ |
|
|
|
|
finally(fn) { |
|
|
|
|
this._finally.push(fn); |
|
|
|
|
return this; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* send |
|
|
|
|
* |
|
|
|
|
* @name send |
|
|
|
|
* @function send specific action with payload a worker |
|
|
|
|
* @access public |
|
|
|
|
* @param {string} action - action to trigger, should be "recognize" or "detect" |
|
|
|
|
* @param {object} payload - data to be consumed |
|
|
|
|
*/ |
|
|
|
|
send(action, payload) { |
|
|
|
|
adapter.sendPacket(this._instance, { |
|
|
|
|
adapter.sendPacket(this._worker, { |
|
|
|
|
jobId: this.id, |
|
|
|
|
action, |
|
|
|
|
payload, |
|
|
|
|
}); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* handle |
|
|
|
|
* |
|
|
|
|
* @name handle |
|
|
|
|
* @function execute packet action |
|
|
|
|
* @access public |
|
|
|
|
* @param {object} packet action and payload to handle |
|
|
|
|
*/ |
|
|
|
|
handle(packet) { |
|
|
|
|
const { data } = packet; |
|
|
|
|
let runFinallyCbs = false; |
|
|
|
@ -74,13 +140,13 @@ class TesseractJob {
@@ -74,13 +140,13 @@ class TesseractJob {
|
|
|
|
|
} |
|
|
|
|
}); |
|
|
|
|
this._resolve = data; |
|
|
|
|
this._instance._dequeue(); |
|
|
|
|
this._worker.dequeue(); |
|
|
|
|
runFinallyCbs = true; |
|
|
|
|
} else if (packet.status === 'reject') { |
|
|
|
|
if (this._reject.length === 0) console.error(data); |
|
|
|
|
this._reject.forEach(fn => fn(data)); |
|
|
|
|
this._reject = data; |
|
|
|
|
this._instance._dequeue(); |
|
|
|
|
this._worker.dequeue(); |
|
|
|
|
runFinallyCbs = true; |
|
|
|
|
} else if (packet.status === 'progress') { |
|
|
|
|
this._progress.forEach(fn => fn(data)); |
|
|
|
|