Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, 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 library barback.serialize; | 5 library barback.serialize; |
| 6 | 6 |
| 7 import 'dart:async'; | 7 import 'dart:async'; |
| 8 import 'dart:isolate'; | 8 import 'dart:isolate'; |
| 9 | 9 |
| 10 import 'package:stack_trace/stack_trace.dart'; | 10 import 'package:stack_trace/stack_trace.dart'; |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 26 sendPort.send({ | 26 sendPort.send({ |
| 27 'type': 'error', | 27 'type': 'error', |
| 28 'error': CrossIsolateException.serialize(error, stackTrace) | 28 'error': CrossIsolateException.serialize(error, stackTrace) |
| 29 }); | 29 }); |
| 30 }); | 30 }); |
| 31 }); | 31 }); |
| 32 | 32 |
| 33 return receivePort.sendPort; | 33 return receivePort.sendPort; |
| 34 } | 34 } |
| 35 | 35 |
| 36 /// Converts [callback] into a [SendPort] which which another isolate can call | |
|
Bob Nystrom
2014/01/30 19:33:44
I think you had something similar in the previous
nweiz
2014/01/31 03:43:27
I don't like how this description exposes the impl
Bob Nystrom
2014/01/31 18:28:53
I'm OK with that too, but in that case, I wouldn't
| |
| 37 /// [callback]. | |
| 38 /// | |
| 39 /// [callback] may return either a serializable value or a [Future] of a | |
| 40 /// serializable value. | |
| 41 SendPort serializeCallback(callback()) { | |
| 42 var receivePort = new ReceivePort(); | |
| 43 receivePort.first.then( | |
| 44 (sendPort) => _sendFuture(syncFuture(callback), sendPort)); | |
| 45 return receivePort.sendPort; | |
| 46 } | |
| 47 | |
| 48 /// Converts [future] into a [SendPort] which which another isolate can listen | |
| 49 /// to the value of [Future]. | |
|
Bob Nystrom
2014/01/30 19:33:44
Ditto above comment.
nweiz
2014/01/31 03:43:27
N/A
| |
| 50 /// | |
| 51 /// [Future] must produce a serializable value. | |
| 52 SendPort serializeFuture(Future future) { | |
| 53 // Ensure the future doesn't top-level an error that arrives before | |
| 54 // [receivePort] fires. | |
| 55 future.catchError(null); | |
|
Bob Nystrom
2014/01/30 19:33:44
I don't understand how passing a null catch handle
nweiz
2014/01/31 03:43:27
"null" means approximately the same thing as "() {
Bob Nystrom
2014/01/31 18:28:53
Oh, wow. That's totally news to me. They certainly
| |
| 56 | |
| 57 var receivePort = new ReceivePort(); | |
| 58 receivePort.first.then((sendPort) => _sendFuture(future, sendPort)); | |
| 59 return receivePort.sendPort; | |
| 60 } | |
| 61 | |
| 62 /// A helper method for sending the result of [future] across [sendPort]. | |
|
Bob Nystrom
2014/01/30 19:33:44
"helper method" doesn't add much. How about "Sends
nweiz
2014/01/31 03:43:27
N/A
| |
| 63 void _sendFuture(Future future, SendPort sendPort) { | |
| 64 future.then((value) { | |
| 65 sendPort.send({'type': 'success', 'value': value}); | |
| 66 }, onError: (error, stackTrace) { | |
| 67 sendPort.send({ | |
| 68 'type': 'error', | |
| 69 'error': CrossIsolateException.serialize(error, stackTrace) | |
| 70 }); | |
| 71 }); | |
| 72 } | |
| 73 | |
| 36 /// Converts a serializable map into an [AssetId]. | 74 /// Converts a serializable map into an [AssetId]. |
| 37 AssetId deserializeId(Map id) => new AssetId(id['package'], id['path']); | 75 AssetId deserializeId(Map id) => new AssetId(id['package'], id['path']); |
| 38 | 76 |
| 39 /// Convert a [SendPort] whose opposite is waiting to send us a stream into a | 77 /// Convert a [SendPort] whose opposite is waiting to send us a stream into a |
| 40 /// [Stream]. | 78 /// [Stream]. |
| 41 /// | 79 /// |
| 42 /// No stream data will actually be sent across the isolate boundary until | 80 /// No stream data will actually be sent across the isolate boundary until |
| 43 /// someone subscribes to the returned stream. | 81 /// someone subscribes to the returned stream. |
| 44 Stream deserializeStream(SendPort sendPort) { | 82 Stream deserializeStream(SendPort sendPort) { |
| 45 return callbackStream(() { | 83 return callbackStream(() { |
| 46 var receivePort = new ReceivePort(); | 84 var receivePort = new ReceivePort(); |
| 47 sendPort.send(receivePort.sendPort); | 85 sendPort.send(receivePort.sendPort); |
| 48 // TODO(nweiz): use a const constructor for StreamTransformer when issue | 86 // TODO(nweiz): use a const constructor for StreamTransformer when issue |
| 49 // 14971 is fixed. | 87 // 14971 is fixed. |
| 50 return receivePort.transform( | 88 return receivePort.transform( |
| 51 new StreamTransformer(_deserializeTransformer)); | 89 new StreamTransformer(_deserializeTransformer)); |
| 52 }); | 90 }); |
| 53 } | 91 } |
| 54 | 92 |
| 93 /// A function that takes no arguments and returns a future. | |
| 94 typedef Future AsyncFunction(); | |
| 95 | |
| 96 /// Converts a [SendPort] that was created using [serializeCallback] to a | |
| 97 /// callback that returns a [Future]. | |
| 98 /// | |
| 99 /// If [deserialize] is passed, the return value of the callback is passed | |
| 100 /// through it before being returned. | |
| 101 AsyncFunction deserializeCallback(SendPort sendPort, [deserialize(value)]) { | |
| 102 return () { | |
|
Bob Nystrom
2014/01/30 19:33:44
What's the outer function wrapper for?
| |
| 103 return deserializeFuture(sendPort).then((value) { | |
| 104 return deserialize == null ? value : deserialize(value); | |
| 105 }); | |
| 106 }; | |
| 107 } | |
| 108 | |
| 109 /// Converts a [SendPort] that was created using [serializeFuture] to a | |
| 110 /// [Future]. | |
| 111 Future deserializeFuture(SendPort sendPort) { | |
| 112 var receivePort = new ReceivePort(); | |
| 113 sendPort.send(receivePort.sendPort); | |
| 114 return receivePort.first.then((response) { | |
| 115 if (response['type'] == 'success') return response['value']; | |
| 116 var exception = new CrossIsolateException.deserialize(response['error']); | |
| 117 return new Future.error(exception, exception.stackTrace); | |
| 118 }); | |
| 119 } | |
| 120 | |
| 55 /// The body of a [StreamTransformer] that deserializes the values in a stream | 121 /// The body of a [StreamTransformer] that deserializes the values in a stream |
| 56 /// sent by [serializeStream]. | 122 /// sent by [serializeStream]. |
| 57 StreamSubscription _deserializeTransformer(Stream input, bool cancelOnError) { | 123 StreamSubscription _deserializeTransformer(Stream input, bool cancelOnError) { |
| 58 var subscription; | 124 var subscription; |
| 59 var transformed = input.transform(new StreamTransformer.fromHandlers( | 125 var transformed = input.transform(new StreamTransformer.fromHandlers( |
| 60 handleData: (data, sink) { | 126 handleData: (data, sink) { |
| 61 if (data['type'] == 'data') { | 127 if (data['type'] == 'data') { |
| 62 sink.add(data['data']); | 128 sink.add(data['data']); |
| 63 } else if (data['type'] == 'error') { | 129 } else if (data['type'] == 'error') { |
| 64 var exception = new CrossIsolateException.deserialize(data['error']); | 130 var exception = new CrossIsolateException.deserialize(data['error']); |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 117 /// A regular expression to match the exception prefix that some exceptions' | 183 /// A regular expression to match the exception prefix that some exceptions' |
| 118 /// [Object.toString] values contain. | 184 /// [Object.toString] values contain. |
| 119 final _exceptionPrefix = new RegExp(r'^([A-Z][a-zA-Z]*)?(Exception|Error): '); | 185 final _exceptionPrefix = new RegExp(r'^([A-Z][a-zA-Z]*)?(Exception|Error): '); |
| 120 | 186 |
| 121 /// Get a string description of an exception. | 187 /// Get a string description of an exception. |
| 122 /// | 188 /// |
| 123 /// Many exceptions include the exception class name at the beginning of their | 189 /// Many exceptions include the exception class name at the beginning of their |
| 124 /// [toString], so we remove that if it exists. | 190 /// [toString], so we remove that if it exists. |
| 125 String _getErrorMessage(error) => | 191 String _getErrorMessage(error) => |
| 126 error.toString().replaceFirst(_exceptionPrefix, ''); | 192 error.toString().replaceFirst(_exceptionPrefix, ''); |
| OLD | NEW |