joltup/react-native-threads

Got the project running, but `postmessage` never reaches my `worker.thread.js`

Genhain opened this issue · 4 comments

I had to manually install as automatic would never run. however i eventually got it running and have placed breakpoints in XCode and confirm that postmessage is being called but the said message never reaches my worker.thread.js

I also had to specify an absolute path for it to work like so

componentDidMount() {
    this.fetchPartiesAndPopulatePartyList()
    this.workerThread = new Thread('src/app/components/party/worker.thread.js')
    this.workerThread.onmessage = this.handleMessage;

    setTimeout(() => {
      this.workerThread.postMessage('Hello')
    }, 1000);
    
  }

otherwise it would say the file did not exist.

and here is my worker thread.js

import { self } from 'react-native-threads';
import Reactotron from 'reactotron-react-native'

console.tron = { log: Function.prototype };

if (__DEV__) {
  Reactotron
    .configure()
    .useReactNative()
    .connect();

  console.tron = Reactotron;
}

let count = 0;

self.onmessage = message => {
  console.tron.log(`THREAD: got message ${message}`);

  count++;

  self.postMessage(`Message #${count} from worker thread!`);
}

I have the same issue!! Did you manage to find a solution @Genhain?

@vishalnarkhede @Genhain did you find the solution for this issue?

@vishalnarkhede @Genhain
When the callback gets called from listeningDeviceEventEmitter.addListener('ThreadMessage', (message) => {
Doing a console of self it seems that onmessage is null thats why Im assuming self.onmessage is never called
{ onmessage: null, postMessage: [Function: postMessage] }

Can someone tell if this is true?

liran commented

In my case:

// main
// start a new react native JS process
const thread = new Thread('thread.js');
// send a message, strings only
thread.postMessage('run');


// thread
// listen for messages
self.onmessage = (message) => {
  console.log(message);
};

However, no message is received in the Thread.js.
My react-native version: 0.63.2