Chromium Code Reviews| Index: pkg/barback/lib/src/serialize.dart |
| diff --git a/pkg/barback/lib/src/serialize.dart b/pkg/barback/lib/src/serialize.dart |
| index 7114039e44d5cc417baf8dfc62a97400619447c3..4d3f973f2a313b097c9f2806d8fda22efff6e992 100644 |
| --- a/pkg/barback/lib/src/serialize.dart |
| +++ b/pkg/barback/lib/src/serialize.dart |
| @@ -33,6 +33,44 @@ SendPort serializeStream(Stream stream) { |
| return receivePort.sendPort; |
| } |
| +/// 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
|
| +/// [callback]. |
| +/// |
| +/// [callback] may return either a serializable value or a [Future] of a |
| +/// serializable value. |
| +SendPort serializeCallback(callback()) { |
| + var receivePort = new ReceivePort(); |
| + receivePort.first.then( |
| + (sendPort) => _sendFuture(syncFuture(callback), sendPort)); |
| + return receivePort.sendPort; |
| +} |
| + |
| +/// Converts [future] into a [SendPort] which which another isolate can listen |
| +/// 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
|
| +/// |
| +/// [Future] must produce a serializable value. |
| +SendPort serializeFuture(Future future) { |
| + // Ensure the future doesn't top-level an error that arrives before |
| + // [receivePort] fires. |
| + 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
|
| + |
| + var receivePort = new ReceivePort(); |
| + receivePort.first.then((sendPort) => _sendFuture(future, sendPort)); |
| + return receivePort.sendPort; |
| +} |
| + |
| +/// 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
|
| +void _sendFuture(Future future, SendPort sendPort) { |
| + future.then((value) { |
| + sendPort.send({'type': 'success', 'value': value}); |
| + }, onError: (error, stackTrace) { |
| + sendPort.send({ |
| + 'type': 'error', |
| + 'error': CrossIsolateException.serialize(error, stackTrace) |
| + }); |
| + }); |
| +} |
| + |
| /// Converts a serializable map into an [AssetId]. |
| AssetId deserializeId(Map id) => new AssetId(id['package'], id['path']); |
| @@ -52,6 +90,34 @@ Stream deserializeStream(SendPort sendPort) { |
| }); |
| } |
| +/// A function that takes no arguments and returns a future. |
| +typedef Future AsyncFunction(); |
| + |
| +/// Converts a [SendPort] that was created using [serializeCallback] to a |
| +/// callback that returns a [Future]. |
| +/// |
| +/// If [deserialize] is passed, the return value of the callback is passed |
| +/// through it before being returned. |
| +AsyncFunction deserializeCallback(SendPort sendPort, [deserialize(value)]) { |
| + return () { |
|
Bob Nystrom
2014/01/30 19:33:44
What's the outer function wrapper for?
|
| + return deserializeFuture(sendPort).then((value) { |
| + return deserialize == null ? value : deserialize(value); |
| + }); |
| + }; |
| +} |
| + |
| +/// Converts a [SendPort] that was created using [serializeFuture] to a |
| +/// [Future]. |
| +Future deserializeFuture(SendPort sendPort) { |
| + var receivePort = new ReceivePort(); |
| + sendPort.send(receivePort.sendPort); |
| + return receivePort.first.then((response) { |
| + if (response['type'] == 'success') return response['value']; |
| + var exception = new CrossIsolateException.deserialize(response['error']); |
| + return new Future.error(exception, exception.stackTrace); |
| + }); |
| +} |
| + |
| /// The body of a [StreamTransformer] that deserializes the values in a stream |
| /// sent by [serializeStream]. |
| StreamSubscription _deserializeTransformer(Stream input, bool cancelOnError) { |