| OLD | NEW |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 part of html; | 5 part of html; |
| 6 | 6 |
| 7 _serialize(var message) { | 7 _serialize(var message) { |
| 8 return new _JsSerializer().traverse(message); | 8 return new _JsSerializer().traverse(message); |
| 9 } | 9 } |
| 10 | 10 |
| (...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 92 var result = _call(_isolateId, _portId, serialized); | 92 var result = _call(_isolateId, _portId, serialized); |
| 93 return _deserialize(result); | 93 return _deserialize(result); |
| 94 } | 94 } |
| 95 | 95 |
| 96 static _call(int isolateId, int portId, var message) { | 96 static _call(int isolateId, int portId, var message) { |
| 97 var target = 'dart-port-$isolateId-$portId'; | 97 var target = 'dart-port-$isolateId-$portId'; |
| 98 // TODO(vsm): Make this re-entrant. | 98 // TODO(vsm): Make this re-entrant. |
| 99 // TODO(vsm): Set this up set once, on the first call. | 99 // TODO(vsm): Set this up set once, on the first call. |
| 100 var source = '$target-result'; | 100 var source = '$target-result'; |
| 101 var result = null; | 101 var result = null; |
| 102 var listener = (Event e) { | 102 window.on[source].first.then((Event e) { |
| 103 result = json.parse(_getPortSyncEventData(e)); | 103 result = json.parse(_getPortSyncEventData(e)); |
| 104 }; | 104 }); |
| 105 window.on[source].add(listener); | |
| 106 _dispatchEvent(target, [source, message]); | 105 _dispatchEvent(target, [source, message]); |
| 107 window.on[source].remove(listener); | |
| 108 return result; | 106 return result; |
| 109 } | 107 } |
| 110 | 108 |
| 111 bool operator==(var other) { | 109 bool operator==(var other) { |
| 112 return (other is _RemoteSendPortSync) && (_isolateId == other._isolateId) | 110 return (other is _RemoteSendPortSync) && (_isolateId == other._isolateId) |
| 113 && (_portId == other._portId); | 111 && (_portId == other._portId); |
| 114 } | 112 } |
| 115 | 113 |
| 116 int get hashCode => _isolateId >> 16 + _portId; | 114 int get hashCode => _isolateId >> 16 + _portId; |
| 117 } | 115 } |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 152 // ReceivePortSync directly. Across isolates (or from JS), an | 150 // ReceivePortSync directly. Across isolates (or from JS), an |
| 153 // EventListener can be used to communicate with the port indirectly. | 151 // EventListener can be used to communicate with the port indirectly. |
| 154 class ReceivePortSync { | 152 class ReceivePortSync { |
| 155 | 153 |
| 156 static Map<int, ReceivePortSync> _portMap; | 154 static Map<int, ReceivePortSync> _portMap; |
| 157 static int _portIdCount; | 155 static int _portIdCount; |
| 158 static int _cachedIsolateId; | 156 static int _cachedIsolateId; |
| 159 | 157 |
| 160 num _portId; | 158 num _portId; |
| 161 Function _callback; | 159 Function _callback; |
| 162 EventListener _listener; | 160 StreamSubscription _portSubscription; |
| 163 | 161 |
| 164 ReceivePortSync() { | 162 ReceivePortSync() { |
| 165 if (_portIdCount == null) { | 163 if (_portIdCount == null) { |
| 166 _portIdCount = 0; | 164 _portIdCount = 0; |
| 167 _portMap = new Map<int, ReceivePortSync>(); | 165 _portMap = new Map<int, ReceivePortSync>(); |
| 168 } | 166 } |
| 169 _portId = _portIdCount++; | 167 _portId = _portIdCount++; |
| 170 _portMap[_portId] = this; | 168 _portMap[_portId] = this; |
| 171 } | 169 } |
| 172 | 170 |
| 173 static int get _isolateId { | 171 static int get _isolateId { |
| 174 // TODO(vsm): Make this coherent with existing isolate code. | 172 // TODO(vsm): Make this coherent with existing isolate code. |
| 175 if (_cachedIsolateId == null) { | 173 if (_cachedIsolateId == null) { |
| 176 _cachedIsolateId = _getNewIsolateId(); | 174 _cachedIsolateId = _getNewIsolateId(); |
| 177 } | 175 } |
| 178 return _cachedIsolateId; | 176 return _cachedIsolateId; |
| 179 } | 177 } |
| 180 | 178 |
| 181 static String _getListenerName(isolateId, portId) => | 179 static String _getListenerName(isolateId, portId) => |
| 182 'dart-port-$isolateId-$portId'; | 180 'dart-port-$isolateId-$portId'; |
| 183 String get _listenerName => _getListenerName(_isolateId, _portId); | 181 String get _listenerName => _getListenerName(_isolateId, _portId); |
| 184 | 182 |
| 185 void receive(callback(var message)) { | 183 void receive(callback(var message)) { |
| 186 _callback = callback; | 184 _callback = callback; |
| 187 if (_listener == null) { | 185 if (_portSubscription == null) { |
| 188 _listener = (Event e) { | 186 _portSubscription = window.on[_listenerName].listen((Event e) { |
| 189 var data = json.parse(_getPortSyncEventData(e)); | 187 var data = json.parse(_getPortSyncEventData(e)); |
| 190 var replyTo = data[0]; | 188 var replyTo = data[0]; |
| 191 var message = _deserialize(data[1]); | 189 var message = _deserialize(data[1]); |
| 192 var result = _callback(message); | 190 var result = _callback(message); |
| 193 _dispatchEvent(replyTo, _serialize(result)); | 191 _dispatchEvent(replyTo, _serialize(result)); |
| 194 }; | 192 }); |
| 195 window.on[_listenerName].add(_listener); | |
| 196 } | 193 } |
| 197 } | 194 } |
| 198 | 195 |
| 199 void close() { | 196 void close() { |
| 200 _portMap.remove(_portId); | 197 _portMap.remove(_portId); |
| 201 if (_listener != null) window.on[_listenerName].remove(_listener); | 198 if (_portSubscription != null) _portSubscription.cancel(); |
| 202 } | 199 } |
| 203 | 200 |
| 204 SendPortSync toSendPort() { | 201 SendPortSync toSendPort() { |
| 205 return new _LocalSendPortSync._internal(this); | 202 return new _LocalSendPortSync._internal(this); |
| 206 } | 203 } |
| 207 | 204 |
| 208 static SendPortSync _lookup(int isolateId, int portId) { | 205 static SendPortSync _lookup(int isolateId, int portId) { |
| 209 if (isolateId == _isolateId) { | 206 if (isolateId == _isolateId) { |
| 210 return _portMap[portId].toSendPort(); | 207 return _portMap[portId].toSendPort(); |
| 211 } else { | 208 } else { |
| 212 return new _RemoteSendPortSync(isolateId, portId); | 209 return new _RemoteSendPortSync(isolateId, portId); |
| 213 } | 210 } |
| 214 } | 211 } |
| 215 } | 212 } |
| 216 | 213 |
| 217 get _isolateId => ReceivePortSync._isolateId; | 214 get _isolateId => ReceivePortSync._isolateId; |
| 218 | 215 |
| 219 void _dispatchEvent(String receiver, var message) { | 216 void _dispatchEvent(String receiver, var message) { |
| 220 var event = new CustomEvent(receiver, canBubble: false, cancelable:false, | 217 var event = new CustomEvent(receiver, canBubble: false, cancelable:false, |
| 221 detail: json.stringify(message)); | 218 detail: json.stringify(message)); |
| 222 window.dispatchEvent(event); | 219 window.dispatchEvent(event); |
| 223 } | 220 } |
| 224 | 221 |
| 225 String _getPortSyncEventData(CustomEvent event) => event.detail; | 222 String _getPortSyncEventData(CustomEvent event) => event.detail; |
| OLD | NEW |