aboutsummaryrefslogtreecommitdiff
path: root/js/preloader/PreloaderWorker.js
diff options
context:
space:
mode:
authorArmen Kesablyan2012-03-19 11:01:00 -0700
committerArmen Kesablyan2012-03-19 11:01:00 -0700
commit60107392554fa993a16e748f7281be423055cd19 (patch)
tree408b7b9422265d29df2c37eb6a120c09812abf2c /js/preloader/PreloaderWorker.js
parent725bb869618b9e0ebb2820a24bca5d1cf53a4810 (diff)
parent954f5a13e371febcb1c0fb8015c577ee51c23130 (diff)
downloadninja-60107392554fa993a16e748f7281be423055cd19.tar.gz
Merge branch 'refs/heads/master' into DataBinding
Diffstat (limited to 'js/preloader/PreloaderWorker.js')
-rwxr-xr-xjs/preloader/PreloaderWorker.js95
1 files changed, 0 insertions, 95 deletions
diff --git a/js/preloader/PreloaderWorker.js b/js/preloader/PreloaderWorker.js
deleted file mode 100755
index 1b5b58ed..00000000
--- a/js/preloader/PreloaderWorker.js
+++ /dev/null
@@ -1,95 +0,0 @@
1/* <copyright>
2This file contains proprietary software owned by Motorola Mobility, Inc.<br/>
3No rights, expressed or implied, whatsoever to this software are provided by Motorola Mobility, Inc. hereunder.<br/>
4(c) Copyright 2011 Motorola Mobility, Inc. All Rights Reserved.
5</copyright> */
6
7//PreloaderWorker makes the file downloading asynchronous. Preloader will continue to do script execution synchronously. PreloaderWorker allows script execution to start soon after the first download.
8//Note: File importing is probably not CPU intensive enough to run on a separate thread. But worth a test.
9// Multi-threading will be more useful for math-intensive webGL or canvas calculations.
10
11self.onmessage = function(e){
12 var msgJson;
13 if(e && e.data){
14 msgJson = e.data;
15 }
16 //workerLog("worker received this: "+JSON.stringify(msgJson));
17 if(msgJson){
18 switch(msgJson.command){
19 case "start":
20 //workerLog('WORKER STARTED');
21
22 downloadFiles(msgJson.jsFiles, msgJson.cssFiles, msgJson.baseUrl);
23 break;
24 case 'stop':
25 workerLog('WORKER STOPPED');
26 self.close(); // Terminates the worker.
27 break;
28 default:
29 workerLog('Unknown command');
30 }
31 }
32};
33
34 self.onerror = function(e){
35 workerLog("worker error: "+ e.message+" @file "+e.filename+" @line# "+e.lineno);
36 self.close(); // Terminates the worker.
37 }
38
39 var fileCounter = 0;
40 var fileArrayLength = 0;
41
42 var xhrDownload = function(fileUrl, fileType, i){
43 var that = self,
44 req = new XMLHttpRequest();
45
46 if(fileType === "js"){
47 //workerLog("Worker will download: fileUrl="+fileUrl+" :: fileType="+fileType+" :: fileIndex="+i);
48 req.overrideMimeType && req.overrideMimeType("application/javascript");
49 }else if(fileType === "css"){
50 //workerLog("Worker will download: fileUrl="+fileUrl+" :: fileType="+fileType+" :: fileIndex="+i);
51 req.overrideMimeType && req.overrideMimeType("text/css");
52 }
53 req.onreadystatechange = function() {
54 if (req.readyState === 4) {
55 if ((req.status === 200 || (req.status === 0 && req.responseText))) {
56 fileCounter++;
57 //workerLog("Status:"+req.status+" : fileUrl="+fileUrl+" :: fileIndex="+i);
58 that.postMessage({command:"newFile", url:fileUrl, fileType:fileType, fileIndex:i, fileContent:req.responseText});
59
60 //trying to clear some memory after last file is processed
61 req.responseText = null;
62 req = null;
63
64 if(fileCounter === fileArrayLength){
65 //workerLog("done downloading...shutting down this worker");
66 that.close();
67 }
68
69 } else {
70 workerLog("Status:"+req.status+" :: Unable to load "+fileUrl);
71 }
72 }
73 }
74 req.open("GET", fileUrl, true);
75 req.send();
76}
77
78var downloadFiles = function(jsFiles, cssFiles, baseUrl){
79 var fileType, fileUrl;
80 fileArrayLength = jsFiles.concat(cssFiles).length;
81 for(var i=0;i<jsFiles.length;i++){
82 fileUrl = baseUrl + jsFiles[i].url;
83 fileType = jsFiles[i].type;
84 xhrDownload(fileUrl, fileType, i);
85 }
86 for(var j=0;j<cssFiles.length;j++){
87 fileUrl = baseUrl + cssFiles[j].url;
88 fileType = cssFiles[j].type;
89 xhrDownload(fileUrl, fileType, j);
90 }
91}
92
93 var workerLog = function(msg){
94 self.postMessage({command:"log", logMsg:msg});
95 } \ No newline at end of file