WinJSContrib.Messenger (NuGet package)

This component is a wrapper for facilitating communication between your code and a web worker or an iframe,
and wrap operations within promises

Within your app you will have something like :

var worker = new Worker('myworker.js');
var messenger = new MCNEXT.UI.Messenger(worker);
messenger.start('longRunningOperation', { foo: 'bar' }).done(function(arg){
	console.log(arg); //42
}, function(err){
	//error
});


In "myworker.js" you may have something like :
importScripts("//Microsoft.WinJS.2.0/js/base.js");
importScripts("/Scripts/winjscontrib/mcnext.ui.messenger.js");

var messenger = new MCNEXT.UI.Messenger(this);

messenger.longRunningOperation = function (arg) {
	//arg.foo == 'bar'
    return 42;
};


As you may have noticed, all event plumbing is managed by the wrapper, on both side.

The component also offer a convenience for smart use of workers. You write regular functions and
run them within a web worker. In this case, if web workers are not available (like with Android 4.0 to 4.3),
the function is still executed in the main thread.

For example :
var worker = new MCNEXT.UI.Messenger.SmartWorker();
smartWorker.importScripts('/Scripts/winjscontrib/mcnext.ui.utils.js').then(function () {
    return page.smartWorker.execute(function (a, b) {
        return (a + b) + ' & ' + MCNEXT.Utils.guid();
    }, 42, 42).then(function (result) {
        console.log('worker dynamic function result : ' + JSON.stringify(result));
        $('.smartworkerTreatmentMessage', page.element).text('worker dynamic function result : ' + JSON.stringify(result));
    });
});


In that case you do not even need to write a separate file for your worker. It will run with "mcnext.ui.messenger.worker.js".
The function given to "execute" is turned into a string and executed within the worker.

Last edited Oct 15, 2014 at 6:29 PM by gleborgne, version 3