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 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
88 return _deserialize(result); | 88 return _deserialize(result); |
89 } | 89 } |
90 | 90 |
91 static _call(int isolateId, int portId, var message) { | 91 static _call(int isolateId, int portId, var message) { |
92 var target = 'dart-port-$isolateId-$portId'; | 92 var target = 'dart-port-$isolateId-$portId'; |
93 // TODO(vsm): Make this re-entrant. | 93 // TODO(vsm): Make this re-entrant. |
94 // TODO(vsm): Set this up set once, on the first call. | 94 // TODO(vsm): Set this up set once, on the first call. |
95 var source = '$target-result'; | 95 var source = '$target-result'; |
96 var result = null; | 96 var result = null; |
97 var listener = (Event e) { | 97 var listener = (Event e) { |
98 result = JSON.parse(_getPortSyncEventData(e)); | 98 result = json.parse(_getPortSyncEventData(e)); |
99 }; | 99 }; |
100 window.on[source].add(listener); | 100 window.on[source].add(listener); |
101 _dispatchEvent(target, [source, message]); | 101 _dispatchEvent(target, [source, message]); |
102 window.on[source].remove(listener); | 102 window.on[source].remove(listener); |
103 return result; | 103 return result; |
104 } | 104 } |
105 } | 105 } |
106 | 106 |
107 // The receiver is in the same Dart isolate, compiled to JS. | 107 // The receiver is in the same Dart isolate, compiled to JS. |
108 class _LocalSendPortSync implements SendPortSync { | 108 class _LocalSendPortSync implements SendPortSync { |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
160 } | 160 } |
161 | 161 |
162 static String _getListenerName(isolateId, portId) => | 162 static String _getListenerName(isolateId, portId) => |
163 'dart-port-$isolateId-$portId'; | 163 'dart-port-$isolateId-$portId'; |
164 String get _listenerName => _getListenerName(_isolateId, _portId); | 164 String get _listenerName => _getListenerName(_isolateId, _portId); |
165 | 165 |
166 void receive(callback(var message)) { | 166 void receive(callback(var message)) { |
167 _callback = callback; | 167 _callback = callback; |
168 if (_listener == null) { | 168 if (_listener == null) { |
169 _listener = (Event e) { | 169 _listener = (Event e) { |
170 var data = JSON.parse(_getPortSyncEventData(e)); | 170 var data = json.parse(_getPortSyncEventData(e)); |
171 var replyTo = data[0]; | 171 var replyTo = data[0]; |
172 var message = _deserialize(data[1]); | 172 var message = _deserialize(data[1]); |
173 var result = _callback(message); | 173 var result = _callback(message); |
174 _dispatchEvent(replyTo, _serialize(result)); | 174 _dispatchEvent(replyTo, _serialize(result)); |
175 }; | 175 }; |
176 window.on[_listenerName].add(_listener); | 176 window.on[_listenerName].add(_listener); |
177 } | 177 } |
178 } | 178 } |
179 | 179 |
180 void close() { | 180 void close() { |
(...skipping 10 matching lines...) Expand all Loading... |
191 return _portMap[portId].toSendPort(); | 191 return _portMap[portId].toSendPort(); |
192 } else { | 192 } else { |
193 return new _RemoteSendPortSync(isolateId, portId); | 193 return new _RemoteSendPortSync(isolateId, portId); |
194 } | 194 } |
195 } | 195 } |
196 } | 196 } |
197 | 197 |
198 get _isolateId => ReceivePortSync._isolateId; | 198 get _isolateId => ReceivePortSync._isolateId; |
199 | 199 |
200 void _dispatchEvent(String receiver, var message) { | 200 void _dispatchEvent(String receiver, var message) { |
201 var event = new CustomEvent(receiver, false, false, JSON.stringify(message)); | 201 var event = new CustomEvent(receiver, false, false, json.stringify(message)); |
202 window.$dom_dispatchEvent(event); | 202 window.$dom_dispatchEvent(event); |
203 } | 203 } |
204 | 204 |
205 String _getPortSyncEventData(CustomEvent event) => event.detail; | 205 String _getPortSyncEventData(CustomEvent event) => event.detail; |
OLD | NEW |