Browse Source

Add comments

pull/265/head
Jerome Wu 7 years ago
parent
commit
69042fc1bf
  1. 76
      src/common/TesseractJob.js
  2. 30
      src/common/TesseractWorker.js

76
src/common/TesseractJob.js

@ -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));

30
src/common/TesseractWorker.js

@ -104,6 +104,20 @@ class TesseractWorker { @@ -104,6 +104,20 @@ class TesseractWorker {
}
}
/**
* dequeue
*
* @name dequeue
* @function dequeue and execute the rear job
* @access public
*/
dequeue() {
this._currentJob = null;
if (this._queue.length) {
this._queue[0]();
}
}
/**
* terminate
*
@ -166,24 +180,10 @@ class TesseractWorker { @@ -166,24 +180,10 @@ class TesseractWorker {
fn(job);
});
if (check.null(this._currentJob)) {
this._dequeue();
this.dequeue();
}
return job;
}
/**
* _dequeue
*
* @name _dequeue
* @function dequeue and execute the rear job
* @access private
*/
_dequeue() {
this._currentJob = null;
if (this._queue.length) {
this._queue[0]();
}
}
}
module.exports = TesseractWorker;

Loading…
Cancel
Save