diff options
author | Pierre Frisch | 2011-12-22 07:25:50 -0800 |
---|---|---|
committer | Valerio Virgillito | 2012-01-27 11:18:17 -0800 |
commit | b89a7ee8b956c96a1dcee995ea840feddc5d4b27 (patch) | |
tree | 0f3136ab0ecdbbbed6a83576581af0a53124d6f1 /js/preloader/PreloaderWorker.js | |
parent | 2401f05d1f4b94d45e4568b81fc73e67b969d980 (diff) | |
download | ninja-b89a7ee8b956c96a1dcee995ea840feddc5d4b27.tar.gz |
First commit of Ninja to ninja-internal
Signed-off-by: Valerio Virgillito <rmwh84@motorola.com>
Diffstat (limited to 'js/preloader/PreloaderWorker.js')
-rw-r--r-- | js/preloader/PreloaderWorker.js | 95 |
1 files changed, 95 insertions, 0 deletions
diff --git a/js/preloader/PreloaderWorker.js b/js/preloader/PreloaderWorker.js new file mode 100644 index 00000000..1b5b58ed --- /dev/null +++ b/js/preloader/PreloaderWorker.js | |||
@@ -0,0 +1,95 @@ | |||
1 | /* <copyright> | ||
2 | This file contains proprietary software owned by Motorola Mobility, Inc.<br/> | ||
3 | No 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 | |||
11 | self.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 | |||
78 | var 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 | ||