Having a problem with web workers

Users and developers helping users with generic and technical Pale Moon issues on all operating systems.

Moderator: trava90

Forum rules
This board is for technical/general usage questions and troubleshooting for the Pale Moon browser only.
Technical issues and questions not related to the Pale Moon browser should be posted in other boards!
Please keep off-topic and general discussion out of this board, thank you!
thosrtanner
Lunatic
Lunatic
Posts: 395
Joined: 2014-05-10, 18:19
Location: UK

Having a problem with web workers

Unread post by thosrtanner » 2018-05-19, 16:30

I've been trying to use a web worker and I have this code:

In main window:

Code: Select all

let worker = new Worker("chrome://addon/content/workers/worker.js")
worker.addEventListener('message', e =>
      {
        console.log("done", e.data);
        worker.terminate();
      }, false);
worker.postMessage('5000')
and worker.js contains

Code: Select all

addEventListener('message', e => {
/**/console.log('Message received from main script', e)
  setTimeout(() => {
/**/console.log('Posting message back to main script')
    postMessage(12345);
  }, 2000);
});
but this is what I get in the log

Code: Select all

17:27:26.306 "Message received from main script" "[object MessageEvent]" worker.js:50:4
17:27:28.431 "Posting message back to main script" worker.js:52:4
17:27:28.665 "done" 12345 main.js:900
How am I meant to access the information from the main script?

Also, in passing, if I set the actual timeout to anything much over 2 seconds, the timeout doesn't expire
Last edited by thosrtanner on 2018-05-19, 18:09, edited 2 times in total.

JustOff

Re: Having a problem with web workers

Unread post by JustOff » 2018-05-19, 17:17

Try something like this:

Code: Select all

addEventListener('message', e => {
/**/console.log('Message received from main script', e)
  let postMessageLocal = postMessage;
  setTimeout(() => {
/**/console.log('Posting message back to main script')
    postMessageLocal(12345);
  }, 2000);
});

thosrtanner
Lunatic
Lunatic
Posts: 395
Joined: 2014-05-10, 18:19
Location: UK

Re: Having a problem with web workers

Unread post by thosrtanner » 2018-05-19, 18:08

Sadly, that doesn't fix either of my problems -

* The parameter to the message event handler appears to be the string "[object MessageEvent]" rather than the actual message event
* If the timeout is too large, it never fires
Last edited by thosrtanner on 2018-05-19, 18:09, edited 1 time in total.

JustOff

Re: Having a problem with web workers

Unread post by JustOff » 2018-05-19, 18:26

thosrtanner wrote:* The parameter to the message event handler appears to be the string "[object MessageEvent]" rather than the actual message event
"[object MessageEvent]" is not a string, try to print e.data and see.
* If the timeout is too large, it never fires
Does it work without setTimeout? Have you tried postMessage('12345')?

PS: Actually I don't have much experience with workers, so these are rather general considerations.
Last edited by JustOff on 2018-05-19, 18:27, edited 1 time in total.

thosrtanner
Lunatic
Lunatic
Posts: 395
Joined: 2014-05-10, 18:19
Location: UK

Re: Having a problem with web workers

Unread post by thosrtanner » 2018-05-19, 19:24

JustOff wrote:
thosrtanner wrote:* The parameter to the message event handler appears to be the string "[object MessageEvent]" rather than the actual message event
"[object MessageEvent]" is not a string, try to print e.data and see.
That's just evil

JustOff wrote:
thosrtanner wrote:* If the timeout is too large, it never fires
Does it work without setTimeout? Have you tried postMessage('12345')?

PS: Actually I don't have much experience with workers, so these are rather general considerations.
And as soon as I sort that problem with e.data, out it decides to work properly >.<

Anyway, many thanks for the help.
Last edited by thosrtanner on 2018-05-19, 19:25, edited 1 time in total.

JustOff

Re: Having a problem with web workers

Unread post by JustOff » 2018-05-19, 19:37

I'm glad it helped you 8-)

Locked