Browse Source

Add eslint & rewrite src/index.js

pull/265/head
Jerome Wu 6 years ago
parent
commit
1e80e42a6f
  1. 11
      .eslintrc
  2. 29
      examples/node/basic.js
  3. 23
      examples/node/detect.js
  4. 1507
      package-lock.json
  5. 8
      package.json
  6. 142
      src/index.js

11
.eslintrc

@ -0,0 +1,11 @@
{
"extends": "airbnb",
"env": {
"browser": true,
"node": true
},
"rules": {
"no-underscore-dangle": 0,
"no-console": 0
}
}

29
examples/node/basic.js

@ -1,15 +1,18 @@
// replace this with require('tesseract.js') // replace this with require('tesseract.js')
var Tesseract = require('../../'), const path = require('path');
image = require('path').resolve(__dirname, 'cosmic.png'); const { TesseractWorker } = require('../../');
Tesseract.recognize(image) const image = path.resolve(__dirname, 'cosmic.png');
.then(data => { const tessWorker = new TesseractWorker();
console.log('then\n', data.text)
}) tessWorker.recognize(image)
.catch(err => { .then((data) => {
console.log('catch\n', err); console.log('then\n', data.text);
}) })
.finally(e => { .catch((err) => {
console.log('finally\n'); console.log('catch\n', err);
process.exit(); })
}); .finally(() => {
console.log('finally\n');
process.exit();
});

23
examples/node/detect.js

@ -1,12 +1,15 @@
// replace this with require('tesseract.js') // replace this with require('tesseract.js')
var Tesseract = require('../../'), const path = require('path');
image = require('path').resolve(__dirname, 'cosmic.png'); const { TesseractWorker } = require('../../');
Tesseract.detect(image) const image = path.resolve(__dirname, 'cosmic.png');
.progress(function(info){ const tessWorker = new TesseractWorker();
console.log(info);
}) tessWorker.detect(image)
.then(function(data){ .progress((info) => {
console.log('done', data); console.log(info);
process.exit(); })
}) .then((data) => {
console.log('done', data);
process.exit();
});

1507
package-lock.json generated

File diff suppressed because it is too large Load Diff

8
package.json

@ -22,15 +22,21 @@
"browserify": "^13.1.0", "browserify": "^13.1.0",
"concurrently": "^3.1.0", "concurrently": "^3.1.0",
"envify": "^3.4.1", "envify": "^3.4.1",
"eslint": "^5.9.0",
"eslint-config-airbnb": "^17.1.0",
"eslint-plugin-import": "^2.14.0",
"eslint-plugin-jsx-a11y": "^6.1.2",
"eslint-plugin-react": "^7.11.1",
"http-server": "^0.9.0", "http-server": "^0.9.0",
"pako": "^1.0.3", "pako": "^1.0.3",
"uglify-js": "^3.4.9", "uglify-js": "^3.4.9",
"watchify": "^3.7.0" "watchify": "^3.7.0"
}, },
"dependencies": { "dependencies": {
"check-types": "^7.4.0",
"file-type": "^3.8.0", "file-type": "^3.8.0",
"isomorphic-fetch": "^2.2.1",
"is-url": "1.2.2", "is-url": "1.2.2",
"isomorphic-fetch": "^2.2.1",
"jpeg-js": "^0.2.0", "jpeg-js": "^0.2.0",
"level-js": "^2.2.4", "level-js": "^2.2.4",
"node-fetch": "^1.6.3", "node-fetch": "^1.6.3",

142
src/index.js

@ -1,75 +1,91 @@
const adapter = require('./node/index.js') const check = require('check-types');
const circularize = require('./common/circularize.js') const adapter = require('./node/index.js');
const circularize = require('./common/circularize.js');
const TesseractJob = require('./common/job'); const TesseractJob = require('./common/job');
const version = require('../package.json').version;
const create = function(workerOptions = {}){
var worker = new TesseractWorker(Object.assign({}, adapter.defaultOptions, workerOptions));
worker.create = create;
worker.version = version;
return worker;
}
class TesseractWorker { class TesseractWorker {
constructor(workerOptions){ constructor(workerOptions = {}) {
this.worker = null; this.worker = null;
this.workerOptions = workerOptions; this.workerOptions = Object.assign({}, adapter.defaultOptions, workerOptions);
this._currentJob = null; this._currentJob = null;
this._queue = []; this._queue = [];
} }
recognize(image, options = {}){
return this._delay(job => {
if (typeof options === 'string') options = {lang: options}
options.lang = options.lang || 'eng';
job._send('recognize', { image, options, workerOptions: this.workerOptions }); recognize(image, options = { lang: 'eng' }) {
}) return this._delay((job) => {
} job._send(
detect(image, options = {}){ 'recognize',
return this._delay(job => { {
job._send('detect', { image, options, workerOptions: this.workerOptions }); image,
}) options: check.string(options)
} ? { lang: options || 'eng' }
: options,
workerOptions: this.workerOptions,
},
);
});
}
terminate(){ detect(image, options = {}) {
if(this.worker) adapter.terminateWorker(this); return this._delay((job) => {
this.worker = null; job._send(
this._currentJob = null; 'detect',
this._queue = []; {
} image,
options,
workerOptions: this.workerOptions,
},
);
});
}
_delay(fn){ terminate() {
if(!this.worker) this.worker = adapter.spawnWorker(this, this.workerOptions); if (this.worker) {
adapter.terminateWorker(this);
}
this.worker = null;
this._currentJob = null;
this._queue = [];
}
var job = new TesseractJob(this); _delay(fn) {
this._queue.push(e => { if (check.null(this.worker)) {
this._queue.shift(); this.worker = adapter.spawnWorker(this, this.workerOptions);
this._currentJob = job; }
fn(job);
});
if(!this._currentJob) this._dequeue();
return job;
}
_dequeue(){ const job = new TesseractJob(this);
this._currentJob = null; this._queue.push(() => {
if(this._queue.length){ this._queue.shift();
this._queue[0](); this._currentJob = job;
} fn(job);
} });
if (check.null(this._currentJob)) {
this._dequeue();
}
return job;
}
_recv(packet){ _dequeue() {
if(packet.status === 'resolve' && packet.action === 'recognize'){ this._currentJob = null;
packet.data = circularize(packet.data); if (this._queue.length) {
} this._queue[0]();
}
}
if(this._currentJob.id === packet.jobId){ _recv(packet) {
this._currentJob._handle(packet) if (this._currentJob.id === packet.jobId) {
} else { this._currentJob._handle({
console.warn('Job ID ' + packet.jobId + ' not known.') data: packet.status === 'resolve' && packet.action === 'recognize'
} ? circularize(packet.data)
} : packet.data,
...packet,
});
} else {
console.warn(`Job ID ${packet.jobId} not known.`);
}
}
} }
module.exports = create(); module.exports = {
TesseractWorker,
};

Loading…
Cancel
Save