|
42 | 42 | document.getElementById('status').innerHTML = status;
|
43 | 43 | }
|
44 | 44 |
|
| 45 | +// This worker will run the application javascript code, |
| 46 | +// making sure that it's run in an environment without a global |
| 47 | +// document, to make it consistent with the JSC executor environment. |
| 48 | +var worker = new Worker('debuggerWorker.js'); |
| 49 | + |
45 | 50 | var messageHandlers = {
|
46 | 51 | // This method is a bit hacky. Catalyst asks for a new clean JS runtime.
|
47 | 52 | // The easiest way to do this is to reload this page. That also means that
|
48 | 53 | // web socket connection will be lost. To send reply back we need to remember
|
49 |
| - // message id |
| 54 | + // message id. |
| 55 | + // This message also needs to be handled outside of the worker, since the worker |
| 56 | + // doesn't have access to local storage. |
50 | 57 | 'prepareJSRuntime': function(message) {
|
51 | 58 | window.onbeforeunload = undefined;
|
52 | 59 | window.localStorage.setItem('sessionID', message.id);
|
53 | 60 | window.location.reload();
|
54 | 61 | },
|
55 |
| - 'executeApplicationScript': function(message, sendReply) { |
56 |
| - for (var key in message.inject) { |
57 |
| - window[key] = JSON.parse(message.inject[key]); |
58 |
| - } |
59 |
| - loadScript(message.url, sendReply.bind(null, null)); |
| 62 | + 'executeApplicationScript': function(message) { |
| 63 | + worker.postMessage(message); |
60 | 64 | },
|
61 |
| - 'executeJSCall': function(message, sendReply) { |
62 |
| - var returnValue = null; |
63 |
| - try { |
64 |
| - if (window && window.require) { |
65 |
| - var module = window.require(message.moduleName); |
66 |
| - returnValue = module[message.moduleMethod].apply(module, message.arguments); |
67 |
| - } |
68 |
| - } finally { |
69 |
| - sendReply(JSON.stringify(returnValue)); |
70 |
| - } |
| 65 | + 'executeJSCall': function(message) { |
| 66 | + worker.postMessage(message); |
71 | 67 | }
|
72 | 68 | };
|
73 | 69 |
|
|
89 | 85 | return;
|
90 | 86 | }
|
91 | 87 |
|
92 |
| - var sendReply = function(result) { |
93 |
| - ws.send(JSON.stringify({replyID: object.id, result: result})); |
94 |
| - }; |
95 | 88 | var handler = messageHandlers[object.method];
|
96 | 89 | if (handler) {
|
97 |
| - handler(object, sendReply); |
| 90 | + handler(object); |
98 | 91 | } else {
|
99 | 92 | console.warn('Unknown method: ' + object.method);
|
100 | 93 | }
|
|
107 | 100 | window.localStorage.removeItem('sessionID');
|
108 | 101 | debuggerSetTimeout(connectToDebuggerProxy, 100);
|
109 | 102 | };
|
| 103 | + |
| 104 | + worker.onmessage = function(message) { |
| 105 | + ws.send(JSON.stringify(message.data)); |
| 106 | + } |
110 | 107 | }
|
111 | 108 |
|
112 | 109 | connectToDebuggerProxy();
|
113 | 110 |
|
114 |
| -function loadScript(src, callback) { |
115 |
| - var script = document.createElement('script'); |
116 |
| - script.type = 'text/javascript'; |
117 |
| - script.src = src; |
118 |
| - script.onload = callback; |
119 |
| - document.head.appendChild(script); |
120 |
| -} |
121 |
| - |
122 | 111 | })();
|
123 | 112 | </script>
|
124 | 113 | <style type="text/css">
|
|
0 commit comments