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

Conversation

addaleax
Copy link
Member

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

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.)
@nodejs-github-bot nodejs-github-bot added c++ Issues and PRs that require attention from people who are familiar with C++. worker Issues and PRs related to Worker support. labels Feb 13, 2019
@addaleax
Copy link
Member Author

/cc @nodejs/workers @surma

@surma
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
Copy link
Member Author

addaleax commented Feb 14, 2019

CI: https://ci.nodejs.org/job/node-test-pull-request/20782/ (:heavy_check_mark:)

@addaleax addaleax added the author ready PRs that have at least one approval, no pending requests for changes, and a CI started. label Feb 15, 2019
@addaleax
Copy link
Member Author

Landed in 5adda2c

@addaleax addaleax closed this Feb 15, 2019
@addaleax addaleax deleted the worker-onmessage-messageevent branch February 15, 2019 22:21
addaleax added a commit that referenced this pull request Feb 15, 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.)

PR-URL: #26082
Reviewed-By: Gus Caplan <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Denys Otrishko <[email protected]>
addaleax added a commit that referenced this pull request Feb 16, 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.)

PR-URL: #26082
Reviewed-By: Gus Caplan <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Denys Otrishko <[email protected]>
@BridgeAR BridgeAR mentioned this pull request Feb 26, 2019
rvagg pushed a commit that referenced this pull request Feb 28, 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.)

PR-URL: #26082
Reviewed-By: Gus Caplan <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Denys Otrishko <[email protected]>
BridgeAR added a commit that referenced this pull request Mar 5, 2019
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
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
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
Copy link

surma commented Mar 22, 2019

@julienw I just filed #26856 for that :)

@julienw
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 :)

@UziTech
Copy link
Contributor

UziTech commented Mar 26, 2019

Just to make it clear, this changes the info passed to port.onmessage to an object with a .data property, but does not change the info passed to port.on('message')?

@addaleax
Copy link
Member Author

Just to make it clear, this changes the info passed to port.onmessage to an object with a .data property, but does not change the info passed to port.on('message')?

Yes, exactly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
author ready PRs that have at least one approval, no pending requests for changes, and a CI started. c++ Issues and PRs that require attention from people who are familiar with C++. worker Issues and PRs related to Worker support.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants