Skip to content

Commit

Permalink
playground: Only process the latest Web Worker request
Browse files Browse the repository at this point in the history
  • Loading branch information
shesek committed Nov 7, 2024
1 parent 896338d commit 9ff89c1
Showing 1 changed file with 6 additions and 9 deletions.
15 changes: 6 additions & 9 deletions web/js/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,18 @@ let minsc, pending

import('../pkg/index.js').then(module => {
minsc = module
if (pending) {
run(pending)
pending = null
}
if (pending) run(pending)
}).catch(console.error)

addEventListener('message', ({ data: req }) => {
if (!minsc) {
pending = req
} else {
run(req)
}
// Wait for the next event loop 'tick' before processing, so that we first read all queued messages
// (multiple could've been accumulated while we were `run()`ing) and only process the latest one
if (minsc && !pending) setTimeout(() => run(pending), 0)
pending = req
})

function run({ code, network }) {
pending = null
try {
const result = minsc.run_playground(code, network)
postMessage({ result })
Expand Down

0 comments on commit 9ff89c1

Please sign in to comment.