These forums are currently read-only due to receiving more spam than actual discussion. Sorry.

It is currently Sat Dec 02, 2017 4:14 pm Advanced search

(Custom) events within Web Workers

Do you think the HTML spec should do something differently? You can discuss spec feedback here, but you should send it to the WHATWG mailing list or file a bug in the W3C bugzilla for it to be considered.

(Custom) events within Web Workers

Postby anentropic » Thu Jul 15, 2010 12:31 pm

I'm not really sure what is possible regarding events *within* a Worker.

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?
anentropic
<h6>
 
Posts: 3
Joined: Thu Jul 15, 2010 11:48 am

Postby zcorpan » Fri Jul 16, 2010 9:43 am

dispatchEvent is there as a side-effect of adding addEventListener. It's not usable since there's no way to create events.

I've proposed that it should be possible to create events using constructors like new Event(), but it didn't go anywhere.
http://lists.w3.org/Archives/Public/www ... /0136.html
zcorpan
<article>
 
Posts: 807
Joined: Tue Feb 06, 2007 8:29 pm
Location: Sweden


Return to Feedback on the Specs

Who is online

Users browsing this forum: No registered users and 0 guests