Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 /** | 5 /** |
| 6 * @interface | 6 * @interface |
| 7 */ | 7 */ |
| 8 function Service() { } | 8 function Service() { } |
| 9 | 9 |
| 10 Service.prototype = { | 10 Service.prototype = { |
| (...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 133 */ | 133 */ |
| 134 _sendErrorResponse: function(messageId, error) | 134 _sendErrorResponse: function(messageId, error) |
| 135 { | 135 { |
| 136 var message = { id: messageId, error: error }; | 136 var message = { id: messageId, error: error }; |
| 137 this._port.send(JSON.stringify(message)); | 137 this._port.send(JSON.stringify(message)); |
| 138 } | 138 } |
| 139 } | 139 } |
| 140 | 140 |
| 141 /** | 141 /** |
| 142 * @constructor | 142 * @constructor |
| 143 * @param {!Port} port | 143 * @param {!Port|!Worker} port |
| 144 * @implements {ServicePort} | 144 * @implements {ServicePort} |
| 145 */ | 145 */ |
| 146 function WorkerServicePort(port) | 146 function WorkerServicePort(port) |
| 147 { | 147 { |
| 148 this._port = port; | 148 this._port = port; |
| 149 this._port.onmessage = this._onMessage.bind(this); | 149 this._port.onmessage = this._onMessage.bind(this); |
| 150 this._port.onerror = console.error; | 150 this._port.onerror = console.error; |
| 151 } | 151 } |
| 152 | 152 |
| 153 WorkerServicePort.prototype = { | 153 WorkerServicePort.prototype = { |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 193 | 193 |
| 194 var dispatchers = []; | 194 var dispatchers = []; |
| 195 var portInitialized = false; | 195 var portInitialized = false; |
| 196 /** @type {!Map<string, function(new:Service)>}*/ | 196 /** @type {!Map<string, function(new:Service)>}*/ |
| 197 var services = new Map(); | 197 var services = new Map(); |
| 198 | 198 |
| 199 /** | 199 /** |
| 200 * @param {string} serviceName | 200 * @param {string} serviceName |
| 201 * @param {function(new:Service)} constructor | 201 * @param {function(new:Service)} constructor |
| 202 */ | 202 */ |
| 203 function initializeWorkerService(serviceName, constructor) | |
| 204 { | |
| 205 services.set(serviceName, constructor); | |
| 206 if (!dispatchers.length) { | |
| 207 var worker = /** @type {!Object} */(self); | |
|
dgozman
2016/10/14 03:14:58
@type {!Worker}
pfeldman
2016/10/14 17:29:20
JSCompiler won't let you do that...
| |
| 208 var servicePort = new WorkerServicePort(/** @type {!Worker} */(worker)); | |
| 209 var dispatcher = new ServiceDispatcher(servicePort); | |
| 210 dispatchers.push(dispatcher); | |
| 211 } | |
| 212 dispatchers[0].registerObject(serviceName, constructor); | |
| 213 } | |
| 214 | |
| 215 /** | |
| 216 * @param {string} serviceName | |
| 217 * @param {function(new:Service)} constructor | |
| 218 */ | |
| 203 function initializeSharedWorkerService(serviceName, constructor) | 219 function initializeSharedWorkerService(serviceName, constructor) |
| 204 { | 220 { |
| 205 services.set(serviceName, constructor); | 221 services.set(serviceName, constructor); |
| 206 | 222 |
| 207 if (!portInitialized) { | 223 if (!portInitialized) { |
| 208 portInitialized = true; | 224 portInitialized = true; |
| 209 Runtime.setSharedWorkerNewPortCallback(onNewPort); | 225 Runtime.setSharedWorkerNewPortCallback(onNewPort); |
| 210 } else { | 226 } else { |
| 211 for (var dispatcher of dispatchers) | 227 for (var dispatcher of dispatchers) |
| 212 dispatcher.registerObject(serviceName, constructor); | 228 dispatcher.registerObject(serviceName, constructor); |
| 213 } | 229 } |
| 214 | 230 |
| 215 function onNewPort(port) | 231 function onNewPort(port) |
| 216 { | 232 { |
| 217 var servicePort = new WorkerServicePort(port); | 233 var servicePort = new WorkerServicePort(port); |
| 218 var dispatcher = new ServiceDispatcher(servicePort); | 234 var dispatcher = new ServiceDispatcher(servicePort); |
| 219 dispatchers.push(dispatcher); | 235 dispatchers.push(dispatcher); |
| 220 for (var name of services.keys()) | 236 for (var name of services.keys()) |
| 221 dispatcher.registerObject(name, services.get(name)); | 237 dispatcher.registerObject(name, services.get(name)); |
| 222 } | 238 } |
| 223 } | 239 } |
| OLD | NEW |