{"action":"created","issue":{"url":"https://api.github.com/repos/nodejs/help/issues/1558","repository_url":"https://api.github.com/repos/nodejs/help","labels_url":"https://api.github.com/repos/nodejs/help/issues/1558/labels{/name}","comments_url":"https://api.github.com/repos/nodejs/help/issues/1558/comments","events_url":"https://api.github.com/repos/nodejs/help/issues/1558/events","html_url":"https://github.com/nodejs/help/issues/1558","id":370478755,"node_id":"MDU6SXNzdWUzNzA0Nzg3NTU=","number":1558,"title":"Sending class instances to worker threads","user":{"login":"wilk","id":647020,"node_id":"MDQ6VXNlcjY0NzAyMA==","avatar_url":"https://avatars2.githubusercontent.com/u/647020?v=4","gravatar_id":"","url":"https://api.github.com/users/wilk","html_url":"https://github.com/wilk","followers_url":"https://api.github.com/users/wilk/followers","following_url":"https://api.github.com/users/wilk/following{/other_user}","gists_url":"https://api.github.com/users/wilk/gists{/gist_id}","starred_url":"https://api.github.com/users/wilk/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/wilk/subscriptions","organizations_url":"https://api.github.com/users/wilk/orgs","repos_url":"https://api.github.com/users/wilk/repos","events_url":"https://api.github.com/users/wilk/events{/privacy}","received_events_url":"https://api.github.com/users/wilk/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":0,"created_at":"2018-10-16T07:41:13Z","updated_at":"2018-10-18T09:09:57Z","closed_at":null,"author_association":"NONE","body":"<!-- Please remove this line and fill the template -->\r\n\r\n* **Node.js Version**: v10.11.0\r\n* **OS**: Mac OS X High Sierra 10.13.6\r\n* **Scope (install, code, runtime, meta, other?)**: runtime and code\r\n* **Module (and version) (if relevant)**: worker threads\r\n\r\nI'm trying to figure out how can I pass complex structures, like class instances, to worker threads.\r\nI tried using strings and just postMessage but well there's no an easy-to-use way to **serialize/deserialize class instances**.\r\nI read about SharedArrayBuffer but I don't know how to convert a class instance in pure bytes.\r\n\r\nHow can I do that?\r\n\r\nExample:\r\n\r\n```js\r\n// main thread\r\nclass Person {\r\n  constructor(name, surname) {\r\n    this.name = name\r\n    this.surname = surname\r\n  }\r\n\r\n  fullname() {\r\n    return `${this.name} ${this.surname}`\r\n  }\r\n\r\n  static whoAmI() {\r\n    return 'a Person'\r\n  }\r\n}\r\n\r\nconst foo = new Person('foo', 'bar')\r\nworker.postMessage(foo) // foo should be serialized\r\nworker2.postMessage(Person) // Person should be serialized\r\n```\r\n\r\n```js\r\n// worker thread\r\nparentPort.on('message', foo => {\r\n  const fullname = foo.fullname()\r\n\r\n  parentPort.postMessage(fullname)\r\n})\r\n```\r\n\r\n```js\r\n// worker2 thread\r\nparentPort.on('message', Person => {\r\n  const whoIs = Person.whoAmI()\r\n\r\n  parentPort.postMessage(whoIs)\r\n})\r\n```\r\n"},"comment":{"url":"https://api.github.com/repos/nodejs/help/issues/comments/430934545","html_url":"https://github.com/nodejs/help/issues/1558#issuecomment-430934545","issue_url":"https://api.github.com/repos/nodejs/help/issues/1558","id":430934545,"node_id":"MDEyOklzc3VlQ29tbWVudDQzMDkzNDU0NQ==","user":{"login":"gireeshpunathil","id":6447530,"node_id":"MDQ6VXNlcjY0NDc1MzA=","avatar_url":"https://avatars2.githubusercontent.com/u/6447530?v=4","gravatar_id":"","url":"https://api.github.com/users/gireeshpunathil","html_url":"https://github.com/gireeshpunathil","followers_url":"https://api.github.com/users/gireeshpunathil/followers","following_url":"https://api.github.com/users/gireeshpunathil/following{/other_user}","gists_url":"https://api.github.com/users/gireeshpunathil/gists{/gist_id}","starred_url":"https://api.github.com/users/gireeshpunathil/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/gireeshpunathil/subscriptions","organizations_url":"https://api.github.com/users/gireeshpunathil/orgs","repos_url":"https://api.github.com/users/gireeshpunathil/repos","events_url":"https://api.github.com/users/gireeshpunathil/events{/privacy}","received_events_url":"https://api.github.com/users/gireeshpunathil/received_events","type":"User","site_admin":false},"created_at":"2018-10-18T09:09:57Z","updated_at":"2018-10-18T09:09:57Z","author_association":"MEMBER","body":"$ cat 1558.js \r\n```js\r\nconst {\r\n  Worker, isMainThread, parentPort, workerData\r\n} = require('worker_threads');\r\n\r\nclass Person {\r\n  constructor(name, surname) {\r\n    this.name = name\r\n    this.surname = surname\r\n  }\r\n\r\n  fullname() {\r\n    return `${this.name} ${this.surname}`\r\n  }\r\n\r\n  static whoAmI() {\r\n    return 'a Person'\r\n  }\r\n}\r\n\r\nif (isMainThread) {\r\n\r\n  const foo = new Person('foo', 'bar')\r\n  const worker = new Worker(__filename, {});\r\n\r\n  worker.on('message', (m) => {\r\n    console.log(`worker message: ${JSON.stringify(m)}`)\r\n  })\r\n  worker.postMessage(foo) // foo is serialized\r\n} else {\r\n  parentPort.on('message', foo => {\r\n    console.log(`in worker, foo is like: ${JSON.stringify(foo)}`)\r\n    parentPort.postMessage(foo)\r\n  })\r\n}\r\n```\r\n\r\n$ node --experimental-worker 1558\r\n```trace\r\nin worker, foo is like: {\"name\":\"foo\",\"surname\":\"bar\"}\r\nworker message: {\"name\":\"foo\",\"surname\":\"bar\"}\r\n```\r\n^C\r\n$\r\n\r\nI believe the member functions are omitted upon serialization because the serializer defines a data-only specification. However, copying @addaleax to confirm."}}
{"actor":{"display_login":"gireeshpunathil"}}