Skip to content

Commit

Permalink
Simplify over engineered code
Browse files Browse the repository at this point in the history
Just Observable.FromAsync instead of Observable.DeferAsync
  • Loading branch information
b-straub committed Jun 21, 2024
1 parent 0f806de commit fc2d327
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 58 deletions.
22 changes: 8 additions & 14 deletions RxBlazorLightCoreTestBase/ServiceFixture.State.cs
Original file line number Diff line number Diff line change
Expand Up @@ -179,28 +179,24 @@ public IDisposable ObserveState(IStateObserverAsync observer)
public IDisposable ObserveStateComplex(IStateObserverAsync observer, ITestOutputHelper outputHelper)
{
var startObservable = Observable
.Return(false)
.Select(_ => Observable.DeferAsync(async token =>
.FromAsync(async ct =>
{
outputHelper.WriteLine("StartFirstWork");
await Task.Delay(1000, token);
await Task.Delay(1000, ct);
IntState.SetValue(10);
outputHelper.WriteLine("StopFirstWork");
return Observable.Timer(TimeSpan.FromSeconds(1));
}))
.Switch();

});

var stopObservable = Observable
.Return(false)
.Select(_ => Observable.DeferAsync(async token =>
.FromAsync(async ct =>
{
outputHelper.WriteLine("StartLastWork");
await Task.Delay(1000, token);
await Task.Delay(1000, ct);
IntState.SetValue(20);
outputHelper.WriteLine("StopLastWork");
return Observable.Return(0);
}))
.Switch();
});

long progress = 0;

Expand All @@ -216,10 +212,8 @@ public IDisposable ObserveStateComplex(IStateObserverAsync observer, ITestOutput
.Subscribe(observer);
}

public IDisposable ObserveStateThrow(IStateObserverAsync observer)
public static IDisposable ObserveStateThrow(IStateObserverAsync observer)
{
var error = new InvalidOperationException("ObserveStateException");

return Observable
.Interval(TimeSpan.FromSeconds(2))
.Timeout(TimeSpan.FromSeconds(1))
Expand Down
6 changes: 3 additions & 3 deletions RxBlazorLightCoreTests/StateTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -806,7 +806,7 @@ public async Task TestStateObserverComplex()
await Task.Delay(5);
}

Assert.InRange(stateChangeCount, 6, 7);
Assert.InRange(stateChangeCount, 4, 5);
Assert.Equal(20, fixture.IntState.Value);
}

Expand Down Expand Up @@ -883,7 +883,7 @@ IDisposable subscribeTest()

