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 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
77 int _portId; | 77 int _portId; |
78 _RemoteSendPortSync(this._isolateId, this._portId); | 78 _RemoteSendPortSync(this._isolateId, this._portId); |
79 | 79 |
80 callSync(var message) { | 80 callSync(var message) { |
81 var serialized = _serialize(message); | 81 var serialized = _serialize(message); |
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 = (Event e) { | 92 var listener = (Event e) { |
93 result = JSON.parse(_getPortSyncEventData(e)); | 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); |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
142 _portIdCount = 0; | 142 _portIdCount = 0; |
143 _portMap = new Map<int, ReceivePortSync>(); | 143 _portMap = new Map<int, ReceivePortSync>(); |
144 } | 144 } |
145 _portId = _portIdCount++; | 145 _portId = _portIdCount++; |
146 _portMap[_portId] = this; | 146 _portMap[_portId] = this; |
147 } | 147 } |
148 | 148 |
149 static int get _isolateId { | 149 static int get _isolateId { |
150 // TODO(vsm): Make this coherent with existing isolate code. | 150 // TODO(vsm): Make this coherent with existing isolate code. |
151 if (_cachedIsolateId == null) { | 151 if (_cachedIsolateId == null) { |
152 _cachedIsolateId = _getNewIsolateId(); | 152 _cachedIsolateId = _getNewIsolateId(); |
153 } | 153 } |
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 = (Event e) { | 164 _listener = (Event e) { |
165 var data = JSON.parse(_getPortSyncEventData(e)); | 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); |
(...skipping 22 matching lines...) Expand all Loading... |
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 = new CustomEvent(receiver, false, false, JSON.stringify(message)); | 196 var event = new CustomEvent(receiver, false, false, JSON.stringify(message)); |
197 window.$dom_dispatchEvent(event); | 197 window.$dom_dispatchEvent(event); |
198 } | 198 } |
199 | 199 |
200 String _getPortSyncEventData(CustomEvent event) => event.detail; | 200 String _getPortSyncEventData(CustomEvent event) => event.detail; |
OLD | NEW |