Commits

Cat's Eye Technologies committed d346da2

Only load the Worker as needed, to reduce the # of requests.

Comments (0)

Files changed (1)

impl/pixley.js/src/pixley-controller.js

         this.display = cfg.display;
         this.output = cfg.output;
         this.workerURL = cfg.workerURL || "../src/pixley-worker.js";
+        this.loadWorker();
+        this.running = false;
         this.setStatus('Ready.');
     };
 
+    this.loadWorker = function() {
+        if (!this.worker) {
+            this.worker = new Worker(this.workerURL);
+        }
+    };
+
     this.setStatus = function(text) {
         if (this.status) {
             this.status.innerHTML = text;
     };
 
     this.start = function() {
+        if (this.running) return;
         this.setStatus('Evaluating...');
 
-        this.worker = new Worker(this.workerURL);
+        this.loadWorker();
         var $this = this;
         this.worker.addEventListener('message', function(e) {
             $this.output.innerHTML = e.data;
             $this.setStatus('Done.');
+            $this.running = false;
             $this.draw();
-            $this.worker = undefined;
         });
         this.worker.postMessage(["eval", depict(this.ast)]);
+        this.running = true;
     };
 
     this.stop = function() {
-        if (this.worker) {
+        if (this.running && this.worker) {
             this.worker.terminate();
+            this.running = false;
             this.worker = undefined;
             this.setStatus('Terminated.');
         }