diff --git a/src/worker/meta-worker.js b/src/worker/meta-worker.js index 94c9793b3..1432eab70 100644 --- a/src/worker/meta-worker.js +++ b/src/worker/meta-worker.js @@ -27,13 +27,13 @@ onmessage = function(evt) { if(m.onrequest && evt.data && evt.data.message === "systemjs-worker-request") { try { - let result = await m.onrequest(evt.data) + let result = await m.onrequest(...evt.data.arguments) // console.log("ON MESSAGE result " + result) return postMessage({message: "systemjs-worker-response", - name: evt.data.name, id: evt.data.id, response: result}) + id: evt.data.id, response: result}) } catch(e) { return postMessage({message: "systemjs-worker-response", - name: "error", id: evt.data.id, response: "" + e}) + error: "" + e, id: evt.data.id}) } } diff --git a/src/worker/systemjs-worker.js b/src/worker/systemjs-worker.js index a50e4e807..81bf41282 100644 --- a/src/worker/systemjs-worker.js +++ b/src/worker/systemjs-worker.js @@ -22,6 +22,9 @@ export default class SystemjsWorker { /*MD The meta-worker is the actual worker, that is generic will load the actual systemjs module, which contains the client code MD*/ this.idCounter = 1 this.resolveRequestForId = new Map() + + this.timeout=1000 + this.metaworker = new Worker("src/worker/meta-worker.js"); /*MD ## bootstrap onmessage MD*/ // console.log("sytemjs-worker new: " + url) @@ -75,26 +78,26 @@ export default class SystemjsWorker { console.log("handleRequest ", msg) var resolve = this.resolveRequestForId.get(msg.id) if (!resolve) { - throw new Error("No resolve func for message " + msg.name + ", " + msg.id + ", " + msg.response) + throw new Error("No resolve func for message " + msg.id + ", " + msg.response) } this.resolveRequestForId.set(msg.id, null) resolve(msg.response) } - async request(name, data, timeout=1000) { - + async postRequest(...data) { + var id = this.newId() var promise = new Promise((resolve, reject) => { this.resolveRequestForId.set(id, resolve) - this.postMessage({message: "systemjs-worker-request", id: id, name: name, arguments: data}) + this.postMessage({message: "systemjs-worker-request", id: id, arguments: data}) var start = performance.now() - if (timeout === Infinity || timeout < 0 || timeout === null || timeout === undefined) { + if (this.timeout === Infinity || this.timeout < 0 || this.timeout === null || this.timeout === undefined) { // do nothing } else { setTimeout(() => { var unhandledRequestResolve = this.resolveRequestForId.get(id) if (unhandledRequestResolve) reject({error: "request timeout after " + (performance.now() - start) + "ms"}) - }, timeout) + }, this.timeout) } }) return promise diff --git a/test/worker/systemjs-worker-test-sum-request-worker.js b/test/worker/systemjs-worker-test-sum-request-worker.js index e03e25fe7..224795ab1 100644 --- a/test/worker/systemjs-worker-test-sum-request-worker.js +++ b/test/worker/systemjs-worker-test-sum-request-worker.js @@ -1,26 +1,12 @@ -export async function onrequest(msg) { - if (msg.name === "sum") { - var array = msg.arguments - let result = array[0] + array[1] +export async function onrequest(name, a, b) { + if (name === "sum") { + let result = a + b return result } - throw new Error("request not understood: " + msg) + throw new Error("request not understood: " + arguments) } - -// export function onmessage(evt) { -// if(evt.data && evt.data.message === "systemjs-worker-request") { -// if (evt.data.name === "sum") { -// var array = evt.data.arguments -// let result = array[0] + array[1] -// // console.log("ON MESSAGE result " + result) -// return postMessage({message: "systemjs-worker-response", name: evt.data.name, id: evt.data.id, response: result}) -// } -// return postMessage({error: "unhandled systemjs-worker-request " + evt}) -// } -// return postMessage({error: "could not handle message " + evt}) -// } \ No newline at end of file diff --git a/test/worker/systemjs-worker-test.js b/test/worker/systemjs-worker-test.js index f55323e6b..acf1b7f52 100644 --- a/test/worker/systemjs-worker-test.js +++ b/test/worker/systemjs-worker-test.js @@ -43,7 +43,7 @@ describe('SystemJSWorker', () => { it('sums', async () => { - var result = await myworker.request("sum", [3, 4]); + var result = await myworker.postRequest("sum", 3, 4); expect(result, "result").equals(7) })