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 _serialize(var message) { | 5 _serialize(var message) { |
6 return new _JsSerializer().traverse(message); | 6 return new _JsSerializer().traverse(message); |
7 } | 7 } |
8 | 8 |
9 class _JsSerializer extends _Serializer { | 9 class _JsSerializer extends _Serializer { |
10 | 10 |
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
82 var result = _call(_isolateId, _portId, serialized); | 82 var result = _call(_isolateId, _portId, serialized); |
83 return _deserialize(result); | 83 return _deserialize(result); |
84 } | 84 } |
85 | 85 |
86 static _call(int isolateId, int portId, var message) { | 86 static _call(int isolateId, int portId, var message) { |
87 var target = 'dart-port-$isolateId-$portId'; | 87 var target = 'dart-port-$isolateId-$portId'; |
88 // TODO(vsm): Make this re-entrant. | 88 // TODO(vsm): Make this re-entrant. |
89 // TODO(vsm): Set this up set once, on the first call. | 89 // TODO(vsm): Set this up set once, on the first call. |
90 var source = '$target-result'; | 90 var source = '$target-result'; |
91 var result = null; | 91 var result = null; |
92 var listener = (TextEvent e) { | 92 var listener = (Event e) { |
93 result = JSON.parse(e.data); | 93 result = JSON.parse(_getPortSyncEventData(e)); |
94 }; | 94 }; |
95 window.on[source].add(listener); | 95 window.on[source].add(listener); |
96 _dispatchEvent(target, [source, message]); | 96 _dispatchEvent(target, [source, message]); |
97 window.on[source].remove(listener); | 97 window.on[source].remove(listener); |
98 return result; | 98 return result; |
99 } | 99 } |
100 } | 100 } |
101 | 101 |
102 // The receiver is in the same Dart isolate, compiled to JS. | 102 // The receiver is in the same Dart isolate, compiled to JS. |
103 class _LocalSendPortSync implements SendPortSync { | 103 class _LocalSendPortSync implements SendPortSync { |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
154 return _cachedIsolateId; | 154 return _cachedIsolateId; |
155 } | 155 } |
156 | 156 |
157 static String _getListenerName(isolateId, portId) => | 157 static String _getListenerName(isolateId, portId) => |
158 'dart-port-$isolateId-$portId'; | 158 'dart-port-$isolateId-$portId'; |
159 String get _listenerName => _getListenerName(_isolateId, _portId); | 159 String get _listenerName => _getListenerName(_isolateId, _portId); |
160 | 160 |
161 void receive(callback(var message)) { | 161 void receive(callback(var message)) { |
162 _callback = callback; | 162 _callback = callback; |
163 if (_listener === null) { | 163 if (_listener === null) { |
164 _listener = (TextEvent e) { | 164 _listener = (Event e) { |
165 var data = JSON.parse(e.data); | 165 var data = JSON.parse(_getPortSyncEventData(e)); |
166 var replyTo = data[0]; | 166 var replyTo = data[0]; |
167 var message = _deserialize(data[1]); | 167 var message = _deserialize(data[1]); |
168 var result = _callback(message); | 168 var result = _callback(message); |
169 _dispatchEvent(replyTo, _serialize(result)); | 169 _dispatchEvent(replyTo, _serialize(result)); |
170 }; | 170 }; |
171 window.on[_listenerName].add(_listener); | 171 window.on[_listenerName].add(_listener); |
172 } | 172 } |
173 } | 173 } |
174 | 174 |
175 void close() { | 175 void close() { |
(...skipping 10 matching lines...) Expand all Loading... |
186 return _portMap[portId].toSendPort(); | 186 return _portMap[portId].toSendPort(); |
187 } else { | 187 } else { |
188 return new _RemoteSendPortSync(isolateId, portId); | 188 return new _RemoteSendPortSync(isolateId, portId); |
189 } | 189 } |
190 } | 190 } |
191 } | 191 } |
192 | 192 |
193 get _isolateId => ReceivePortSync._isolateId; | 193 get _isolateId => ReceivePortSync._isolateId; |
194 | 194 |
195 void _dispatchEvent(String receiver, var message) { | 195 void _dispatchEvent(String receiver, var message) { |
196 var event = document.$dom_createEvent('TextEvent'); | 196 var event = document.$dom_createEvent('CustomEvent'); |
197 event.initTextEvent(receiver, false, false, window, JSON.stringify(message)); | 197 event.initCustomEvent(receiver, false, false, JSON.stringify(message)); |
198 window.$dom_dispatchEvent(event); | 198 window.$dom_dispatchEvent(event); |
199 } | 199 } |
| 200 |
| 201 String _getPortSyncEventData(CustomEvent event) => event.detail; |
OLD | NEW |