Workers Event and EventTarget Get Closer to the Web Spec!
Hi, it's Shii-chan!
Cloudflare ChangelogToday's news is about Workers events moving closer to browser behavior.
What was announced?
The Cloudflare Changelog announced that the Workers runtime's Event, EventTarget and related APIs are now closer to their web platform specifications.
There are five changes:
isTrustedis now correcton<type>handlers fire in registration orderMessageEventconstructor improvementsCloseEventandErrorEventconstructor improvementsMessagePortstarts when you add a message listener
The story so far
Some behavior used to differ from the specs:
- User-constructed events incorrectly reported
isTrustedastrue onmessage,onabortand similar handlers always fired beforeaddEventListener()listeners, no matter when they were assigned- A
MessagePortonly started when you assignedonmessage
What changes
Code written for browsers, and libraries that assume spec-compliant behavior, should be more likely to work as expected on Workers!
Dive Deep
isTrusted
User-constructed events such as new MessageEvent(...), new CustomEvent(...), new ErrorEvent(...) and new CloseEvent(...) now report isTrusted as false.
Handler ordering
Event handler attributes such as onmessage and onabort now fire in registration order relative to addEventListener() calls, just like in browsers.
Constructors
new MessageEvent(type)works without an init object, anddatadefaults tonullper specMessageEventInitnow supportsorigin,lastEventId,sourceandportsin addition todataCloseEventInitandErrorEventInitaccept the standardEventInitmembersbubbles,cancelableandcomposed
MessagePort
Adding a listener with addEventListener("message", ...) now starts the port.
Wrap-up
isTrustedisfalsefor user-constructed eventson<type>handlers fire in registration orderMessageEvent,CloseEventandErrorEventconstructors are closer to the spec- A
MessagePortstarts once you add a message listener
This is for anyone sharing code between browsers and Workers, or porting event-heavy libraries!