rvagg/node-worker-farm

How to pass class instance as parameters to the child process

NeoyeElf opened this issue · 1 comments

runner.js:

function run(options, callback) {
    // some code
    options.user.getCurrentUser()
}
class UserService {
  currentUser() {
    return {id: 123};
  }
}

// ...

const workers = workerFarm(workerOptions, require.resolve('./runner'), ['run']);
const options = {
  str: 'aa'
  num: 1,
  user: new UserService(),
}
workers.run(options, (err, out) => {
  //do something
})

When I run the code above, And trying to exec options.user.currentUser() in the runner.js, it throws an error TypeError: options.user.currentUser is not a function. It seems the options.user be serialized after passed to the child process.

Is there some way to pass class instance to the child process?

I have realized it's impossible, close it...