Ejemplo n.º 1
0
        static void _chainForeignFuture(Future source, _Future target)
        {
            D.assert(!target._isComplete);
            D.assert(!(source is _Future));

            // Mark the target as chained (and as such half-completed).
            target._setPendingComplete();
            try {
                source.then((value) => {
                    D.assert(target._isPendingComplete);
                    // The "value" may be another future if the foreign future
                    // implementation is mis-behaving,
                    // so use _complete instead of _completeWithValue.
                    target._clearPendingComplete();     // Clear this first, it's set again.
                    target._complete(FutureOr.value(value));
                    return(new FutureOr());
                },
                            onError: (Exception error) => {
                    D.assert(target._isPendingComplete);
                    target._completeError(error);
                    return(new FutureOr());
                });
            }
            catch (Exception e) {
                // This only happens if the `then` call threw synchronously when given
                // valid arguments.
                // That requires a non-conforming implementation of the Future interface,
                // which should, hopefully, never happen.
                async_.scheduleMicrotask(() => {
                    target._completeError(e);
                    return(null);
                });
            }
        }
Ejemplo n.º 2
0
        public static Stream <T> fromFuture(Future <T> future)
        {
            // Use the controller's buffering to fill in the value even before
            // the stream has a listener. For a single value, it's not worth it
            // to wait for a listener before doing the `then` on the future.
            _StreamController <T> controller =
                new _SyncStreamController <T>(null, null, null, null);

            future.then((value) => {
                controller._add((T)value);
                controller._closeUnchecked();
            }, onError: (error) => {
                controller._addError(error, null);
                controller._closeUnchecked();
                return(FutureOr.nil);
            });
            return(controller.stream);
        }