From e1fb1b966e97ce4685708684fd02522c47a96d31 Mon Sep 17 00:00:00 2001 From: Jason Stafford Date: Thu, 31 Jan 2019 15:07:48 -0800 Subject: [PATCH] add a generic webworker file --- Makefile | 5 ++++- src/webworker.js | 23 +++++++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) create mode 100644 src/webworker.js diff --git a/Makefile b/Makefile index 059f9db0e..1709c9a8b 100644 --- a/Makefile +++ b/Makefile @@ -57,7 +57,8 @@ all: build/pyodide.asm.js \ build/renderedhtml.css \ build/test.data \ build/packages.json \ - build/test.html + build/test.html \ + build/webworker.js build/pyodide.asm.js: src/main.bc src/jsimport.bc src/jsproxy.bc src/js2python.bc \ @@ -112,6 +113,8 @@ build/test.html: src/test.html build/renderedhtml.css: src/renderedhtml.less lessc $< $@ +build/webworker.js: src/webworker.js + cp $< $@ test: all pytest test/ -v diff --git a/src/webworker.js b/src/webworker.js new file mode 100644 index 000000000..ab718b607 --- /dev/null +++ b/src/webworker.js @@ -0,0 +1,23 @@ +importScripts('./pyodide.js') + +var onmessage = function(e) { // eslint-disable-line no-unused-vars + languagePluginLoader.then(() => { + const data = e.data; + const keys = Object.keys(data); + for (let key of keys) { + if (key !== 'python') { + // Keys other than python must be arguments for the python script. + // Set them on self, so that `from js import key` works. + self[key] = data[key]; + } + } + self.pyodide.runPythonAsync(data.python, () => {}).then((results) => { + self.postMessage({results}); + }).catch((err) => { + // if you prefer messages with the error + self.postMessage({error: err.message}); + // if you prefer onerror events + // setTimeout(() => { throw err; }); + }); + }); +}