aboutsummaryrefslogtreecommitdiff
path: root/js/preloader/PreloaderWorker.js
diff options
context:
space:
mode:
authorJose Antonio Marquez2012-03-15 06:31:50 -0700
committerJose Antonio Marquez2012-03-15 06:31:50 -0700
commit88796fb80e042aa0d2d4d22b5c15bceda592e949 (patch)
treef42b69bd846970c29d69d29590fbae8c0e1b2844 /js/preloader/PreloaderWorker.js
parent7a9e7ff2e113c95aada64cfccbc140fa77599d57 (diff)
parente6bfd7fb3d3324bea5dbc05350832a9e25c6c00d (diff)
downloadninja-88796fb80e042aa0d2d4d22b5c15bceda592e949.tar.gz
Merge branch 'refs/heads/Ninja-Internal' into FileIO
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