/** 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); }
ToggleableMultiplier( float multiplier, RandomList <ToggleableMultiplier> list, IRxRef <float> totalMultiplier, bool active ) { _multiplier = multiplier; this.list = list; this.totalMultiplier = totalMultiplier; _active = active; }
public static ISubscription bind <T>( this IRxRef <T> subject, dfLabel control, Fn <T, string> mapper, Fn <string, T> comapper ) { return(subject.bind( mapper, comapper, text => control.Text = text, handler => control.TextChanged += handler, handler => control.TextChanged -= handler )); }
static void inAsyncSeq <A, B>( IEnumerator <A> e, IRxRef <Option <B> > rxRef, Fn <A, Future <B> > asyncAction ) { if (!e.MoveNext()) { return; } asyncAction(e.Current).onComplete(b => { rxRef.value = F.some(b); inAsyncSeq(e, rxRef, asyncAction); }); }
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 static ISubscription bind <T>( this IRxRef <Option <T> > subject, IEnumerable <dfCheckbox> checkboxes, Fn <T, string> mapper, Fn <string, T> comapper ) { Action uncheckAll = () => { foreach (var cb in checkboxes) { cb.IsChecked = false; } }; Act <Option <T>, string> check = (v, name) => checkboxes.hIter().find(cb => cb.name == name).voidFold( () => { throw new Exception(String.Format( "Can't find checkbox with name {0} which was mapped from {1}", name, v )); }, cb => cb.IsChecked = true ); uncheckAll(); subject.value.map(mapper).each(name => check(subject.value, name)); var subscription = subject.subscribe(v => v.map(mapper).voidFold(uncheckAll, name => check(v, name)) ); PropertyChangedEventHandler <bool> handler = (control, selected) => { if (selected) { subject.value = F.some(comapper(control.name)); } }; foreach (var cb in checkboxes) { cb.CheckChanged += handler; } return(new Subscription(() => { subscription.unsubscribe(); foreach (var cb in checkboxes) { cb.CheckChanged -= handler; } })); }
public static ISubscription bind <T>( this IRxRef <T> subject, Fn <T, string> mapper, Fn <string, T> comapper, Act <string> changeControlText, Act <PropertyChangedEventHandler <string> > subscribeToControlChanged, Act <PropertyChangedEventHandler <string> > unsubscribeToControlChanged ) { var f = mapper.andThen(changeControlText); var subscription = subject.subscribe(f); PropertyChangedEventHandler <string> handler = (c, value) => subject.value = comapper(value); subscribeToControlChanged(handler); return(new Subscription(() => { unsubscribeToControlChanged(handler); subscription.unsubscribe(); })); }
/*********** Two-way binds ***********/ public static ISubscription bind <T>( this IRxRef <T> subject, IEnumerable <dfCheckbox> checkboxes, Fn <T, string> mapper, Fn <string, T> comapper ) { var optSubject = RxRef.a(F.some(subject.value)); var optSubjectSourceSubscription = subject.subscribe(v => optSubject.value = F.some(v) ); var optSubjectTargetSubscription = optSubject.subscribe(opt => opt.each(v => subject.value = v) ); var bindSubscription = optSubject.bind(checkboxes, mapper, comapper); return(new Subscription(() => { optSubjectSourceSubscription.unsubscribe(); optSubjectTargetSubscription.unsubscribe(); bindSubscription.unsubscribe(); })); }
/// <summary> /// This class calculates realtimeSinceStartup, /// but excludes time intervals when an ad is showing or application is paused /// /// on android - interstitials usually run on a separate activity (application gets paused/resumed automatically) /// on IOS and some android ad networks - application does not get paused, so we need to call `setPaused` ourselves /// </summary> RealTimeButPauseWhenAdIsShowing() { var pauseStarted = Time.realtimeSinceStartup; externalPause = RxRef.a(false); ASync.onAppPause.toRxVal(false).zip(externalPause, F.or2).subscribeWithoutEmit( NeverDisposeDisposableTracker.instance, paused => { isPaused = paused; if (paused) { pauseStarted = Time.realtimeSinceStartup; } else { var secondsPaused = Time.realtimeSinceStartup - pauseStarted; totalSecondsPaused += secondsPaused; } } ); }
public static ISubscription bind( this IRxRef <uint> subject, dfLabel control ) { return(subject.bind(control, uintMapper, uintComapper)); }
public static ISubscription bind( this IRxRef <string> subject, dfLabel control ) { return(subject.bind(control, strMapper, strMapper)); }
public static ISubscription bind( this IRxRef <int> subject, dfTextbox control ) { return(subject.bind(control, intMapper, intComapper)); }
public RxList() { _rxSize = RxRef.a(0); }
public static IRxVal <A> asVal <A>(this IRxRef <A> rx) => rx;