Beispiel #1
0
        /** Measures how much time has passed from call to timed to future completion. **/
        public static Future <Tpl <A, Duration> > timed <A>(this Future <A> future)
        {
            var startTime = Time.realtimeSinceStartup;

            return(future.map(a => {
                var time = Time.realtimeSinceStartup - startTime;
                return F.t(a, Duration.fromSeconds(time));
            }));
        }
Beispiel #2
0
        /* Waits at most `timeout` for the future to complete. Completes with
         * exception produced by `onTimeout` on timeout. */
        public static Future <Either <B, A> > timeout <A, B>(
            this Future <A> future, Duration timeout, Fn <B> onTimeout, ITimeContext tc = null
            )
        {
            var timeoutF = delay(timeout, () => future.value.fold(
                                     // onTimeout() might have side effects, so we only need to execute it if
                                     // there is no value in the original future once the timeout hits.
                                     () => onTimeout().left().r <A>(),
                                     v => v.right().l <B>()
                                     ), tc);

            return(new[] { future.map(v => v.right().l <B>()), timeoutF }.firstOf());
        }
Beispiel #3
0
        /* Waits at most `timeoutSeconds` for the future to complete. Completes with
         * exception produced by `onTimeout` on timeout. */
        public static Future <Either <B, A> > timeout <A, B>(
            this Future <A> future, float timeoutSeconds, Fn <B> onTimeout
            )
        {
            // TODO: test me - how? Unity test runner doesn't support delays.
            var timeoutF = delay(timeoutSeconds, () => future.value.fold(
                                     // onTimeout() might have side effects, so we only need to execute it if
                                     // there is no value in the original future once the timeout hits.
                                     () => onTimeout().left().r <A>(),
                                     v => v.right().l <B>()
                                     ));

            return(new[] { future.map(v => v.right().l <B>()), timeoutF }.firstOf());
        }
Beispiel #4
0
 public static Future <Option <To> > mapO <From, To>(
     this Future <Option <From> > future, Fn <From, To> mapper
     )
 {
     return(future.map(opt => opt.map(mapper)));
 }
Beispiel #5
0
 public static Future <Either <Err, To> > mapE <From, To, Err>(
     this Future <Either <Err, From> > future, Fn <From, To> mapper
     )
 {
     return(future.map(e => e.mapRight(mapper)));
 }
Beispiel #6
0
 public static Future <Option <Exception> > ofFailure <A>(this Future <Try <A> > future) =>
 future.map(e => e.exception);
Beispiel #7
0
 public static Future <Option <A> > ofFailure <A, B>(this Future <Either <A, B> > future) =>
 future.map(e => e.leftValue);
Beispiel #8
0
 public static Future <Option <A> > ofSuccess <A>(this Future <Try <A> > future) =>
 future.map(e => e.value);
Beispiel #9
0
 public static Future <Option <B> > ofSuccess <A, B>(this Future <Either <A, B> > future) =>
 future.map(e => e.rightValue);
Beispiel #10
0
 public static Future <B> Select <A, B>(this Future <A> fa, Fn <A, B> f) => fa.map(f);