public Future <Return> query(Params parameters) { return(Future <Return> .async((p, f) => { theFuture.onComplete(_ => doQuery(parameters, p)); theFuture = f; })); }
public static Future <Either <ErrorMsg, UnityWebRequest> > toFuture(this UnityWebRequest req) { Promise <Either <ErrorMsg, UnityWebRequest> > promise; var f = Future <Either <ErrorMsg, UnityWebRequest> > .async(out promise); StartCoroutine(webRequestEnumerator(req, promise)); return(f); }
/** * Delays completing of given future until the returned action is called. **/ public static Tpl <Future <A>, Action> delayUntilSignal <A>(this Future <A> future) { Promise <Unit> signalP; var f = future.zip(Future <Unit> .async(out signalP), (a, _) => a); Action act = () => signalP.tryComplete(F.unit); return(F.t(f, act)); }
public Future <B> flatMap <B>(Fn <A, Future <B> > mapper) { return(implementation.fold( mapper, _ => Future <B> .unfulfilled, f => Future <B> .async(p => f.onComplete(v => mapper(v).onComplete(p.complete))) )); }
public Future <B> map <B>(Fn <A, B> mapper) { return(implementation.fold( v => Future <B> .successful(mapper(v)), _ => Future <B> .unfulfilled, f => Future <B> .async(p => f.onComplete(v => p.complete(mapper(v)))) )); }
/** * Returns result from the first future that completes. **/ public static Future <A> firstOf <A>(this IEnumerable <Future <A> > enumerable) { return(Future <A> .async(p => { foreach (var f in enumerable) { f.onComplete(v => p.tryComplete(v)); } })); }
public void WithUnknownType() { Promise <int> promise; var f = Future <int> .async(out promise); var rx = f.toRxVal(); rx.value.shouldBeNone(); promise.complete(10); rx.value.shouldBeSome(10); }
public void NotCompletedThenSignalThenCompletion() { Promise <Unit> p; var t = Future <Unit> .async(out p).delayUntilSignal(); t._1.shouldNotBeCompleted(); t._2(); t._1.shouldNotBeCompleted(); p.complete(F.unit); t._1.shouldBeCompleted(F.unit); }
public void WhenASync() { Promise <int> p; var f = Future <int> .async(out p); var result = 0; f.onComplete(i => result = i); result.shouldEqual(0, "it should not run the function immediately"); p.complete(value); result.shouldEqual(value, "it run the function after completion"); }
public void SuccessfulToASync() { Promise <int> p2; var f2 = Future <int> .async(out p2); var f = successful.flatMap(_ => f2); f.type.shouldEqual(FutureType.ASync); f.value.shouldBeNone("it should be uncompleted if source future is incomplete"); p2.complete(2); f.value.shouldBeSome(2, "it should complete after completing the source future"); }
public void WhenASync() { Promise <int> p; var f = Future <int> .async(out p); var f2 = f.map(mapper); f2.type.shouldEqual(FutureType.ASync); f2.value.shouldBeNone("it should not have value before original future completion"); p.complete(1); f2.value.shouldBeSome(2, "it should have value after original future completion"); }
public void WhenASync() { Promise <int> p; var f = Future <int> .async(out p); var result = 0; f.nowAndOnComplete(iOpt => result += iOpt.fold(-1, _ => _)); result.shouldEqual(-1, "it should run the function immediately"); p.complete(2); result.shouldEqual(1, "it run the function after completion again"); }
/** * Filter & map future on value. If collector returns Some, completes the future, * otherwise - never completes. **/ public Future <B> collect <B>(Fn <A, Option <B> > collector) { return(implementation.fold( a => collector(a).fold(Future <B> .unfulfilled, Future <B> .successful), _ => Future <B> .unfulfilled, f => Future <B> .async(p => f.onComplete(a => { foreach (var b in collector(a)) { p.complete(b); } })) )); }
public void Test() { var dict = new Dictionary <int, Promise <string> >(); var queue = ASyncNAtATimeQueue.a( (int i) => Future <string> .async(p => dict[i] = p), 2 ); var f0 = queue.enqueue(0); var f1 = queue.enqueue(1); queue.running.shouldEqual(2u); queue.queued.shouldEqual(0u); var f2 = queue.enqueue(2); queue.running.shouldEqual(2u); queue.queued.shouldEqual(1u); dict.ContainsKey(3).shouldBeFalse(); var f3 = queue.enqueue(3); var f4 = queue.enqueue(4); queue.running.shouldEqual(2u); queue.queued.shouldEqual(3u); dict[1].complete("foo"); f1.value.shouldBeSome("foo"); queue.running.shouldEqual(2u); queue.queued.shouldEqual(2u); dict[2].complete("bar"); f2.value.shouldBeSome("bar"); queue.running.shouldEqual(2u); queue.queued.shouldEqual(1u); dict[0].complete("baz"); f0.value.shouldBeSome("baz"); queue.running.shouldEqual(2u); queue.queued.shouldEqual(0u); dict[3].complete("buz"); f3.value.shouldBeSome("buz"); queue.running.shouldEqual(1u); queue.queued.shouldEqual(0u); dict[4].complete("biz"); f4.value.shouldBeSome("biz"); queue.running.shouldEqual(0u); queue.queued.shouldEqual(0u); }
public void WithRxValInside() { Promise <IRxVal <int> > p; var f = Future <IRxVal <int> > .async(out p); var rx = f.toRxVal(0); rx.value.shouldEqual(0); var rx2 = RxRef.a(100); p.complete(rx2); rx.value.shouldEqual(100); rx2.value = 200; rx.value.shouldEqual(200); }
public void Equals() { Promise <int> asyncP; var asyncF = Future <int> .async(out asyncP); var unfullfilled = Future.unfulfilled <int>(); var completed = Future.successful(3); shouldNotEqualSymmetrical(unfullfilled, completed); shouldBeIdentical(unfullfilled, asyncF); shouldNotEqualSymmetrical(asyncF, completed); asyncP.complete(3); shouldNotEqualSymmetrical(unfullfilled, asyncF); shouldBeIdentical(asyncF, completed); }
public static Future <Unit> fromCoroutine(Coroutine coroutine) => Future <Unit> .async(p => { if (coroutine.finished) { p.complete(F.unit); } else { Action onComplete = null; onComplete = () => { p.complete(F.unit); coroutine.onFinish -= onComplete; }; coroutine.onFinish += onComplete; } });
static void whenASync(bool completeFirst) { Promise <int> p1, p2; var f1 = Future <int> .async(out p1); var f2 = Future <int> .async(out p2); var f = f1.zip(f2); f.type.shouldEqual(FutureType.ASync); f.value.shouldBeNone(); (completeFirst ? p1 : p2).complete(completeFirst ? 1 : 2); f.value.shouldBeNone("it should not complete just from one side"); (completeFirst ? p2 : p1).complete(completeFirst ? 2 : 1); f.value.shouldBeSome(F.t(1, 2), "it should complete from both sides"); }
public void ASyncToSuccessful() { Promise <int> p; var f = Future <int> .async(out p); var called = false; var f2 = f.flatMap(i => { called = true; return(Future.successful(i)); }); f2.type.shouldEqual(FutureType.ASync); f2.value.shouldBeNone(); called.shouldBeFalse("it should not call function until completion of a source promise"); p.complete(1); called.shouldBeTrue(); f2.value.shouldBeSome(1); }
public void ASyncToUnfulfilled() { Promise <int> p; var f = Future <int> .async(out p); var called = false; var f2 = f.flatMap(_ => { called = true; return(Future <int> .unfulfilled); }); f2.type.shouldEqual(FutureType.ASync); f2.value.shouldBeNone(); called.shouldBeFalse(); p.complete(1); called.shouldBeTrue(); f2.value.shouldBeNone("it shouldn't complete even if source future is completed"); }
public Future <C> zip <B, C>(Future <B> fb, Fn <A, B, C> mapper) { if (implementation.isB || fb.implementation.isB) { return(Future <C> .unfulfilled); } if (implementation.isA && fb.implementation.isA) { return(Future.successful(mapper(implementation.__unsafeGetA, fb.implementation.__unsafeGetA))); } var fa = this; return(Future <C> .async(p => { Act tryComplete = () => fa.value.zip(fb.value, mapper).each(ab => p.tryComplete(ab)); fa.onComplete(a => tryComplete()); fb.onComplete(b => tryComplete()); })); }
/* Do async cancellable WWW request. */ public static Cancellable <Future <Either <Cancelled, Either <WWWError, WWW> > > > toFuture(this WWW www) { Promise <Either <Cancelled, Either <WWWError, WWW> > > promise; var f = Future <Either <Cancelled, Either <WWWError, WWW> > > .async(out promise); var wwwCoroutine = StartCoroutine(WWWEnumerator(www, promise)); return(Cancellable.a(f, () => { if (www.isDone) { return false; } wwwCoroutine.stop(); www.Dispose(); promise.complete(new Either <Cancelled, Either <WWWError, WWW> >(Cancelled.instance)); return true; })); }
public void ASyncToASync() { Promise <int> p1; var f1 = Future <int> .async(out p1); Promise <int> p2; var f2 = Future <int> .async(out p2); var called = false; var f = f1.flatMap(_ => { called = true; return(f2); }); f.type.shouldEqual(FutureType.ASync); f.value.shouldBeNone("it should be not completed at start"); called.shouldBeFalse(); p1.complete(1); called.shouldBeTrue(); f.value.shouldBeNone("it should be not completed if source future completes"); p2.complete(2); f.value.shouldBeSome(2, "it should be completed"); }
/** * Returns result from the first future that satisfies the predicate as a Some. * If all futures do not satisfy the predicate returns None. **/ public static Future <Option <B> > firstOfWhere <A, B> (this IEnumerable <Future <A> > enumerable, Fn <A, Option <B> > predicate) { var futures = enumerable.ToList(); return(Future <Option <B> > .async(p => { var completed = 0; foreach (var f in futures) { f.onComplete(a => { completed++; var res = predicate(a); if (res.isSome) { p.tryComplete(res); } else if (completed == futures.Count) { p.tryComplete(F.none <B>()); } }); } })); }
public static Future <A> async <A>(out Promise <A> promise) => Future <A> .async(out promise);
public static Future <A> a <A>(Act <Promise <A> > action) => Future <A> .async(action);
public static Future <A> fromBusyLoop <A>( Fn <Option <A> > checker, YieldInstruction delay = null ) { return(Future <A> .async(p => ASync.StartCoroutine(busyLoopEnum(delay, p, checker)))); }
public Future <C> flatMap <B, C>(Fn <A, Future <B> > mapper, Fn <A, B, C> joiner) => implementation.fold( a => mapper(a).map(b => joiner(a, b)), _ => Future <C> .unfulfilled, f => Future <C> .async(p => f.onComplete(a => mapper(a).onComplete(b => p.complete(joiner(a, b))))) );
public static Future <A> StartCoroutine <A>( Func <Promise <A>, IEnumerator> coroutine ) => Future <A> .async(p => behaviour.StartCoroutine(coroutine(p)));
public void setup() { sourceFuture = Future <int> .async(out promise); tc = new TestTimeContext(); }