优化热更插件.
This commit is contained in:
@@ -8,12 +8,23 @@ class FileUploader {
|
||||
maxClients = 3;
|
||||
options = {};
|
||||
queue = [];
|
||||
idleCallback = null;
|
||||
loopId = 0;
|
||||
lastIdleState = true;
|
||||
|
||||
constructor() {
|
||||
this.loopId = setInterval(this.loop.bind(this), 200);
|
||||
}
|
||||
|
||||
setOption(options, maxThread) {
|
||||
this.options = options;
|
||||
this.maxClients = maxThread || 1;
|
||||
}
|
||||
|
||||
setStateListener(callback) {
|
||||
this.idleCallback = callback;
|
||||
}
|
||||
|
||||
prepare() {
|
||||
const currLen = Object.keys(this.ftps).length;
|
||||
logger.info('准备创建上传线程:', this.maxClients, currLen);
|
||||
@@ -30,6 +41,28 @@ class FileUploader {
|
||||
this.ftps[id].destroy();
|
||||
delete this.ftps[id];
|
||||
}
|
||||
if (this.loopId) {
|
||||
clearInterval(this.loopId)
|
||||
this.loopId = 0;
|
||||
}
|
||||
}
|
||||
|
||||
// event loop.
|
||||
loop() {
|
||||
const curState = this.checkState();
|
||||
if (curState != this.lastIdleState) {
|
||||
this.lastIdleState = curState;
|
||||
this.idleCallback && this.idleCallback(curState);
|
||||
}
|
||||
}
|
||||
|
||||
checkState() {
|
||||
let idleState = true;
|
||||
for (let i in this.ftps) {
|
||||
idleState = idleState && this.ftps[i].idleState;
|
||||
if (!idleState) break;
|
||||
}
|
||||
return idleState;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -71,6 +104,7 @@ class UploadThread {
|
||||
ready = false;
|
||||
stopped = false;
|
||||
options = null;
|
||||
idleState = true;
|
||||
|
||||
constructor(options) {
|
||||
this.options = options;
|
||||
@@ -89,19 +123,15 @@ class UploadThread {
|
||||
this.ready = false;
|
||||
const ftp = new Client();
|
||||
ftp.on('ready', () => {
|
||||
logger.log('ftp ready');
|
||||
this.ready = true;
|
||||
});
|
||||
ftp.on('close', () => {
|
||||
logger.log('ftp client has close');
|
||||
this.ready = false;
|
||||
});
|
||||
ftp.on('end', () => {
|
||||
logger.log('ftp client has end');
|
||||
this.ready = false;
|
||||
});
|
||||
ftp.on('error', (err) => {
|
||||
logger.log('ftp client has an error : ', JSON.stringify(err));
|
||||
this.ready = false;
|
||||
});
|
||||
ftp.connect(this.options);
|
||||
@@ -123,8 +153,10 @@ class UploadThread {
|
||||
while (!this.stopped) {
|
||||
if (!this.ready || this.queue.length <= 0) {
|
||||
await this.sleep(500);
|
||||
this.idleState = true;
|
||||
continue;
|
||||
}
|
||||
this.idleState = false;
|
||||
const once = this.queue[0];
|
||||
const err = await this.uploadOnce(this.ftp, once);
|
||||
if (!err) {
|
||||
|
Reference in New Issue
Block a user