Рефакторинг

This commit is contained in:
Book Pauk
2019-10-28 23:30:02 +07:00
parent b64985349e
commit 9ebdbc81d0
4 changed files with 25 additions and 12 deletions

View File

@@ -1,7 +1,7 @@
const fs = require('fs-extra');
const path = require('path');
const workerState = require('./workerState');
const WorkerState = require('./WorkerState');//singleton
const FileDownloader = require('./FileDownloader');
const FileDecompressor = require('./FileDecompressor');
const BookConverter = require('./BookConverter');
@@ -20,6 +20,7 @@ class ReaderWorker {
this.config.tempPublicDir = `${config.publicDir}/tmp`;
fs.ensureDirSync(this.config.tempPublicDir);
this.workerState = new WorkerState();
this.down = new FileDownloader();
this.decomp = new FileDecompressor();
this.bookConverter = new BookConverter(this.config);
@@ -106,8 +107,8 @@ class ReaderWorker {
}
loadBookUrl(opts) {
const workerId = workerState.generateWorkerId();
const wState = workerState.getControl(workerId);
const workerId = this.workerState.generateWorkerId();
const wState = this.workerState.getControl(workerId);
wState.set({state: 'start'});
this.loadBook(opts, wState);

View File

@@ -3,10 +3,18 @@ const utils = require('./utils');
const cleanInterval = 3600; //sec
const cleanAfterLastModified = cleanInterval - 60; //sec
let instance = null;
//singleton
class WorkerState {
constructor() {
this.states = {};
setTimeout(this.cleanStates.bind(this), cleanInterval*1000);
if (!instance) {
this.states = {};
this.cleanStates();
instance = this;
}
return instance;
}
generateWorkerId() {
@@ -51,6 +59,4 @@ class WorkerState {
}
}
const workerState = new WorkerState();
module.exports = workerState;
module.exports = WorkerState;