Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(266)

Side by Side Diff: lib/html/src/Isolates.dart

Issue 10695147: Simplify ReceivePortSync.receive implementation. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 8 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 139 matching lines...) Expand 10 before | Expand all | Expand 10 after
150 _cachedIsolateId = _getNewIsolateId(); 150 _cachedIsolateId = _getNewIsolateId();
151 } 151 }
152 return _cachedIsolateId; 152 return _cachedIsolateId;
153 } 153 }
154 154
155 static String _getListenerName(isolateId, portId) => 155 static String _getListenerName(isolateId, portId) =>
156 'dart-port-$isolateId-$portId'; 156 'dart-port-$isolateId-$portId';
157 String get _listenerName() => _getListenerName(_isolateId, _portId); 157 String get _listenerName() => _getListenerName(_isolateId, _portId);
158 158
159 void receive(callback(var message)) { 159 void receive(callback(var message)) {
160 // Clear old listener. 160 _callback = callback;
161 if (_callback != null) { 161 if (_listener === null) {
162 window.on[_listenerName].remove(_listener); 162 _listener = (TextEvent e) {
163 var data = JSON.parse(e.data);
164 var replyTo = data[0];
165 var message = _deserialize(data[1]);
166 var result = _callback(message);
167 _dispatchEvent(replyTo, _serialize(result));
168 };
169 window.on[_listenerName].add(_listener);
163 } 170 }
164
165 _callback = callback;
166
167 // Install new listener.
168 var sendport = toSendPort();
169 _listener = (TextEvent e) {
170 var data = JSON.parse(e.data);
171 var replyTo = data[0];
172 var message = _deserialize(data[1]);
173 var result = sendport.callSync(message);
174 _dispatchEvent(replyTo, _serialize(result));
175 };
176 window.on[_listenerName].add(_listener);
177 } 171 }
178 172
179 void close() { 173 void close() {
180 _portMap.remove(_portId); 174 _portMap.remove(_portId);
181 window.on[_listenerName].remove(_listener); 175 if (_listener !== null) window.on[_listenerName].remove(_listener);
182 } 176 }
183 177
184 SendPortSync toSendPort() { 178 SendPortSync toSendPort() {
185 return new _LocalSendPortSync._internal(this); 179 return new _LocalSendPortSync._internal(this);
186 } 180 }
187 181
188 static SendPortSync _lookup(int isolateId, int portId) { 182 static SendPortSync _lookup(int isolateId, int portId) {
189 if (isolateId == _isolateId) { 183 if (isolateId == _isolateId) {
190 return _portMap[portId].toSendPort(); 184 return _portMap[portId].toSendPort();
191 } else { 185 } else {
192 return new _RemoteSendPortSync(isolateId, portId); 186 return new _RemoteSendPortSync(isolateId, portId);
193 } 187 }
194 } 188 }
195 } 189 }
196 190
197 void _dispatchEvent(String receiver, var message) { 191 void _dispatchEvent(String receiver, var message) {
198 var event = document.$dom_createEvent('TextEvent'); 192 var event = document.$dom_createEvent('TextEvent');
199 event.initTextEvent(receiver, false, false, window, JSON.stringify(message)); 193 event.initTextEvent(receiver, false, false, window, JSON.stringify(message));
200 window.$dom_dispatchEvent(event); 194 window.$dom_dispatchEvent(event);
201 } 195 }
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698