-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add typed wrapper functions to delegate classes.
These mirror the methods in the the collection package, and serve a similar purpose of safely casting generic objects when the user is confident that the actual object's values are more specific than the static type. R=floitsch@google.com, lrn@google.com Review URL: https://codereview.chromium.org//1870543004 .
- Loading branch information
Showing
20 changed files
with
1,223 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file | ||
// 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. | ||
|
||
import 'dart:async'; | ||
|
||
import '../typed/stream.dart'; | ||
|
||
/// Simple delegating wrapper around a [Stream]. | ||
/// | ||
/// Subclasses can override individual methods, or use this to expose only the | ||
/// [Stream] methods of a subclass. | ||
/// | ||
/// Note that this is identical to [StreamView] in `dart:async`. It's provided | ||
/// under this name for consistency with other `Delegating*` classes. | ||
class DelegatingStream<T> extends StreamView<T> { | ||
DelegatingStream(Stream<T> stream) : super(stream); | ||
|
||
/// Creates a wrapper which throws if [stream]'s events aren't instances of | ||
/// `T`. | ||
/// | ||
/// This soundly converts a [Stream] to a `Stream<T>`, regardless of its | ||
/// original generic type, by asserting that its events are instances of `T` | ||
/// whenever they're provided. If they're not, the stream throws a | ||
/// [CastError]. | ||
static Stream/*<T>*/ typed/*<T>*/(Stream stream) => | ||
stream is Stream/*<T>*/ ? stream : new TypeSafeStream/*<T>*/(stream); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file | ||
// 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. | ||
|
||
import 'dart:async'; | ||
|
||
class TypeSafeFuture<T> implements Future<T> { | ||
final Future _future; | ||
|
||
TypeSafeFuture(this._future); | ||
|
||
Stream<T> asStream() => _future.then((value) => value as T).asStream(); | ||
|
||
Future catchError(Function onError, {bool test(Object error)}) => | ||
_future.catchError(onError, test: test); | ||
|
||
Future/*<S>*/ then/*<S>*/(/*=S*/ onValue(T value), {Function onError}) => | ||
_future.then((value) => onValue(value as T), onError: onError); | ||
|
||
Future<T> whenComplete(action()) => | ||
new TypeSafeFuture<T>(_future.whenComplete(action)); | ||
|
||
Future<T> timeout(Duration timeLimit, {onTimeout()}) => | ||
new TypeSafeFuture<T>(_future.timeout(timeLimit, onTimeout: onTimeout)); | ||
} |
Oops, something went wrong.