Apparently WorkerGlobalScope supports these methods:
self.addEventListener()
self.removeEventListener()
self.dispatchEvent()
The first two make sense in light of the need to listen to incoming 'message' events from the Worker's parent... but what can I do with dispatchEvent?
I was trying out some code where one 'manager' Worker looks after a pool of 'worker' Workers and it seemed like it would be useful to set up some custom events.
Most (all?) the JS libraries which provide custom event helpers seem to use DOM events, maybe attached to the 'document' object. But there's no document or DOM in a Worker of course. The only docs for dispatchEvent I can find online are for the DOM version.
I tried a couple of ways of naively using dispatchEvent() within my Worker:
- Code: Select all
dispatchEvent('myevent', {'msg':'hello'});
and in FF 3.6.6 I get:
Could not convert JavaScript argument arg 0 [nsIDOMEventTarget.dispatchEvent]
...I guess it looks for a native EventType that matches 'myevent'.
Also tried:
- Code: Select all
dispatchEvent({
'type': 'myevent',
'data': 'hello',
'target': this
});
just to very roughly try to mock an event object...
This seems to bypass the event listener in my Worker and go straight through to the onmessage handler in my parent page as a 'message' event.
(in Chrome, both approaches result in a 'Uncaught Error: UNSPECIFIED_EVENT_TYPE_ERR: DOM Events Exception 0' error)
So... is the presence of dispatchEvent method in the Worker just a side-effect of implementing the postMessage functionality and not meant to be used by user code?