Exemple #1
0
        public override Future timeout(TimeSpan timeLimit, Func <FutureOr> onTimeout = null)
        {
            if (_isComplete)
            {
                return(immediate(this));
            }

            _Future result = new _Future();
            Timer   timer;

            if (onTimeout == null)
            {
                timer = Timer.create(timeLimit, () => {
                    result._completeError(
                        new TimeoutException("Future not completed", timeLimit));
                    return(null);
                });
            }
            else
            {
                Zone zone = Zone.current;
                onTimeout = async_._registerHandler(onTimeout, zone);

                timer = Timer.create(timeLimit, () => {
                    try {
                        result._complete((FutureOr)zone.run(() => onTimeout()));
                    }
                    catch (Exception e) {
                        result._completeError(e);
                    }

                    return(null);
                });
            }

            then(v => {
                if (timer.isActive)
                {
                    timer.cancel();
                    result._completeWithValue(v);
                }

                return(FutureOr.nil);
            }, onError: e => {
                if (timer.isActive)
                {
                    timer.cancel();
                    result._completeError(e);
                }

                return(FutureOr.nil);
            });
            return(result);
        }
Exemple #2
0
        public static Future delayed(TimeSpan duration, Func <FutureOr> computation = null)
        {
            _Future result = new _Future();

            Timer.create(duration, () => {
                if (computation == null)
                {
                    result._complete();
                }
                else
                {
                    try {
                        result._complete(computation());
                    }
                    catch (Exception e) {
                        async_._completeWithErrorCallback(result, e);
                    }
                }

                return(null);
            });
            return(result);
        }
Exemple #3
0
        public static Stream <T> periodic(TimeSpan period,
                                          Func <int, T> computation = null)
        {
            Timer timer                     = default;
            int   computationCount          = 0;
            StreamController <T> controller = null;
            // Counts the time that the Stream was running (and not paused).
            Stopwatch watch = new Stopwatch();

            Action sendEvent = () => {
                watch.reset();
                T data = default;
                if (computation != null)
                {
                    try {
                        data = computation(computationCount++);
                    }
                    catch (Exception e) {
                        controller.addError(e, e.StackTrace);
                        return;
                    }
                }

                controller.add(data);
            };

            Action startPeriodicTimer = () => {
                D.assert(timer == null);
                timer = Timer.periodic(period, (object timer1) => {
                    sendEvent();
                    return(null);
                });
            };

            // the original code new an abstract class
            controller = StreamController <T> .create(
                sync : true,
                onListen : () => {
                watch.start();
                startPeriodicTimer();
            },
                onPause : () => {
                timer.cancel();
                timer = null;
                watch.stop();
            },
                onResume : () => {
                D.assert(timer == null);
                TimeSpan elapsed = watch.elapsed;
                watch.start();
                timer = Timer.create(period - elapsed, () => {
                    timer = null;
                    startPeriodicTimer();
                    sendEvent();
                });
            },
                onCancel : () => {
                if (timer != null)
                {
                    timer.cancel();
                }
                timer = null;
                return(Future._nullFuture);
            });

            return(controller.stream);
        }