From 17b103746e59c02342664c5750e645feb1d1fd48 Mon Sep 17 00:00:00 2001 From: Jerome Wu Date: Sun, 18 Nov 2018 21:29:38 +0800 Subject: [PATCH] Rewrite src/node/worker.js --- .eslintrc | 3 ++- src/node/worker.js | 33 +++++++++++++++++---------------- 2 files changed, 19 insertions(+), 17 deletions(-) diff --git a/.eslintrc b/.eslintrc index 0283e5f..fa01611 100644 --- a/.eslintrc +++ b/.eslintrc @@ -6,6 +6,7 @@ }, "rules": { "no-underscore-dangle": 0, - "no-console": 0 + "no-console": 0, + "global-require": 0 } } diff --git a/src/node/worker.js b/src/node/worker.js index bea5d5c..1531d34 100644 --- a/src/node/worker.js +++ b/src/node/worker.js @@ -1,19 +1,20 @@ -const workerUtils = require('../common/worker.js') +const check = require('check-types'); +const workerUtils = require('../common/worker'); -process.on('message', function(packet){ - workerUtils.dispatchHandlers(packet, obj => process.send(obj)) -}) +let TesseractCore = null; -var TesseractCore; -exports.getCore = function(req, res){ - if(!TesseractCore){ - res.progress({ status: 'loading tesseract core' }) - TesseractCore = require('tesseract.js-core') - res.progress({ status: 'loaded tesseract core' }) - } - return TesseractCore -} - -exports.getLanguageData = require('./lang.js') +process.on('message', (packet) => { + workerUtils.dispatchHandlers(packet, obj => process.send(obj)); +}); -workerUtils.setAdapter(module.exports); +workerUtils.setAdapter({ + getCore: (req, res) => { + if (check.null(TesseractCore)) { + res.progress({ status: 'loading tesseract core' }); + TesseractCore = require('tesseract.js-core'); + res.progress({ status: 'loaded tesseract core' }); + } + return TesseractCore; + }, + getLanguageData: require('./lang'), +});