| 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. |
| 9 * [IsolateStream] is created automatically and it is commonly used to establish | 9 * |
| 10 * the first communication between isolates (see [streamSpawnFunction] and | 10 * This IsolateStream is created automatically and is commonly used |
| 11 * [streamSpawnUri]). | 11 * to establish the first communication between isolates. |
| 12 * (See [streamSpawnFunction].) |
| 12 */ | 13 */ |
| 13 final IsolateStream stream = new IsolateStream._fromOriginalReceivePort(port); | 14 final IsolateStream stream = new IsolateStream._fromOriginalReceivePort(port); |
| 14 | 15 |
| 15 /** | 16 /** |
| 16 * A [MessageBox] creates an [IsolateStream], [stream], and an [IsolateSink], | 17 * The creator of the [IsolateStream] and [IsolateSink] |
| 17 * [sink]. | 18 * that allow an isolate to exchange messages with other isolates. |
| 18 * | 19 * |
| 19 * Any message that is written into the [sink] (independent of the isolate) is | 20 * Any message that is written into the [sink] (independent of the isolate) is |
| 20 * sent to the [stream] where its subscribers can react to the messages. | 21 * sent to the [stream] where its subscribers can react to the messages. |
| 21 */ | 22 */ |
| 22 class MessageBox { | 23 class MessageBox { |
| 23 final IsolateStream stream; | 24 final IsolateStream stream; |
| 24 final IsolateSink sink; | 25 final IsolateSink sink; |
| 25 | 26 |
| 26 external MessageBox.oneShot(); | 27 external MessageBox.oneShot(); |
| 27 external MessageBox(); | 28 external MessageBox(); |
| 28 } | 29 } |
| 29 | 30 |
| 30 external bool _isCloseToken(var object); | 31 external bool _isCloseToken(var object); |
| 31 | 32 |
| 32 /** | 33 /** |
| 33 * [IsolateStream]s, together with [IsolateSink]s, are the only means of | 34 * Together with [IsolateSink], the only means of |
| 34 * communication between isolates. Each IsolateStream has a corresponding | 35 * communication between isolates. |
| 36 * |
| 37 * Each IsolateStream has a corresponding |
| 35 * [IsolateSink]. Any message written into that sink will be delivered to | 38 * [IsolateSink]. Any message written into that sink will be delivered to |
| 36 * the stream and then dispatched to the stream's subscribers. | 39 * the stream and then dispatched to the stream's subscribers. |
| 37 */ | 40 */ |
| 38 class IsolateStream extends Stream<dynamic> { | 41 class IsolateStream extends Stream<dynamic> { |
| 39 bool _isClosed = false; | 42 bool _isClosed = false; |
| 40 final ReceivePort _port; | 43 final ReceivePort _port; |
| 41 StreamController _controller = new StreamController(sync: true); | 44 StreamController _controller = new StreamController(sync: true); |
| 42 | 45 |
| 43 IsolateStream._fromOriginalReceivePort(this._port) { | 46 IsolateStream._fromOriginalReceivePort(this._port) { |
| 44 _port.receive((message, replyTo) { | 47 _port.receive((message, replyTo) { |
| (...skipping 12 matching lines...) Expand all Loading... |
| 57 | 60 |
| 58 void _add(var message) { | 61 void _add(var message) { |
| 59 if (_isCloseToken(message)) { | 62 if (_isCloseToken(message)) { |
| 60 close(); | 63 close(); |
| 61 } else { | 64 } else { |
| 62 _controller.sink.add(message); | 65 _controller.sink.add(message); |
| 63 } | 66 } |
| 64 } | 67 } |
| 65 | 68 |
| 66 /** | 69 /** |
| 67 * Close the stream from the receiving end. | 70 * Closes the stream from the receiving end. |
| 68 * | 71 * |
| 69 * Closing an already closed port has no effect. | 72 * Closing an already closed port has no effect. |
| 70 */ | 73 */ |
| 71 void close() { | 74 void close() { |
| 72 if (!_isClosed) { | 75 if (!_isClosed) { |
| 73 _isClosed = true; | 76 _isClosed = true; |
| 74 _port.close(); | 77 _port.close(); |
| 75 _controller.close(); | 78 _controller.close(); |
| 76 } | 79 } |
| 77 } | 80 } |
| 78 | 81 |
| 79 StreamSubscription listen(void onData(event), | 82 StreamSubscription listen(void onData(event), |
| 80 { void onError(error), | 83 { void onError(error), |
| 81 void onDone(), | 84 void onDone(), |
| 82 bool cancelOnError}) { | 85 bool cancelOnError}) { |
| 83 return _controller.stream.listen(onData, | 86 return _controller.stream.listen(onData, |
| 84 onError: onError, | 87 onError: onError, |
| 85 onDone: onDone, | 88 onDone: onDone, |
| 86 cancelOnError: cancelOnError); | 89 cancelOnError: cancelOnError); |
| 87 } | 90 } |
| 88 } | 91 } |
| 89 | 92 |
| 90 /** | 93 /** |
| 91 * [IsolateSink]s represent the feed for [IsolateStream]s. Any message written | 94 * The feed for an [IsolateStream]. |
| 92 * to [this] is delivered to its respective [IsolateStream]. [IsolateSink]s are | 95 * |
| 93 * created by [MessageBox]es. | 96 * Any message written to [this] is delivered |
| 97 * to its respective [IsolateStream]. |
| 98 * [IsolateSink]s are created by [MessageBox]es. |
| 94 * | 99 * |
| 95 * [IsolateSink]s can be transmitted to other isolates. | 100 * [IsolateSink]s can be transmitted to other isolates. |
| 96 */ | 101 */ |
| 97 abstract class IsolateSink extends EventSink<dynamic> { | 102 abstract class IsolateSink extends EventSink<dynamic> { |
| 98 // TODO(floitsch): Actually it should be a StreamSink (being able to flow- | 103 // TODO(floitsch): Actually it should be a StreamSink (being able to flow- |
| 99 // control). | 104 // control). |
| 100 | 105 |
| 101 /** | 106 /** |
| 102 * Sends an asynchronous [message] to the linked [IsolateStream]. The message | 107 * Sends an asynchronous [message] to the linked [IsolateStream]; |
| 103 * is copied to the receiving isolate. | 108 * the message is copied to the receiving isolate. |
| 104 * | 109 * |
| 105 * The content of [message] can be: primitive values (null, num, bool, double, | 110 * The content of [message] can be: primitive values (null, num, bool, double, |
| 106 * String), instances of [IsolateSink]s, and lists and maps whose elements are | 111 * String), instances of [IsolateSink]s, and lists and maps whose elements are |
| 107 * any of these. List and maps are also allowed to be cyclic. | 112 * any of these. List and maps are also allowed to be cyclic. |
| 108 * | 113 * |
| 109 * In the special circumstances when two isolates share the same code and are | 114 * In the special circumstances when two isolates share the same code and are |
| 110 * running in the same process (e.g. isolates created via [spawnFunction]), it | 115 * running in the same process (e.g. isolates created via [spawnFunction]), it |
| 111 * is also possible to send object instances (which would be copied in the | 116 * is also possible to send object instances (which would be copied in the |
| 112 * process). This is currently only supported by the dartvm. For now, the | 117 * process). This is currently only supported by the dartvm. For now, the |
| 113 * dart2js compiler only supports the restricted messages described above. | 118 * dart2js compiler only supports the restricted messages described above. |
| 114 */ | 119 */ |
| 115 void add(dynamic message); | 120 void add(dynamic message); |
| 116 | 121 |
| 117 void addError(errorEvent); | 122 void addError(errorEvent); |
| 118 | 123 |
| 119 /** Closing multiple times is allowed. */ | 124 /** Closing multiple times is allowed. */ |
| 120 void close(); | 125 void close(); |
| 121 | 126 |
| 122 /** | 127 /** |
| 123 * Tests whether [other] is an [IsolateSink] feeding into the same | 128 * Tests whether [other] is an [IsolateSink] feeding into the same |
| 124 * [IsolateStream] as this one. | 129 * [IsolateStream] as this one. |
| 125 */ | 130 */ |
| 126 bool operator==(var other); | 131 bool operator==(var other); |
| 127 } | 132 } |
| 128 | 133 |
| 129 | 134 |
| 130 /** | 135 /** |
| 131 * Creates and spawns an isolate that shares the same code as the current | 136 * Creates and spawns an isolate that shares the same code as the current |
| 132 * isolate, but that starts from [topLevelFunction]. The [topLevelFunction] | 137 * isolate, but that starts from the specified function. |
| 133 * argument must be a static top-level function or a static method that takes no | 138 * |
| 134 * arguments. | 139 * The [topLevelFunction] argument must be |
| 140 * a static top-level function or a static method that takes no arguments. |
| 135 * | 141 * |
| 136 * When any isolate starts (even the main script of the application), a default | 142 * When any isolate starts (even the main script of the application), a default |
| 137 * [IsolateStream] is created for it. This sink is available from the top-level | 143 * [IsolateStream] is created for it. This sink is available from the top-level |
| 138 * getter [stream] defined in this library. | 144 * getter [stream] defined in this library. |
| 139 * | 145 * |
| 140 * [spawnFunction] returns an [IsolateSink] feeding into the child isolate's | 146 * [spawnFunction] returns an [IsolateSink] feeding into the child isolate's |
| 141 * default stream. | 147 * default stream. |
| 142 * | 148 * |
| 143 * The optional [unhandledExceptionCallback] argument is invoked whenever an | 149 * The optional [unhandledExceptionCallback] argument is invoked whenever an |
| 144 * exception inside the isolate is unhandled. It can be seen as a big | 150 * exception inside the isolate is unhandled. It can be seen as a big |
| 145 * `try/catch` around everything that is executed inside the isolate. The | 151 * `try/catch` around everything that is executed inside the isolate. The |
| 146 * callback should return `true` when it was able to handled the exception. | 152 * callback should return `true` if it was able to handle the exception. |
| 147 */ | 153 */ |
| 148 external IsolateSink streamSpawnFunction( | 154 external IsolateSink streamSpawnFunction( |
| 149 void topLevelFunction(), | 155 void topLevelFunction(), |
| 150 [bool unhandledExceptionCallback(IsolateUnhandledException e)]); | 156 [bool unhandledExceptionCallback(IsolateUnhandledException e)]); |
| OLD | NEW |