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 = { |
11 /** | 11 /** |
12 * @return {!Promise} | 12 * @return {!Promise} |
13 */ | 13 */ |
14 dispose: function() { } | 14 dispose: function() { } |
15 } | 15 }; |
16 | 16 |
17 /** | 17 /** |
18 * @constructor | 18 * @constructor |
19 * @param {!ServicePort} port | 19 * @param {!ServicePort} port |
20 */ | 20 */ |
21 function ServiceDispatcher(port) | 21 function ServiceDispatcher(port) |
22 { | 22 { |
23 this._constructors = new Map(); | 23 this._constructors = new Map(); |
24 this._objects = new Map(); | 24 this._objects = new Map(); |
25 this._lastObjectId = 1; | 25 this._lastObjectId = 1; |
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
132 | 132 |
133 /** | 133 /** |
134 * @param {string} messageId | 134 * @param {string} messageId |
135 * @param {string} error | 135 * @param {string} error |
136 */ | 136 */ |
137 _sendErrorResponse: function(messageId, error) | 137 _sendErrorResponse: function(messageId, error) |
138 { | 138 { |
139 var message = { id: messageId, error: error }; | 139 var message = { id: messageId, error: error }; |
140 this._port.send(JSON.stringify(message)); | 140 this._port.send(JSON.stringify(message)); |
141 } | 141 } |
142 } | 142 }; |
143 | 143 |
144 /** | 144 /** |
145 * @constructor | 145 * @constructor |
146 * @param {!Port|!Worker} port | 146 * @param {!Port|!Worker} port |
147 * @implements {ServicePort} | 147 * @implements {ServicePort} |
148 */ | 148 */ |
149 function WorkerServicePort(port) | 149 function WorkerServicePort(port) |
150 { | 150 { |
151 this._port = port; | 151 this._port = port; |
152 this._port.onmessage = this._onMessage.bind(this); | 152 this._port.onmessage = this._onMessage.bind(this); |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
185 return Promise.resolve(); | 185 return Promise.resolve(); |
186 }, | 186 }, |
187 | 187 |
188 /** | 188 /** |
189 * @param {!MessageEvent} event | 189 * @param {!MessageEvent} event |
190 */ | 190 */ |
191 _onMessage: function(event) | 191 _onMessage: function(event) |
192 { | 192 { |
193 this._messageHandler(event.data); | 193 this._messageHandler(event.data); |
194 } | 194 } |
195 } | 195 }; |
196 | 196 |
197 var dispatchers = []; | 197 var dispatchers = []; |
198 var portInitialized = false; | 198 var portInitialized = false; |
199 /** @type {!Map<string, function(new:Service)>}*/ | 199 /** @type {!Map<string, function(new:Service)>}*/ |
200 var services = new Map(); | 200 var services = new Map(); |
201 | 201 |
202 /** | 202 /** |
203 * @param {string} serviceName | 203 * @param {string} serviceName |
204 * @param {function(new:Service)} constructor | 204 * @param {function(new:Service)} constructor |
205 */ | 205 */ |
(...skipping 27 matching lines...) Expand all Loading... |
233 | 233 |
234 function onNewPort(port) | 234 function onNewPort(port) |
235 { | 235 { |
236 var servicePort = new WorkerServicePort(port); | 236 var servicePort = new WorkerServicePort(port); |
237 var dispatcher = new ServiceDispatcher(servicePort); | 237 var dispatcher = new ServiceDispatcher(servicePort); |
238 dispatchers.push(dispatcher); | 238 dispatchers.push(dispatcher); |
239 for (var name of services.keys()) | 239 for (var name of services.keys()) |
240 dispatcher.registerObject(name, services.get(name)); | 240 dispatcher.registerObject(name, services.get(name)); |
241 } | 241 } |
242 } | 242 } |
OLD | NEW |