Добавлена возможность использования HeavyCalc как singleton
This commit is contained in:
@@ -86,10 +86,17 @@ class CalcThread {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//singleton
|
||||||
|
let instance = null;
|
||||||
|
|
||||||
class HeavyCalc {
|
class HeavyCalc {
|
||||||
constructor(opts = {}) {
|
constructor(opts = {}) {
|
||||||
|
const singleton = opts.singleton || false;
|
||||||
|
|
||||||
|
if (singleton && instance)
|
||||||
|
return instance;
|
||||||
|
|
||||||
this.threads = opts.threads || 1;
|
this.threads = opts.threads || 1;
|
||||||
this.singleton = opts.singleton || false;
|
|
||||||
this.terminated = false;
|
this.terminated = false;
|
||||||
|
|
||||||
this.workers = [];
|
this.workers = [];
|
||||||
@@ -99,6 +106,10 @@ class HeavyCalc {
|
|||||||
this.workers.push(worker);
|
this.workers.push(worker);
|
||||||
this.load.push(0);
|
this.load.push(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (singleton) {
|
||||||
|
instance = this;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async run(params) {
|
async run(params) {
|
||||||
|
|||||||
Reference in New Issue
Block a user