developit / web-worker

Consistent Web Workers in browser and Node.
https://npm.im/web-worker
Apache License 2.0
1.05k stars 55 forks source link

TypeError: Cannot set property currentTarget of #<Event> which has only a getter #49

Open josselinonduty opened 3 months ago

josselinonduty commented 3 months ago

Description

I am trying to use CustomEvent for communicating between the main thread and the worker thread(s). Worker.dispatchEvent(event) raises an error: TypeError: Cannot set property currentTarget of #<Event> which has only a getter

Reproduction

In the worker code,

const e = new CustomEvent("my-custom-event");
globalThis.dispatchEvent(e);

The issue is related to Event.target and Event.currentTarget being read-only values. Worker.dispatchEvent tries to redefine them after initialiation, hence causing this error.

System

Why am I not using v1.3.0 ? Issue with ES support in v1.3.0 - see #44