Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(118)

Side by Side Diff: pkg/barback/lib/src/serialize.dart

Issue 808713003: Remove barback from the repo. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 6 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
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
3 // BSD-style license that can be found in the LICENSE file.
4
5 library barback.serialize;
6
7 import 'dart:async';
8 import 'dart:isolate';
9
10 import 'package:stack_trace/stack_trace.dart';
11
12 import 'asset/asset_id.dart';
13 import 'utils.dart';
14
15 /// Converts [id] into a serializable map.
16 Map serializeId(AssetId id) => {'package': id.package, 'path': id.path};
17
18 /// Converts [stream] into a [SendPort] with which another isolate can request
19 /// the data from [stream].
20 SendPort serializeStream(Stream stream) {
21 var receivePort = new ReceivePort();
22 receivePort.first.then((sendPort) {
23 stream.listen((data) => sendPort.send({'type': 'data', 'data': data}),
24 onDone: () => sendPort.send({'type': 'done'}),
25 onError: (error, stackTrace) {
26 sendPort.send({
27 'type': 'error',
28 'error': CrossIsolateException.serialize(error, stackTrace)
29 });
30 });
31 });
32
33 return receivePort.sendPort;
34 }
35
36 /// Converts a serializable map into an [AssetId].
37 AssetId deserializeId(Map id) => new AssetId(id['package'], id['path']);
38
39 /// Convert a [SendPort] whose opposite is waiting to send us a stream into a
40 /// [Stream].
41 ///
42 /// No stream data will actually be sent across the isolate boundary until
43 /// someone subscribes to the returned stream.
44 Stream deserializeStream(SendPort sendPort) {
45 return callbackStream(() {
46 var receivePort = new ReceivePort();
47 sendPort.send(receivePort.sendPort);
48 return receivePort.transform(
49 const StreamTransformer(_deserializeTransformer));
50 });
51 }
52
53 /// The body of a [StreamTransformer] that deserializes the values in a stream
54 /// sent by [serializeStream].
55 StreamSubscription _deserializeTransformer(Stream input, bool cancelOnError) {
56 var subscription;
57 var transformed = input.transform(new StreamTransformer.fromHandlers(
58 handleData: (data, sink) {
59 if (data['type'] == 'data') {
60 sink.add(data['data']);
61 } else if (data['type'] == 'error') {
62 var exception = new CrossIsolateException.deserialize(data['error']);
63 sink.addError(exception, exception.stackTrace);
64 } else {
65 assert(data['type'] == 'done');
66 sink.close();
67 subscription.cancel();
68 }
69 }));
70 subscription = transformed.listen(null, cancelOnError: cancelOnError);
71 return subscription;
72 }
73
74 /// An exception that was originally raised in another isolate.
75 ///
76 /// Exception objects can't cross isolate boundaries in general, so this class
77 /// wraps as much information as can be consistently serialized.
78 class CrossIsolateException implements Exception {
79 /// The name of the type of exception thrown.
80 ///
81 /// This is the return value of [error.runtimeType.toString()]. Keep in mind
82 /// that objects in different libraries may have the same type name.
83 final String type;
84
85 /// The exception's message, or its [toString] if it didn't expose a `message`
86 /// property.
87 final String message;
88
89 /// The exception's stack chain, or `null` if no stack chain was available.
90 final Chain stackTrace;
91
92 /// Loads a [CrossIsolateException] from a serialized representation.
93 ///
94 /// [error] should be the result of [CrossIsolateException.serialize].
95 CrossIsolateException.deserialize(Map error)
96 : type = error['type'],
97 message = error['message'],
98 stackTrace = error['stack'] == null ? null :
99 new Chain.parse(error['stack']);
100
101 /// Serializes [error] to an object that can safely be passed across isolate
102 /// boundaries.
103 static Map serialize(error, [StackTrace stack]) {
104 if (stack == null && error is Error) stack = error.stackTrace;
105 return {
106 'type': error.runtimeType.toString(),
107 'message': getErrorMessage(error),
108 'stack': stack == null ? null : new Chain.forTrace(stack).toString()
109 };
110 }
111
112 String toString() => "$message\n$stackTrace";
113 }
OLDNEW
« no previous file with comments | « pkg/barback/lib/src/package_provider.dart ('k') | pkg/barback/lib/src/transformer/aggregate_transform.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698