User:Jeblad/Javascript and Mediawiki/Worker

Jump to navigation Jump to search

A Worker or WebWorker is a separate and limited executing environment set up so it is possible to pass messages back and fort between the world executable (often the browser) and the separate worker environment. The separation creates a simple and safe means to execute lengthy processing without locking up the main UI thread. The child itself is often implemented as a separate OS process. There is no shared objects between the world and the children, or between any child and subchildren, and there is no synchronization involved.

World executable
This is typically from the Special:MyPage/vector.js page. The uri variable holds a complete link to the script page that constructs the worker process. After constructing the worker process an onmessage handler and an onerror handler is set up, and after that a message is posted to the worker. This message is although only delivered when the worker process is ready to handle the message.
var uri = mw.util.wikiScript( 'index' )
    + '?' + $.param({
        title: 'User:Jeblad/worker.js',
        action: 'raw',
        ctype: 'text/javascript',
        smaxage: '21600',
        maxage: '86400'
var worker = new Worker(uri);
worker.onmessage = function (event){"World:" +;
worker.onerror = function (event){
    console.log(event.filename + ' [' + event.lineno + ']: ' + event.message);
worker.postMessage("Is there anybody in there" );
Child executable
This is typically from a Special:MyPage/worker.js page. As soon as the worker process is set up the onmessage handler becomes active and the posted message from the world process will be handled. It will then fire a new message back to the world with a small addition. Uncommenting the last line in the onmessage call will generate an error which is reported to the onerror handler in the world executable.
self.onmessage = function(event) {  
    self.postMessage( + ' -> out there!');