| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (C) 2014 Google Inc. All rights reserved. | 2 * Copyright (C) 2014 Google Inc. All rights reserved. |
| 3 * | 3 * |
| 4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
| 5 * modification, are permitted provided that the following conditions are | 5 * modification, are permitted provided that the following conditions are |
| 6 * met: | 6 * met: |
| 7 * | 7 * |
| 8 * * Redistributions of source code must retain the above copyright | 8 * * Redistributions of source code must retain the above copyright |
| 9 * notice, this list of conditions and the following disclaimer. | 9 * notice, this list of conditions and the following disclaimer. |
| 10 * * Redistributions in binary form must reproduce the above | 10 * * Redistributions in binary form must reproduce the above |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 42 | 42 |
| 43 var callback; | 43 var callback; |
| 44 /** @type {!Promise<!Worker|!SharedWorker>} */ | 44 /** @type {!Promise<!Worker|!SharedWorker>} */ |
| 45 this._workerPromise = new Promise(fulfill => callback = fulfill); | 45 this._workerPromise = new Promise(fulfill => callback = fulfill); |
| 46 | 46 |
| 47 /** @type {!Worker|!SharedWorker} */ | 47 /** @type {!Worker|!SharedWorker} */ |
| 48 var worker; | 48 var worker; |
| 49 var isSharedWorker = !!workerName; | 49 var isSharedWorker = !!workerName; |
| 50 if (isSharedWorker) { | 50 if (isSharedWorker) { |
| 51 worker = new SharedWorker(url, workerName); | 51 worker = new SharedWorker(url, workerName); |
| 52 worker.port.onmessage = onMessage; | 52 worker.port.onmessage = onMessage.bind(this); |
| 53 } else { | 53 } else { |
| 54 worker = new Worker(url); | 54 worker = new Worker(url); |
| 55 worker.onmessage = onMessage; | 55 worker.onmessage = onMessage.bind(this); |
| 56 } | 56 } |
| 57 // Hold a reference to worker until the promise is resolved. |
| 58 // Otherwise the worker could be GCed. |
| 59 this._workerProtect = worker; |
| 57 | 60 |
| 58 /** | 61 /** |
| 59 * @param {!Event} event | 62 * @param {!Event} event |
| 63 * @this {WebInspector.Worker} |
| 60 */ | 64 */ |
| 61 function onMessage(event) | 65 function onMessage(event) |
| 62 { | 66 { |
| 63 console.assert(event.data === "workerReady"); | 67 console.assert(event.data === "workerReady"); |
| 64 if (isSharedWorker) | 68 if (isSharedWorker) |
| 65 worker.port.onmessage = null; | 69 worker.port.onmessage = null; |
| 66 else | 70 else |
| 67 worker.onmessage = null; | 71 worker.onmessage = null; |
| 68 callback(worker); | 72 callback(worker); |
| 73 this._workerProtect = null; |
| 69 } | 74 } |
| 70 } | 75 } |
| 71 | 76 |
| 72 WebInspector.Worker.prototype = { | 77 WebInspector.Worker.prototype = { |
| 73 /** | 78 /** |
| 74 * @param {*} message | 79 * @param {*} message |
| 75 */ | 80 */ |
| 76 postMessage: function(message) | 81 postMessage: function(message) |
| 77 { | 82 { |
| 78 this._workerPromise.then(postToWorker.bind(this)); | 83 this._workerPromise.then(postToWorker.bind(this)); |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 138 */ | 143 */ |
| 139 function setOnError(worker) | 144 function setOnError(worker) |
| 140 { | 145 { |
| 141 if (worker.port) | 146 if (worker.port) |
| 142 worker.port.onerror = listener; | 147 worker.port.onerror = listener; |
| 143 else | 148 else |
| 144 worker.onerror = listener; | 149 worker.onerror = listener; |
| 145 } | 150 } |
| 146 } | 151 } |
| 147 } | 152 } |
| OLD | NEW |