var disposable = subscribeTest();
fixture.ResetExceptions();
fixture.CancellableObserverAsync.ExecuteAsync(fixture.ObserveStateThrow);
fixture.CancellableObserverAsync.ExecuteAsync(ServiceFixture.ObserveStateThrow);
while (!done)
{
await Task.Delay(5);
Expand Down Expand Up @@ -931,7 +931,7 @@ IDisposable subscribeTest()

var disposable = subscribeTest();
fixture.ResetExceptions();
fixture.CancellableObserverHandleErrorAsync.ExecuteAsync(fixture.ObserveStateThrow);
fixture.CancellableObserverHandleErrorAsync.ExecuteAsync(ServiceFixture.ObserveStateThrow);
while (!done)
{
await Task.Delay(5);
Expand Down
83 changes: 42 additions & 41 deletions RxMudBlazorLightTestBase/Service/TestService.State.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Reactive.Linq;
using System.Reactive;
using System.Reactive.Linq;
using RxBlazorLightCore;

namespace RxMudBlazorLightTestBase.Service
Expand All @@ -9,21 +10,15 @@ public sealed partial class TestService

public Action IncrementCounter => () => Counter++;

public static Action IncrementCounterIndirect(int value, Action<int> setter) => () =>
{
setter(++value);
};
public static Action IncrementCounterIndirect(int value, Action<int> setter) => () => { setter(++value); };

public Func<IStateCommandAsync, Task> IncrementCounterAsync => async _ =>
{
await Task.Delay(1000);
Counter++;
};

public Action AddToCounter(int value) => () =>
{
Counter += value;
};
public Action AddToCounter(int value) => () => { Counter += value; };

public Func<IStateCommandAsync, Task> AddToCounterAsync(int value)
{
Expand All @@ -37,72 +32,78 @@ public Func<IStateCommandAsync, Task> AddToCounterAsync(int value)
public Func<IStateObserverAsync, IDisposable> IncrementCounterObservable => observer =>
{
var stopObservable = Observable
.Return(false)
.Select(_ => Observable.DeferAsync(async ct =>
.FromAsync(async ct =>
{
await Task.Delay(2000, ct);
Counter++;
return Observable.Timer(TimeSpan.FromSeconds(1));
}))
.Switch();
});

return Observable
.Interval(TimeSpan.FromMilliseconds(100))
.TakeUntil(stopObservable)
.Select(_ => -1L)
.Subscribe(observer);
};

public Func<IStateObserverAsync, IDisposable> IncrementCounterTimeoutObservable => observer =>
{
var stopObservable = Observable
.Return(false)
.Select(_ => Observable.DeferAsync(async ct =>
.FromAsync(async ct =>
{
await Task.Delay(2000, ct);
Counter++;
return Observable.Timer(TimeSpan.FromSeconds(1));
}))
.Switch();
});

return Observable
.Interval(TimeSpan.FromMilliseconds(100))
.TakeUntil(stopObservable)
.Select(i => i > 10 ? throw new TimeoutException() : -1L)
.Subscribe(observer);
};

public Func<IStateObserverAsync, IDisposable> AddCounterObservable => observer =>
{
var startObservable = Observable
.Return(false)
.Select(_ => Observable.DeferAsync(async token =>
.FromAsync(async ct =>
{
await Task.Delay(1000, token);
await Task.Delay(1000, ct);
Counter += 10;
return Observable.Timer(TimeSpan.FromSeconds(1));
}))
.Switch();

return Observable.Return(Unit.Default);
});

var stopObservable = Observable
.Return(false)
.Select(_ => Observable.DeferAsync(async token =>
.FromAsync(async ct =>
{
await Task.Delay(1000, token);
await Task.Delay(1000, ct);
Counter += 10;
return Observable.Return(0);
}))
.Switch();
return Observable.Return(Unit.Default);
});

var triggerObservable = Observable
.Interval(TimeSpan.FromMilliseconds(100))
.Select(i =>
{
Console.WriteLine($"Interval: {i}");
return i;
})
.TakeUntil(i => i > 2000);

long progress = 0;

return Observable
.Interval(TimeSpan.FromMilliseconds(30))
.Interval(TimeSpan.FromMilliseconds(40))
.TakeUntil(startObservable)
.Concat(
Observable
.Interval(TimeSpan.FromMilliseconds(30))
.TakeUntil(stopObservable)
.Interval(TimeSpan.FromMilliseconds(40))
.TakeUntil(triggerObservable)
.Concat(
Observable
.Interval(TimeSpan.FromMilliseconds(40))
.TakeUntil(stopObservable)
)
)
.Select(_ => progress++)
.Subscribe(observer);
Expand Down Expand Up @@ -151,10 +152,10 @@ public bool RatingValueCanChange(int _)
}

public static Func<Pizza, Pizza, Task> ChangePizzaAsync => async (o, n) =>
{
await Task.Delay(1000);
Console.WriteLine($"{o}, {n}");
};
{
await Task.Delay(1000);
Console.WriteLine($"{o}, {n}");
};

public static Action<Pizza, Pizza> ChangePizza(string value)
{
Expand All @@ -167,4 +168,4 @@ public static Action<Pizza, Pizza> ChangePizza(string value)

public static Action<TestColor, TestColor> ChangeTestColor => (o, n) => Console.WriteLine($"{o}, {n}");
}
}
}

0 comments on commit fc2d327

Please sign in to comment.