Browse Source

Добавлена возможность использования HeavyCalc как singleton

Book Pauk 2 years ago
parent
commit
ab4d54cc85
1 changed files with 12 additions and 1 deletions
  1. 12 1
      server/core/HeavyCalc.js

+ 12 - 1
server/core/HeavyCalc.js

@@ -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) {