Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

worker: use fake MessageEvent for port.onmessage #26082

Closed
wants to merge 1 commit into
base: master
from

Conversation

Projects
None yet
7 participants
@addaleax
Copy link
Member

addaleax commented Feb 13, 2019

Instead of passing the payload for Workers directly to .onmessage,
perform something more similar to what the browser API provides,
namely create an event object with a .data property.

This does not make MessagePort implement the EventTarget API, nor
does it implement the full MessageEvent API, but it would make
such extensions non-breaking changes if we desire them at
some point in the future.

(This would be a breaking change if Workers were not experimental.
Currently, this method is also undocumented and only exists with
the idea of enabling some degree of Web compatibility.)

Checklist
  • make -j4 test (UNIX), or vcbuild test (Windows) passes
  • tests and/or benchmarks are included
  • commit message follows commit guidelines
worker: use fake MessageEvent for port.onmessage
Instead of passing the payload for Workers directly to `.onmessage`,
perform something more similar to what the browser API provides,
namely create an event object with a `.data` property.

This does not make `MessagePort` implement the `EventTarget` API, nor
does it implement the full `MessageEvent` API, but it would make
such extensions non-breaking changes if we desire them at
some point in the future.

(This would be a breaking change if Workers were not experimental.
Currently, this method is also undocumented and only exists with
the idea of enabling some degree of Web compatibility.)
@addaleax

This comment has been minimized.

Copy link
Member Author

addaleax commented Feb 13, 2019

@surma

This comment has been minimized.

Copy link

surma commented Feb 14, 2019

I can't comment much on the implementation, but I'm very supportive of the event-ish object for easier web interop and future-proofing the API.

Thanks for doing this, @addaleax!

@addaleax

This comment has been minimized.

Copy link
Member Author

addaleax commented Feb 14, 2019

@addaleax

This comment has been minimized.

Copy link
Member Author

addaleax commented Feb 15, 2019

Landed in 5adda2c

@addaleax addaleax closed this Feb 15, 2019

@addaleax addaleax deleted the addaleax:worker-onmessage-messageevent branch Feb 15, 2019

addaleax added a commit that referenced this pull request Feb 15, 2019

worker: use fake MessageEvent for port.onmessage
Instead of passing the payload for Workers directly to `.onmessage`,
perform something more similar to what the browser API provides,
namely create an event object with a `.data` property.

This does not make `MessagePort` implement the `EventTarget` API, nor
does it implement the full `MessageEvent` API, but it would make
such extensions non-breaking changes if we desire them at
some point in the future.

(This would be a breaking change if Workers were not experimental.
Currently, this method is also undocumented and only exists with
the idea of enabling some degree of Web compatibility.)

PR-URL: #26082
Reviewed-By: Gus Caplan <me@gus.host>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Denys Otrishko <shishugi@gmail.com>

addaleax added a commit that referenced this pull request Feb 16, 2019

worker: use fake MessageEvent for port.onmessage
Instead of passing the payload for Workers directly to `.onmessage`,
perform something more similar to what the browser API provides,
namely create an event object with a `.data` property.

This does not make `MessagePort` implement the `EventTarget` API, nor
does it implement the full `MessageEvent` API, but it would make
such extensions non-breaking changes if we desire them at
some point in the future.

(This would be a breaking change if Workers were not experimental.
Currently, this method is also undocumented and only exists with
the idea of enabling some degree of Web compatibility.)

PR-URL: #26082
Reviewed-By: Gus Caplan <me@gus.host>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Denys Otrishko <shishugi@gmail.com>

@BridgeAR BridgeAR referenced this pull request Feb 26, 2019

Merged

v11.11.0 proposal #26322

rvagg added a commit that referenced this pull request Feb 28, 2019

worker: use fake MessageEvent for port.onmessage
Instead of passing the payload for Workers directly to `.onmessage`,
perform something more similar to what the browser API provides,
namely create an event object with a `.data` property.

This does not make `MessagePort` implement the `EventTarget` API, nor
does it implement the full `MessageEvent` API, but it would make
such extensions non-breaking changes if we desire them at
some point in the future.

(This would be a breaking change if Workers were not experimental.
Currently, this method is also undocumented and only exists with
the idea of enabling some degree of Web compatibility.)

PR-URL: #26082
Reviewed-By: Gus Caplan <me@gus.host>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Denys Otrishko <shishugi@gmail.com>

BridgeAR added a commit that referenced this pull request Mar 5, 2019

2019-03-06, Version 11.11.0 (Current)
Notable Changes

* n-api:
  * Implement date object (Jarrod Connolly)
    #25917
* util:
  * Add compact depth mode for `util.inspect()` (Ruben Bridgewater)
    #26269
* worker:
  * Improve integration with native addons (Anna Henningsen)
    #26175
  * Use fake MessageEvent for port.onmessage (Anna Henningsen)
    #26082

BridgeAR added a commit that referenced this pull request Mar 5, 2019

2019-03-06, Version 11.11.0 (Current)
Notable Changes

* n-api:
  * Implement date object (Jarrod Connolly)
    #25917
* util:
  * Add compact depth mode for `util.inspect()` (Ruben Bridgewater)
    #26269
* worker:
  * Improve integration with native addons (Anna Henningsen)
    #26175
  * MessagePort.prototype.onmessage takes arguments closer to the Web
    specification now (Anna Henningsen)
    #26082

Trott pushed a commit to Trott/io.js that referenced this pull request Mar 6, 2019

2019-03-06, Version 11.11.0 (Current)
Notable Changes

* n-api:
  * Implement date object (Jarrod Connolly)
    nodejs#25917
* util:
  * Add compact depth mode for `util.inspect()` (Ruben Bridgewater)
    nodejs#26269
* worker:
  * Improve integration with native addons (Anna Henningsen)
    nodejs#26175
  * MessagePort.prototype.onmessage takes arguments closer to the Web
    specification now (Anna Henningsen)
    nodejs#26082
@julienw

This comment has been minimized.

Copy link

julienw commented Mar 22, 2019

hey @addaleax, it seems that this changes the api when calling postMessage on the worker instance (and so onmessage inside the worker), but I don't see it changes the other way, when calling postMessage from inside the worker and getting the result in onmessage on the worker instance. Is it expected? Do you have any plan to implement this change as well?

@surma

This comment has been minimized.

Copy link

surma commented Mar 22, 2019

@julienw I just filed #26856 for that :)

@julienw

This comment has been minimized.

Copy link

julienw commented Mar 22, 2019

Ah I realize that in our code we use on('message', handler), and not onmessage indeed. But the general comment about implementing the structure { data: XXX } still applies :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.