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 dart.isolate; | 5 part of dart.isolate; |
6 | 6 |
7 /** | 7 /** |
8 * The initial [IsolateStream] available by default for this isolate. This | 8 * The initial [IsolateStream] available by default for this isolate. This |
9 * [IsolateStream] is created automatically and it is commonly used to establish | 9 * [IsolateStream] is created automatically and it is commonly used to establish |
10 * the first communication between isolates (see [streamSpawnFunction] and | 10 * the first communication between isolates (see [streamSpawnFunction] and |
(...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
119 } | 119 } |
120 | 120 |
121 /** | 121 /** |
122 * [IsolateSink]s represent the feed for [IsolateStream]s. Any message written | 122 * [IsolateSink]s represent the feed for [IsolateStream]s. Any message written |
123 * to [this] is delivered to its respective [IsolateStream]. [IsolateSink]s are | 123 * to [this] is delivered to its respective [IsolateStream]. [IsolateSink]s are |
124 * created by [MessageBox]es. | 124 * created by [MessageBox]es. |
125 * | 125 * |
126 * [IsolateSink]s can be transmitted to other isolates. | 126 * [IsolateSink]s can be transmitted to other isolates. |
127 */ | 127 */ |
128 class IsolateSink extends StreamSink<dynamic> { | 128 class IsolateSink extends StreamSink<dynamic> { |
| 129 // TODO(8997): Implement EventSink instead. |
129 bool _isClosed = false; | 130 bool _isClosed = false; |
130 final SendPort _port; | 131 final SendPort _port; |
131 IsolateSink._fromPort(this._port); | 132 IsolateSink._fromPort(this._port); |
132 | 133 |
133 /** | 134 /** |
134 * Sends an asynchronous [message] to the linked [IsolateStream]. The message | 135 * Sends an asynchronous [message] to the linked [IsolateStream]. The message |
135 * is copied to the receiving isolate. | 136 * is copied to the receiving isolate. |
136 * | 137 * |
137 * The content of [message] can be: primitive values (null, num, bool, double, | 138 * The content of [message] can be: primitive values (null, num, bool, double, |
138 * String), instances of [IsolateSink]s, and lists and maps whose elements are | 139 * String), instances of [IsolateSink]s, and lists and maps whose elements are |
139 * any of these. List and maps are also allowed to be cyclic. | 140 * any of these. List and maps are also allowed to be cyclic. |
140 * | 141 * |
141 * In the special circumstances when two isolates share the same code and are | 142 * In the special circumstances when two isolates share the same code and are |
142 * running in the same process (e.g. isolates created via [spawnFunction]), it | 143 * running in the same process (e.g. isolates created via [spawnFunction]), it |
143 * is also possible to send object instances (which would be copied in the | 144 * is also possible to send object instances (which would be copied in the |
144 * process). This is currently only supported by the dartvm. For now, the | 145 * process). This is currently only supported by the dartvm. For now, the |
145 * dart2js compiler only supports the restricted messages described above. | 146 * dart2js compiler only supports the restricted messages described above. |
146 */ | 147 */ |
147 void add(dynamic message) { | 148 void add(dynamic message) { |
148 var mangled = _mangleMessage(message); | 149 var mangled = _mangleMessage(message); |
149 _port.send(mangled); | 150 _port.send(mangled); |
150 } | 151 } |
151 | 152 |
152 void signalError(AsyncError errorEvent) { | 153 void addError(AsyncError errorEvent) { |
153 throw new UnimplementedError("signalError on isolate streams"); | 154 throw new UnimplementedError("signalError on isolate streams"); |
154 } | 155 } |
155 | 156 |
156 dynamic _mangleMessage(var message) { | 157 dynamic _mangleMessage(var message) { |
157 _IsolateEncoder encoder = new _IsolateEncoder( | 158 _IsolateEncoder encoder = new _IsolateEncoder( |
158 _ISOLATE_STREAM_TOKEN, | 159 _ISOLATE_STREAM_TOKEN, |
159 (data) { | 160 (data) { |
160 if (data is IsolateSink) return ["Sink", data._port]; | 161 if (data is IsolateSink) return ["Sink", data._port]; |
161 if (identical(data, const _CloseToken())) return ["Close"]; | 162 if (identical(data, const _CloseToken())) return ["Close"]; |
162 return data; | 163 return data; |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
206 * Creates and spawns an isolate whose code is available at [uri]. Like with | 207 * Creates and spawns an isolate whose code is available at [uri]. Like with |
207 * [streamSpawnFunction], the child isolate will have a default [IsolateStream], | 208 * [streamSpawnFunction], the child isolate will have a default [IsolateStream], |
208 * and a this function returns an [IsolateSink] feeding into it. | 209 * and a this function returns an [IsolateSink] feeding into it. |
209 * | 210 * |
210 * See comments at the top of this library for more details. | 211 * See comments at the top of this library for more details. |
211 */ | 212 */ |
212 IsolateSink streamSpawnUri(String uri) { | 213 IsolateSink streamSpawnUri(String uri) { |
213 SendPort sendPort = spawnUri(uri); | 214 SendPort sendPort = spawnUri(uri); |
214 return new IsolateSink._fromPort(sendPort); | 215 return new IsolateSink._fromPort(sendPort); |
215 } | 216 } |
OLD | NEW |