OLD | NEW |
1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2011, 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 class _ReceivePortFactory { | 5 class _ReceivePortFactory { |
6 factory ReceivePort() { | 6 factory ReceivePort() { |
7 return new _ReceivePortImpl(); | 7 return new _ReceivePortImpl(); |
8 } | 8 } |
9 | |
10 factory ReceivePort.singleShot() { | |
11 return new _ReceivePortSingleShotImpl(); | |
12 } | |
13 } | 9 } |
14 | 10 |
15 | 11 |
16 class _ReceivePortImpl implements ReceivePort { | 12 class _ReceivePortImpl implements ReceivePort { |
17 /*--- public interface ---*/ | 13 /*--- public interface ---*/ |
18 factory _ReceivePortImpl() native "ReceivePortImpl_factory"; | 14 factory _ReceivePortImpl() native "ReceivePortImpl_factory"; |
19 | 15 |
20 receive(void onMessage(var message, SendPort replyTo)) { | 16 receive(void onMessage(var message, SendPort replyTo)) { |
21 _onMessage = onMessage; | 17 _onMessage = onMessage; |
22 } | 18 } |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
60 static _closeInternal(int id) native "ReceivePortImpl_closeInternal"; | 56 static _closeInternal(int id) native "ReceivePortImpl_closeInternal"; |
61 | 57 |
62 final int _id; | 58 final int _id; |
63 var _onMessage; | 59 var _onMessage; |
64 | 60 |
65 // id to ReceivePort mapping. | 61 // id to ReceivePort mapping. |
66 static Map _portMap; | 62 static Map _portMap; |
67 } | 63 } |
68 | 64 |
69 | 65 |
70 class _ReceivePortSingleShotImpl implements ReceivePort { | |
71 | |
72 _ReceivePortSingleShotImpl() : _port = new _ReceivePortImpl() { } | |
73 | |
74 void receive(void callback(var message, SendPort replyTo)) { | |
75 _port.receive((var message, SendPort replyTo) { | |
76 _port.close(); | |
77 callback(message, replyTo); | |
78 }); | |
79 } | |
80 | |
81 void close() { | |
82 _port.close(); | |
83 } | |
84 | |
85 SendPort toSendPort() { | |
86 return _port.toSendPort(); | |
87 } | |
88 | |
89 final _ReceivePortImpl _port; | |
90 | |
91 } | |
92 | |
93 | |
94 class _SendPortImpl implements SendPort { | 66 class _SendPortImpl implements SendPort { |
95 /*--- public interface ---*/ | 67 /*--- public interface ---*/ |
96 void send(var message, [SendPort replyTo = null]) { | 68 void send(var message, [SendPort replyTo = null]) { |
97 this._sendNow(message, replyTo); | 69 this._sendNow(message, replyTo); |
98 } | 70 } |
99 | 71 |
100 void _sendNow(var message, SendPort replyTo) { | 72 void _sendNow(var message, SendPort replyTo) { |
101 int replyId = (replyTo === null) ? 0 : replyTo._id; | 73 int replyId = (replyTo === null) ? 0 : replyTo._id; |
102 _sendInternal(_id, replyId, message); | 74 _sendInternal(_id, replyId, message); |
103 } | 75 } |
104 | 76 |
105 _ReceivePortSingleShotImpl call(var message) { | 77 Future call(var message) { |
106 final result = new _ReceivePortSingleShotImpl(); | 78 final completer = new Completer(); |
107 this.send(message, result.toSendPort()); | 79 final port = new _ReceivePortImpl(); |
108 return result; | 80 send(message, port.toSendPort()); |
109 } | 81 port.receive((value, ignoreReplyTo) { |
110 | 82 port.close(); |
111 _ReceivePortSingleShotImpl _callNow(var message) { | 83 if (value is Exception) { |
112 final result = new _ReceivePortSingleShotImpl(); | 84 completer.completeException(value); |
113 this._sendNow(message, result.toSendPort()); | 85 } else { |
114 return result; | 86 completer.complete(value); |
| 87 } |
| 88 }); |
| 89 return completer.future; |
115 } | 90 } |
116 | 91 |
117 bool operator==(var other) { | 92 bool operator==(var other) { |
118 return (other is _SendPortImpl) && _id == other._id; | 93 return (other is _SendPortImpl) && _id == other._id; |
119 } | 94 } |
120 | 95 |
121 int hashCode() { | 96 int hashCode() { |
122 return _id; | 97 return _id; |
123 } | 98 } |
124 | 99 |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
159 | 134 |
160 SendPort _spawnFunction(void topLevelFunction()) { | 135 SendPort _spawnFunction(void topLevelFunction()) { |
161 throw new NotImplementedException(); | 136 throw new NotImplementedException(); |
162 } | 137 } |
163 | 138 |
164 SendPort _spawnUri(String uri) { | 139 SendPort _spawnUri(String uri) { |
165 throw new NotImplementedException(); | 140 throw new NotImplementedException(); |
166 } | 141 } |
167 | 142 |
168 ReceivePort _port = null; | 143 ReceivePort _port = null; |
OLD | NEW |