OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, 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 input_stream; | 5 library input_stream; |
6 | 6 |
7 import 'archive.dart' as archive; | 7 import 'archive.dart' as archive; |
8 import 'entry.dart'; | 8 import 'entry.dart'; |
9 import 'read_request.dart'; | 9 import 'read_request.dart'; |
10 import 'utils.dart'; | 10 import 'utils.dart'; |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
68 * [onEntry]. | 68 * [onEntry]. |
69 */ | 69 */ |
70 Future<List<CompleteArchiveEntry>> readAll() { | 70 Future<List<CompleteArchiveEntry>> readAll() { |
71 var completer = new Completer<List<Future<CompleteArchiveEntry>>>(); | 71 var completer = new Completer<List<Future<CompleteArchiveEntry>>>(); |
72 var result = <Future<CompleteArchiveEntry>>[]; | 72 var result = <Future<CompleteArchiveEntry>>[]; |
73 | 73 |
74 this.onEntry = (entry) => result.add(entry.readAll()); | 74 this.onEntry = (entry) => result.add(entry.readAll()); |
75 this.onError = (e, stack) => completer.completeException(e, stack); | 75 this.onError = (e, stack) => completer.completeException(e, stack); |
76 this.onClosed = () => completer.complete(result); | 76 this.onClosed = () => completer.complete(result); |
77 | 77 |
78 return completer.future.chain(Futures.wait); | 78 return completer.future.then(Futures.wait); |
79 } | 79 } |
80 | 80 |
81 /** | 81 /** |
82 * Sets a callback to call when a new entry is read from the archive. | 82 * Sets a callback to call when a new entry is read from the archive. |
83 * | 83 * |
84 * The [ArchiveEntry] that's read from an archive initially only contains | 84 * The [ArchiveEntry] that's read from an archive initially only contains |
85 * header information such as the filename and permissions. To get the actual | 85 * header information such as the filename and permissions. To get the actual |
86 * data contained in the entry, use [ArchiveEntry.openInputStream]. | 86 * data contained in the entry, use [ArchiveEntry.openInputStream]. |
87 * | 87 * |
88 * Since the entries are read in sequence from the archive, the data stream | 88 * Since the entries are read in sequence from the archive, the data stream |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
123 if (_currentEntry != null) _currentEntry.close(); | 123 if (_currentEntry != null) _currentEntry.close(); |
124 if (!_onEntryCompleter.future.isComplete) _onEntryCompleter.complete(null); | 124 if (!_onEntryCompleter.future.isComplete) _onEntryCompleter.complete(null); |
125 } | 125 } |
126 | 126 |
127 /** | 127 /** |
128 * Consumes and emits all [ArchiveEntries] in this archive. | 128 * Consumes and emits all [ArchiveEntries] in this archive. |
129 */ | 129 */ |
130 Future _consumeHeaders() { | 130 Future _consumeHeaders() { |
131 if (closed) return new Future.immediate(null); | 131 if (closed) return new Future.immediate(null); |
132 var data; | 132 var data; |
133 return call(NEXT_HEADER, _id.value).chain((_data) { | 133 return call(NEXT_HEADER, _id.value).then((_data) { |
134 data = _data; | 134 data = _data; |
135 if (data == null) return new Future.immediate(null); | 135 if (data == null) return new Future.immediate(null); |
136 return _emit(new archive.ArchiveEntry.internal(data, _id.value)). | 136 return _emit(new archive.ArchiveEntry.internal(data, _id.value)). |
137 chain((_) => _consumeHeaders()); | 137 then((_) => _consumeHeaders()); |
138 }); | 138 }); |
139 } | 139 } |
140 | 140 |
141 /** | 141 /** |
142 * Emits [entry] to the [onEntry] callback. Returns a [Future] that will | 142 * Emits [entry] to the [onEntry] callback. Returns a [Future] that will |
143 * complete once the callback's return value completes and the entry's data | 143 * complete once the callback's return value completes and the entry's data |
144 * has been fully consumed. | 144 * has been fully consumed. |
145 */ | 145 */ |
146 Future _emit(ArchiveEntry entry) { | 146 Future _emit(ArchiveEntry entry) { |
147 _currentEntry = entry; | 147 _currentEntry = entry; |
148 var future = _onEntryCompleter.future.chain((onEntry) { | 148 var future = _onEntryCompleter.future.then((onEntry) { |
149 if (closed) return new Future.immediate(null); | 149 if (closed) return new Future.immediate(null); |
150 var result = onEntry(entry); | 150 var result = onEntry(entry); |
151 if (result is Future) return result; | 151 if (result is Future) return result; |
152 return new Future.immediate(null); | 152 return new Future.immediate(null); |
153 }).chain((_) { | 153 }).then((_) { |
154 if (entry.isInputOpen) return entry.inputComplete; | 154 if (entry.isInputOpen) return entry.inputComplete; |
155 return new Future.immediate(null); | 155 return new Future.immediate(null); |
156 }); | 156 }); |
157 future.onComplete((_) { | 157 future.whenComplete(() { |
158 _currentEntry = null; | 158 _currentEntry = null; |
159 entry.close(); | 159 entry.close(); |
160 }); | 160 }); |
161 return future; | 161 return future; |
162 } | 162 } |
163 } | 163 } |
OLD | NEW |