Synchronously Wait for Message in Web-Worker

前端 未结 3 1479
温柔的废话
温柔的废话 2021-02-13 05:50

Is there some way to synchronously wait for or check for a new message in a web-worker?

I have a large complicated body of code (compiled LLVM from emscripten) that I c

相关标签:
3条回答
  • 2021-02-13 06:02

    A crude hack to do this could be to use a synchronous API, such as the FileSystem API, and use a temporary file as a synchronising mechanism.

    In your example, the emscripten code could issue a synchronous read on a file, while the UI thread writes to the same file, effectively unblocking the worker.

    0 讨论(0)
  • 2021-02-13 06:11

    Can you break the sequence as follow without refactoring?

    wrk.onmessage = function(e) {
        if (e.data.step === 'initial input') {
            doInitialWork(e.data.inputs)
        } else if (e.data.step === 'ui input') {
            doTheRest(e.data.uiData)
        }
    }
    

    The onmessage event will be blocked until the initial step execution stack is complete and the critical ui bits will only launch when and if the info is available.

    0 讨论(0)
  • 2021-02-13 06:13

    No, unfortunately. There was some discussion of adding a way to block on a message from the parent page, but it went nowhere. I'll see if I can dig up the relevant mailing list thread.

    0 讨论(0)
提交回复
热议问题