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'; |
11 | 11 |
12 import 'asset/asset_id.dart'; | 12 import 'asset/asset_id.dart'; |
13 import 'utils.dart'; | 13 import 'utils.dart'; |
14 | 14 |
15 /// Converts [id] into a serializable map. | 15 /// Converts [id] into a serializable map. |
16 Map serializeId(AssetId id) => {'package': id.package, 'path': id.path}; | 16 Map serializeId(AssetId id) => {'package': id.package, 'path': id.path}; |
17 | 17 |
18 /// Converts [stream] into a [SendPort] with which another isolate can request | 18 /// Converts [stream] into a [SendPort] with which another isolate can request |
19 /// the data from [stream]. | 19 /// the data from [stream]. |
20 SendPort serializeStream(Stream stream) { | 20 SendPort serializeStream(Stream stream) { |
21 var receivePort = new ReceivePort(); | 21 var receivePort = new ReceivePort(); |
22 receivePort.first.then((sendPort) { | 22 receivePort.first.then((sendPort) { |
23 stream.listen((data) => sendPort.send({'type': 'data', 'data': data}), | 23 stream.listen((data) => sendPort.send({'type': 'data', 'data': data}), |
24 onDone: () => sendPort.send({'type': 'done'}), | 24 onDone: () => sendPort.send({'type': 'done'}), |
25 onError: (error, stackTrace) { | 25 onError: (error, stackTrace) { |
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 a serializable map into an [AssetId]. | 36 /// Converts a serializable map into an [AssetId]. |
37 AssetId deserializeId(Map id) => new AssetId(id['package'], id['path']); | 37 AssetId deserializeId(Map id) => new AssetId(id['package'], id['path']); |
38 | 38 |
39 /// Convert a [SendPort] whose opposite is waiting to send us a stream into a | 39 /// Convert a [SendPort] whose opposite is waiting to send us a stream into a |
40 /// [Stream]. | 40 /// [Stream]. |
41 /// | 41 /// |
42 /// No stream data will actually be sent across the isolate boundary until | 42 /// No stream data will actually be sent across the isolate boundary until |
43 /// someone subscribes to the returned stream. | 43 /// someone subscribes to the returned stream. |
44 Stream deserializeStream(SendPort sendPort) { | 44 Stream deserializeStream(SendPort sendPort) { |
45 return callbackStream(() { | 45 return callbackStream(() { |
46 var receivePort = new ReceivePort(); | 46 var receivePort = new ReceivePort(); |
47 sendPort.send(receivePort.sendPort); | 47 sendPort.send(receivePort.sendPort); |
48 return receivePort.transform( | 48 return receivePort |
49 const StreamTransformer(_deserializeTransformer)); | 49 .transform(const StreamTransformer(_deserializeTransformer)); |
50 }); | 50 }); |
51 } | 51 } |
52 | 52 |
53 /// The body of a [StreamTransformer] that deserializes the values in a stream | 53 /// The body of a [StreamTransformer] that deserializes the values in a stream |
54 /// sent by [serializeStream]. | 54 /// sent by [serializeStream]. |
55 StreamSubscription _deserializeTransformer(Stream input, bool cancelOnError) { | 55 StreamSubscription _deserializeTransformer(Stream input, bool cancelOnError) { |
56 var subscription; | 56 var subscription; |
57 var transformed = input.transform(new StreamTransformer.fromHandlers( | 57 var transformed = input |
58 handleData: (data, sink) { | 58 .transform(new StreamTransformer.fromHandlers(handleData: (data, sink) { |
59 if (data['type'] == 'data') { | 59 if (data['type'] == 'data') { |
60 sink.add(data['data']); | 60 sink.add(data['data']); |
61 } else if (data['type'] == 'error') { | 61 } else if (data['type'] == 'error') { |
62 var exception = new CrossIsolateException.deserialize(data['error']); | 62 var exception = new CrossIsolateException.deserialize(data['error']); |
63 sink.addError(exception, exception.stackTrace); | 63 sink.addError(exception, exception.stackTrace); |
64 } else { | 64 } else { |
65 assert(data['type'] == 'done'); | 65 assert(data['type'] == 'done'); |
66 sink.close(); | 66 sink.close(); |
67 subscription.cancel(); | 67 subscription.cancel(); |
68 } | 68 } |
(...skipping 19 matching lines...) Expand all Loading... |
88 | 88 |
89 /// The exception's stack chain, or `null` if no stack chain was available. | 89 /// The exception's stack chain, or `null` if no stack chain was available. |
90 final Chain stackTrace; | 90 final Chain stackTrace; |
91 | 91 |
92 /// Loads a [CrossIsolateException] from a serialized representation. | 92 /// Loads a [CrossIsolateException] from a serialized representation. |
93 /// | 93 /// |
94 /// [error] should be the result of [CrossIsolateException.serialize]. | 94 /// [error] should be the result of [CrossIsolateException.serialize]. |
95 CrossIsolateException.deserialize(Map error) | 95 CrossIsolateException.deserialize(Map error) |
96 : type = error['type'], | 96 : type = error['type'], |
97 message = error['message'], | 97 message = error['message'], |
98 stackTrace = error['stack'] == null ? null : | 98 stackTrace = |
99 new Chain.parse(error['stack']); | 99 error['stack'] == null ? null : new Chain.parse(error['stack']); |
100 | 100 |
101 /// Serializes [error] to an object that can safely be passed across isolate | 101 /// Serializes [error] to an object that can safely be passed across isolate |
102 /// boundaries. | 102 /// boundaries. |
103 static Map serialize(error, [StackTrace stack]) { | 103 static Map serialize(error, [StackTrace stack]) { |
104 if (stack == null && error is Error) stack = error.stackTrace; | 104 if (stack == null && error is Error) stack = error.stackTrace; |
105 return { | 105 return { |
106 'type': error.runtimeType.toString(), | 106 'type': error.runtimeType.toString(), |
107 'message': getErrorMessage(error), | 107 'message': getErrorMessage(error), |
108 'stack': stack == null ? null : new Chain.forTrace(stack).toString() | 108 'stack': stack == null ? null : new Chain.forTrace(stack).toString() |
109 }; | 109 }; |
110 } | 110 } |
111 | 111 |
112 String toString() => "$message\n$stackTrace"; | 112 String toString() => "$message\n$stackTrace"; |
113 } | 113 } |
OLD | NEW |