/** 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)); })); }
/* 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()); }
/* 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()); }
public static Future <Option <To> > mapO <From, To>( this Future <Option <From> > future, Fn <From, To> mapper ) { return(future.map(opt => opt.map(mapper))); }
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))); }
public static Future <Option <Exception> > ofFailure <A>(this Future <Try <A> > future) => future.map(e => e.exception);
public static Future <Option <A> > ofFailure <A, B>(this Future <Either <A, B> > future) => future.map(e => e.leftValue);
public static Future <Option <A> > ofSuccess <A>(this Future <Try <A> > future) => future.map(e => e.value);
public static Future <Option <B> > ofSuccess <A, B>(this Future <Either <A, B> > future) => future.map(e => e.rightValue);
public static Future <B> Select <A, B>(this Future <A> fa, Fn <A, B> f) => fa.map(f);