/** Returns a new ref that is bound to this ref and vice versa. **/ public static IRxRef <B> comap <A, B>(this IRxRef <A> rx, Fn <A, B> mapper, Fn <B, A> comapper) { var bRef = RxRef.a(mapper(rx.value)); bRef.subscribe(b => rx.value = comapper(b)); return(bRef); }
public IRxRef <B> comap <B>(Fn <A, B> mapper, Fn <B, A> comapper) { var bRef = RxRef.a(mapper(value)); bRef.subscribe(b => value = comapper(b)); return(bRef); }
public static ObserverBuilder <Elem, IRxRef <Elem> > builder <Elem>(Elem value) { return(builder => { var rxRef = new RxRef <Elem>(value); builder(rxRef); return rxRef; }); }
public static IRxVal <A> get(A value) { return(staticCache.get(value).getOrElse(() => { var cached = (IRxVal <A>)RxRef.a(value); staticCache.Add(value, cached); return cached; })); }
public static IRxVal <A> extractFuture <A>( this Future <IRxVal <A> > future, A whileNotCompleted ) { var rx = RxRef.a(whileNotCompleted); future.onComplete(rx2 => rx2.subscribe(v => rx.value = v)); return(rx); }
public static ObserverBuilder <Elem, IRxVal <Elem> > builder <Elem>(Elem value) { // Unity Mono doesn't quite understand that it is the same type :| // return RxRef.builder(value); return(builder => { var rxRef = new RxRef <Elem>(value); builder(rxRef); return (IRxVal <Elem>)rxRef; }); }
public void WhenFromSingleItem() { var rx = RxRef.a(3.some()); var dst = new[] { rx }.anyDefined(); dst.value.shouldBeSome(3); rx.value = Option <int> .None; dst.value.shouldBeNone(); rx.value = 4.some(); dst.value.shouldBeSome(4); }
public void WhenFromSingleItemSearchForFalse() { var rx = RxRef.a(false); var dst = new[] { rx }.anyOf(searchFor: false); dst.value.shouldBeTrue(); rx.value = true; dst.value.shouldBeFalse(); rx.value = false; dst.value.shouldBeTrue(); }
public void WhenFromSingleItem() { var rx = RxRef.a(3); var rx2 = new[] { rx }.anyThat(i => i % 2 != 0); rx2.value.shouldBeSome(3); rx.value = 2; rx2.value.shouldBeNone(); rx.value = 5; rx2.value.shouldBeSome(5); }
public void WhenMultipleItemsSearchForFalse() { var rx1 = RxRef.a(false); var rx2 = RxRef.a(true); var dst = new[] { rx1, rx2 }.anyOf(searchFor: false); dst.value.shouldBeTrue(); rx1.value = true; dst.value.shouldBeFalse(); rx2.value = false; dst.value.shouldBeTrue(); rx1.value = false; dst.value.shouldBeTrue(); }
public virtual IRxVal <Option <A> > rxElement(int index) { return(elementRxRefs.get(index).fold( () => { var rxRef = RxRef.a(this.get(index)); // The cast is mono bug. // ArrayTypeMismatchException: Source array type cannot be assigned to destination array type. // (wrapper stelemref) object:stelemref (object,intptr,object) elementRxRefs[index] = rxRef; return rxRef; }, _ => _ )); }
public void ctor() => describe(() => { var mapperInvocations = 0; var actionInvocations = 0; var lastActionResult = 0; IRxRef <Tpl <int, int> > src = null; IRxVal <int> rx = null; beforeEach += () => { mapperInvocations = 0; actionInvocations = 0; src = RxRef.a(F.t(10, 0)); rx = new RxVal <int>( -11, setValue => src.subscribeWithoutEmit(tracker, t => { mapperInvocations++; setValue(t._1 + t._2 + 1); }) ); rx.subscribe(tracker, i => { actionInvocations++; lastActionResult = i; }); }; on["creation"] = () => { it["should create a subscription to source"] = () => src.subscribers.shouldEqual(1); it["should not invoke mapper"] = () => mapperInvocations.shouldEqual(0); it["should have specified value"] = () => rx.value.shouldEqual(-11); it["should invoke action"] = () => actionInvocations.shouldEqual(1); it["should invoke action with current value"] = () => lastActionResult.shouldEqual(-11); when["source changes"] = () => { beforeEach += () => src.value = F.t(2, 3); it["should invoke mapper"] = () => mapperInvocations.shouldEqual(1); it["should update value"] = () => rx.value.shouldEqual(6); it["should invoke action"] = () => actionInvocations.shouldEqual(2); it["should invoke action with recomputed value"] = () => lastActionResult.shouldEqual(6); when["source changes, but transformation result is the same"] = () => { beforeEach += () => src.value = F.t(3, 2); it["should invoke mapper"] = () => mapperInvocations.shouldEqual(2); it["should keep the value same"] = () => rx.value.shouldEqual(6); it["should not invoke action"] = () => actionInvocations.shouldEqual(2); }; }; }; });
public void WhenMultipleItems() { Fn <int, bool> predicate = i => i % 2 != 0; Fn <Option <int>, bool> matchPredicate = _ => _.exists(predicate); var rx1 = RxRef.a(3); var rx2 = RxRef.a(4); var dst = new[] { rx1, rx2 }.anyThat(predicate); dst.value.shouldMatch(matchPredicate); rx1.value = 2; dst.value.shouldBeNone(); rx2.value = 5; dst.value.shouldMatch(matchPredicate); rx1.value = 1; dst.value.shouldMatch(matchPredicate); }
public void WhenMultipleItems() { var rx1 = RxRef.a(3.some()); var rx2 = RxRef.a(Option <int> .None); var dst = new[] { rx1, rx2 }.anyDefined(); dst.value.shouldBeSome(3); rx1.value = Option <int> .None; dst.value.shouldBeNone(); rx2.value = 4.some(); dst.value.shouldBeSome(4); rx1.value = 1.some(); dst.value.shouldBeAnySome(); rx2.value = Option <int> .None; dst.value.shouldBeSome(1); }
/** Convert an enum of rx values into one rx value using a traversal function. **/ public static IRxVal <B> traverse <A, B>( this IEnumerable <IRxVal <A> > vals, Fn <IEnumerable <A>, B> traverse ) { Fn <IEnumerable <A> > readValues = () => vals.Select(v => v.value); var val = RxRef.a(traverse(readValues())); // TODO: this is probably suboptimal. Act rescan = () => val.value = traverse(readValues()); foreach (var rxVal in vals) { rxVal.subscribe(_ => rescan(), emitCurrent: false); } rescan(); return(val); }
/* Returns first value that satisfies the predicate. */ public static IRxVal <Option <A> > firstThat <A>(this IEnumerable <IRxVal <A> > vals, Fn <A, bool> predicate) { var val = RxRef.a(F.none <A>()); // TODO: this is probably suboptimal. Act rescan = () => { foreach (var rxVal in vals.Where(rxVal => predicate(rxVal.value))) { val.value = F.some(rxVal.value); return; } val.value = F.none <A>(); }; foreach (var rxVal in vals) { rxVal.subscribe(_ => rescan(), emitCurrent: false); } rescan(); return(val); }
public RxList() { _rxSize = RxRef.a(0); }
public static ObserverBuilder <Elem, IRxVal <Elem> > builder <Elem>(Elem value) { return(RxRef.builder(value)); }