Skip to content

Instantly share code, notes, and snippets.

@derek
Created August 17, 2012 02:06
Show Gist options
  • Select an option

  • Save derek/3375276 to your computer and use it in GitHub Desktop.

Select an option

Save derek/3375276 to your computer and use it in GitHub Desktop.
Loading YUI from the CDN into a Web Worker
YUI().use('array-extras', 'gallery-async', 'gallery-yql-rest-client', 'node', function (Y) {
Y.Async.runAll(Y.Array.map([
'http://yui.yahooapis.com/3.5.1/build/yui/yui-min.js'
].concat(new Y.Loader({
ignoreRegistered: true,
require: [
'json-stringify'
]
}).resolve(true).js), function (url) {
return function (success) {
Y.YQLRESTClient.request({
method: 'get',
url: url
}, function (result) {
success(result.response);
});
};
})).on('complete', function (eventFacade) {
var blobBuilder = new (Y.config.win.BlobBuilder || Y.config.win.MozBlobBuilder)(),
webWorker;
blobBuilder.append(eventFacade.value.join('\n') + '\n' + Y.one('#my-script').getHTML());
webWorker = new Worker(Y.config.win.URL.createObjectURL(blobBuilder.getBlob()));
webWorker.onmessage = function (message) {
Y.one('body').append('<p>' + message.data + '</p>');
};
});
});
YUI().use('array-extras', 'gallery-async', 'gallery-yql-rest-client', 'node', function (Y) {
// Create a Loader instance to easily build the required URLs
var loader = new Y.Loader({
combine: true,
ignoreRegistered: true,
require: [
'json-stringify'
]
})
// Include the dependencies as well
var urls = loader.resolve(true).js;
// Don't forget the seed file. Push it onto the front
urls.unshift('http://yui.yahooapis.com/3.5.1/build/yui/yui-min.js');
// Create the async command queue that will fetch all the scripts
var queue = Y.Array.map(urls, function (url) {
return (function (success) {
Y.YQLRESTClient.request({
method: 'get',
url: url
}, function (result) {
success(result.response);
});
});
});
// Executed after the entire queue is complete
var onComplete = function (eventFacade) {
var blobBuilder = new (Y.config.win.BlobBuilder || Y.config.win.MozBlobBuilder)(),
yuiScripts = eventFacade.value.join('\n'),
myScript = Y.one('#my-script').get('text'),
blob, objURL, webWorker;
// Combine the YUI module scripts with my script at the end
blobBuilder.append(yuiScripts + '\n' + myScript);
// Fetch a blob of everything
blob = blobBuilder.getBlob();
// Get a URL to represent the script blob
objectURL = Y.config.win.URL.createObjectURL(blob);
// Create the worker and onmessage listener
webWorker = new Worker(objectURL);
webWorker.onmessage = function (message) {
Y.one('body').append('<p>' + message.data + '</p>');
};
};
// Execute the command queue
Y.Async.runAll(queue).on('complete', onComplete);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment