Skip to content

Commit

Permalink
postRequest and flat arguments
Browse files Browse the repository at this point in the history
SQUASHED: AUTO-COMMIT-src-worker-meta-worker.js,AUTO-COMMIT-src-worker-systemjs-worker.js,AUTO-COMMIT-test-worker-systemjs-worker-test.js,AUTO-COMMIT-test-worker-systemjs-worker-test-sum-request-worker.js,
  • Loading branch information
JensLincke committed Nov 14, 2023
1 parent aa24439 commit b868c9c
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 28 deletions.
6 changes: 3 additions & 3 deletions src/worker/meta-worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -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})
}
}

Expand Down
15 changes: 9 additions & 6 deletions src/worker/systemjs-worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
22 changes: 4 additions & 18 deletions test/worker/systemjs-worker-test-sum-request-worker.js
Original file line number Diff line number Diff line change
@@ -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})
// }
2 changes: 1 addition & 1 deletion test/worker/systemjs-worker-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
Expand Down

0 comments on commit b868c9c

Please sign in to comment.