Commits

Cat's Eye Technologies committed 7ae4307

Implement a Web Worker for the Pixley evaluator.

  • Participants
  • Parent commits 634dcf9

Comments (0)

Files changed (3)

File impl/pixley.js/demo/pixley.html

 <button id="load">Load</button>
 <button id="edit">Edit</button>
 <button id="start">Start</button>
+<button id="kill">Kill</button>
 <button id="stop">Stop</button>
 <button id="step">Step</button>
 <button onclick="c.wrapIt();">Wrap it!</button>
   c.depictor = new PixleyDepictor();
   c.depictor.init(document.getElementById('canvas'));
   c.click_load();
+
+  document.getElementById('kill').onclick = function(e) {
+    c.kill();
+  };
+
+
 </script>

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

         this.pixleyInterpreter = cfg.pixleyInterpreter || '???';
         this.display = cfg.display;
         this.output = cfg.output;
-        this.finished = false;
         this.setStatus('Ready.');
     };
 
         }
     };
 
-    this.step = function() {
-        if (this.finished) return;
+    this.start = function() {
         this.setStatus('Evaluating...');
-        var result = evalPixley(this.ast, {});
-        this.output.innerHTML = depict(result);
-        this.finished = true;
-        this.setStatus('Done.');
-        this.draw();
+
+        this.worker = new Worker("../src/pixley-worker.js");
+        var $this = this;
+        this.worker.addEventListener('message', function(e) {
+            $this.output.innerHTML = e.data;
+            $this.setStatus('Done.');
+            $this.draw();
+            $this.worker = undefined;
+        });
+        this.worker.postMessage(["eval", depict(this.ast)]);
+    };
+
+    this.kill = function() {
+        if (this.worker) {
+            this.worker.terminate();
+            this.worker = undefined;
+            this.setStatus('Terminated.');
+        }
     };
 
     this.load = function(text) {
         p.init(text);
         this.ast = p.parse();
         if (this.ast) {
-            this.finished = false;
             this.setStatus('Program loaded.');
         } else {
             errorHandler.error("Can't parse your Pixley program");
-            this.finished = true;
             this.setStatus('Parsing error!');
         }
         this.output.innerHTML = '';

File impl/pixley.js/src/pixley-worker.js

+importScripts('pixley.js');
+
+addEventListener('message', function(e) {
+    if (e.data[0] === 'eval') {
+        postMessage(depict(runPixley(e.data[1])));
+    }
+});