Permalink
Browse files

Fixed example by putting python,js in a worker. fix #8

  • Loading branch information...
1 parent 841ee7c commit 0268bc5cb05889f8e8dee0cc73143a6236b44609 @amasad amasad committed Apr 29, 2012
Showing with 51 additions and 20 deletions.
  1. +21 −20 dist/index.html
  2. +30 −0 dist/worker.js
View
41 dist/index.html
@@ -2,37 +2,38 @@
<html>
<head>
<title>Empythoned</title>
- <script src="python.opt.js"></script>
<script>
function start() {
- var input = document.getElementById('input');
- var output = document.getElementById('output');
- var button = document.getElementById('button');
+ var input = document.getElementById('input')
+ , output = document.getElementById('output')
+ , button = document.getElementById('button')
+ , worker = new Worker('worker.js')
+ , loaded = false
+ , handler = function (e) {
+ if (!loaded) {
+ loaded = true;
+ button.value = "Execute";
+ input.disabled = false;
+ button.disabled = false;
+ return;
+ }
+ output.value += e.data;
+ };
+
- Python.initialize(null, function(chr) {
- if (chr !== null) output.value += String.fromCharCode(chr);
- });
+ worker.addEventListener('message', handler, false);
button.onclick = function() {
- if (Python.isFinished(input.value)) {
- output.value = '';
- var result = Python.eval(input.value);
- if (result !== null && result !== undefined) {
- output.value += '\n--------------------------\nResult: ' + result;
- }
- output.scrollTop = output.scrollHeight;
- } else {
- output.value = 'Command not finished.';
- }
- }
+ worker.postMessage(input.value);
+ };
};
window.onload = start;
</script>
</head>
<body>
<h1>Empythoned Demo</h1>
- <textarea id="input" style="font-family: monospace; width: 80%" rows="8"></textarea>
- <input id="button" type="button" value="Execute" style="display: block; margin: auto" />
+ <textarea disabled="true" id="input" style="font-family: monospace; width: 80%" rows="8">print 'Hello World'</textarea>
+ <input disabled="true" id="button" type="button" value="loading........." style="display: block; margin: auto" />
<textarea id="output" style="font-family: monospace; width: 80%" rows="8"></textarea>
<hr>
</body>
View
30 dist/worker.js
@@ -0,0 +1,30 @@
+;(function () {
+ self.console = {
+ log: function () {}
+ };
+ importScripts('python.opt.js');
+
+ self.prompt = function () {
+ return 'Input not supported in demo';
+ }
+ Python.initialize(null, function(chr) {
+ if (chr !== null)
+ postMessage(String.fromCharCode(chr));
+ });
+
+ var msgHandler = function (e) {
+ if (Python.isFinished(e.data)) {
+ var result = Python.eval(e.data);
+ if (result !== null && result !== undefined) {
+ postMessage('\n--------------------------\nResult: ' + result);
+ }
+
+ } else {
+ postMessage('\nCommand not finished.\n');
+ }
+ };
+
+ addEventListener('message', msgHandler, false);
+
+ postMessage('Loaded');
+})();

0 comments on commit 0268bc5

Please sign in to comment.