nolanlawson / promise-worker

Promise-based messaging for Web Workers and Service Workers
Apache License 2.0
478 stars 40 forks source link

Worker handler getting called twice #26

Closed subhranshudas closed 6 years ago

subhranshudas commented 6 years ago

Hi,

I am using promise-worker. I am triggering events using setInterval.I have noticed that for first few set of events (1+) the worker handler is called twice.

i am using webpack & worker-loader set up

webpack

{
        test: /\.*worker\.js$/,
        use: {
          loader: 'worker-loader',
          options: { name: 'Worker.[hash].js' }
        }
}

worker.js

import registerPromiseWorker from 'promise-worker/register';

function messagingSystem(messageReceived) {
   const { ACTION, DATA } = messageReceived;
  if (ACTION === 'EVENT_TRIGGER') {
    console.log('\n\nworker gets called receiving message from the PARENT thread: ', ACTION);
    console.log(JSON.stringify(DATA), '\n');
  }
}

registerPromiseWorker(messagingSystem);

worker-index.js

import PromiseWorker from 'promise-worker';
import WebWorker from './worker';

const webWorker = new WebWorker();
const worker = new PromiseWorker(webWorker); // promisifying worker

export default worker;

app.js

import worker from './worker-index';

setInterval(
            () => {
                worker.postMessage({
                    'ACTION': 'EVENT_TRIGGER',
                   'DATA': uuid.v4()
                 });
            },800
        );

What i am observing is the console.logs in the web worker are repeated twice as -

worker gets called receiving message from the PARENT thread: EVENT_TRIGGER
12345678

worker gets called receiving message from the PARENT thread:
12345678

worker gets called receiving message from the PARENT thread: EVENT_TRIGGER
987654321

worker gets called receiving message from the PARENT thread:
987654321

The second time the ACTION is not printed, but the DATA is. But when i set up counters in both APP and the WORKER, the number of events printed is equal!!!

Not sure what to make of it.

Regards.

nolanlawson commented 6 years ago

Thanks for reporting the bug! Is there any chance you can make a small Gist or GitHub repo to reproduce this? I'm not very familiar with worker-loader and so I'm afraid I won't be able to reproduce what you describe.

subhranshudas commented 6 years ago

@nolanlawson thanks for the reply, let me create a small repo to replicate it...

pavloDeshko commented 6 years ago

from the code above if messageRecieved.ACTION is blank, the if-clause should never be entered on the first place ! my bet is your setup has some alternative logging system that also prints out the first argument passed to log function..

nolanlawson commented 6 years ago

Closing old issues, please reopen if we can find a repro! Thanks