Chromium Code Reviews| Index: pkg/barback/lib/src/internal_asset.dart |
| diff --git a/pkg/barback/lib/src/asset.dart b/pkg/barback/lib/src/internal_asset.dart |
| similarity index 56% |
| copy from pkg/barback/lib/src/asset.dart |
| copy to pkg/barback/lib/src/internal_asset.dart |
| index fda813380dc1965b6b901db7e043f531256eed41..396c9dee567d892ea2a87e41fdc82d61f6b6d8c9 100644 |
| --- a/pkg/barback/lib/src/asset.dart |
| +++ b/pkg/barback/lib/src/internal_asset.dart |
| @@ -2,65 +2,54 @@ |
| // for details. All rights reserved. Use of this source code is governed by a |
| // BSD-style license that can be found in the LICENSE file. |
| -library barback.asset; |
| +library barback.internal_asset; |
| import 'dart:async'; |
| import 'dart:convert'; |
| import 'dart:io'; |
| +import 'asset.dart'; |
| import 'asset_id.dart'; |
| import 'file_pool.dart'; |
| +import 'serialize.dart'; |
| import 'stream_replayer.dart'; |
| import 'utils.dart'; |
| -/// A blob of content. |
| +/// The internal base class of barback assets. |
| /// |
| -/// Assets may come from the file system, or as the output of a [Transformer]. |
| -/// They are identified by [AssetId]. |
| -abstract class Asset { |
| - /// The ID for this asset. |
| +/// This exposes serialization infrastructure so that pub can use it without |
| +/// making it part of the public barback API. |
| +abstract class InternalAsset implements Asset { |
| final AssetId id; |
| - Asset(this.id); |
| - |
| - factory Asset.fromBytes(AssetId id, List<int> bytes) => |
| - new _BinaryAsset(id, bytes); |
| - |
| - factory Asset.fromFile(AssetId id, File file) => |
| - new _FileAsset(id, file); |
| - |
| - factory Asset.fromString(AssetId id, String content) => |
| - new _StringAsset(id, content); |
| - |
| - factory Asset.fromPath(AssetId id, String path) => |
| - new _FileAsset(id, new File(path)); |
| - |
| - /// Creates an asset from a stream. |
| - /// |
| - /// This immediately starts draining [stream]. |
| - factory Asset.fromStream(AssetId id, Stream<List<int>> stream) => |
| - new _StreamAsset(id, stream); |
| - |
| - /// Returns the contents of the asset as a string. |
| - /// |
| - /// If the asset was created from a [String] the original string is always |
| - /// returned and [encoding] is ignored. Otherwise, the binary data of the |
| - /// asset is decoded using [encoding], which defaults to [UTF8]. |
| - Future<String> readAsString({Encoding encoding}); |
| + InternalAsset(this.id); |
| + |
| + /// Deserialize an asset that's been serialized using [serialize]. |
| + factory InternalAsset.deserialize(Map asset) { |
| + switch (asset['type']) { |
| + case 'stream': return new StreamAsset.deserialize(asset); |
| + case 'binary': return new BinaryAsset.deserialize(asset); |
| + case 'file': return new FileAsset.deserialize(asset); |
| + case 'string': return new StringAsset.deserialize(asset); |
| + default: |
| + throw new FormatException('Unknown asset type "${asset['type']}".'); |
| + } |
| + } |
|
Bob Nystrom
2013/11/06 21:29:08
How about just moving this to Asset and getting ri
nweiz
2013/11/07 00:44:48
I really didn't want to commit to a publicly-visib
Bob Nystrom
2013/11/07 18:04:56
We could put it in a separate library (lib/seriali
nweiz
2013/11/07 22:36:08
"lib/serialization.dart" would still be part of th
|
| - /// Streams the binary contents of the asset. |
| - /// |
| - /// If the asset was created from a [String], this returns its UTF-8 encoding. |
| - Stream<List<int>> read(); |
| + /// Serialize this asset to a map that can be sent between isolates. |
| + Map serialize(); |
| } |
| /// An asset whose data is stored in a list of bytes. |
| -class _BinaryAsset extends Asset { |
| +class BinaryAsset extends InternalAsset { |
| final List<int> _contents; |
| - _BinaryAsset(AssetId id, this._contents) |
| + BinaryAsset(AssetId id, this._contents) |
| : super(id); |
| + BinaryAsset.deserialize(Map asset) |
| + : this(deserializeId(asset['id']), asset['contents']); |
| + |
| Future<String> readAsString({Encoding encoding}) { |
| if (encoding == null) encoding = UTF8; |
| @@ -96,35 +85,58 @@ class _BinaryAsset extends Asset { |
| buffer.write("]"); |
| return buffer.toString(); |
| } |
| + |
| + Map serialize() { |
| + // TODO(nweiz): Send a typed array if that works after issue 14703 is fixed. |
| + return { |
| + 'type': 'binary', |
| + 'id': serializeId(id), |
| + 'contents': _contents.toList() |
|
Bob Nystrom
2013/11/06 21:29:08
Why copy the list? It's immutable (or, at least, i
nweiz
2013/11/07 00:44:48
The user could pass anything that implements List<
Bob Nystrom
2013/11/07 18:04:56
Ah, makes sense. Leave a comment to that effect.
nweiz
2013/11/07 22:36:08
Done.
|
| + }; |
| + } |
| } |
| /// An asset backed by a file on the local file system. |
| -class _FileAsset extends Asset { |
| +class FileAsset extends InternalAsset { |
| /// Use a [FilePool] to handle reads so we can try to cope with running out |
| /// of file descriptors more gracefully. |
| static final _pool = new FilePool(); |
| - final File _file; |
| - _FileAsset(AssetId id, this._file) |
| + final String _path; |
| + FileAsset(AssetId id, this._path) |
| : super(id); |
| + FileAsset.deserialize(Map asset) |
| + : this(deserializeId(asset['id']), asset['path']); |
| + |
| Future<String> readAsString({Encoding encoding}) { |
| if (encoding == null) encoding = UTF8; |
| - return _pool.readAsString(_file, encoding); |
| + return _pool.readAsString(_path, encoding); |
| } |
| - Stream<List<int>> read() => _pool.openRead(_file); |
| + Stream<List<int>> read() => _pool.openRead(_path); |
| + |
| + String toString() => 'File "${_path}"'; |
| - String toString() => 'File "${_file.path}"'; |
| + Map serialize() { |
| + return { |
| + 'type': 'file', |
| + 'id': serializeId(id), |
| + 'path': _path |
| + }; |
| + } |
| } |
| /// An asset whose data is stored in a string. |
| -class _StringAsset extends Asset { |
| +class StringAsset extends InternalAsset { |
| final String _contents; |
| - _StringAsset(AssetId id, this._contents) |
| + StringAsset(AssetId id, this._contents) |
| : super(id); |
| + StringAsset.deserialize(Map asset) |
| + : this(deserializeId(asset['id']), asset['contents']); |
| + |
| Future<String> readAsString({Encoding encoding}) => |
| new Future.value(_contents); |
| @@ -150,18 +162,29 @@ class _StringAsset extends Asset { |
| .replaceAll("\r", r"\r") |
| .replaceAll("\t", r"\t"); |
| } |
| + |
| + Map serialize() { |
| + return { |
| + 'type': 'string', |
| + 'id': serializeId(id), |
| + 'contents': _contents |
| + }; |
| + } |
| } |
| /// An asset whose data is available from a stream. |
| -class _StreamAsset extends Asset { |
| +class StreamAsset extends InternalAsset { |
| /// A stream replayer that records and replays the contents of the input |
| /// stream. |
| final StreamReplayer<List<int>> _replayer; |
| - _StreamAsset(AssetId id, Stream<List<int>> stream) |
| + StreamAsset(AssetId id, Stream<List<int>> stream) |
| : _replayer = new StreamReplayer(stream), |
| super(id); |
| + StreamAsset.deserialize(Map asset) |
| + : this(deserializeId(asset['id']), deserializeStream(asset['stream'])); |
| + |
| Future<String> readAsString({Encoding encoding}) { |
| if (encoding == null) encoding = UTF8; |
| return _replayer.getReplay().toList() |
| @@ -171,4 +194,12 @@ class _StreamAsset extends Asset { |
| Stream<List<int>> read() => _replayer.getReplay(); |
| String toString() => "Stream"; |
| + |
| + Map serialize() { |
| + return { |
| + 'type': 'stream', |
| + 'id': serializeId(id), |
| + 'stream': serializeStream(_replayer.getReplay()) |
| + }; |
| + } |
| } |