TransformOnObservable vs AutoRefreshOnObservable + Transform? #1010
Replies: 2 comments 1 reply
-
They seem similar but are different. Which one you want depends on the specifics of the situation. Use Use |
Beta Was this translation helpful? Give feedback.
-
Thanks @dwcullop, that makes sense. However, I think there are particular cases in which both could used. I've set up a minimal example which is somewhat similar to a use case we have in production. In this particular case, the current value of the observable is also exposed as a property, so we're able to use class Example
{
private record Person(string Name, DateTimeOffset DateOfBirth);
// Calculates the age of a person and exposes it as an observable.
private class AgeService
{
// BehaviourSubject lets you access the current value of the observable
// This "pattern" is the same you'd have with an object that implements INotifyPropertyChanged
public BehaviorSubject<int?> AgeChanged { get; } = new(null);
public AgeService(Person person)
{
Observable
.Interval(TimeSpan.FromDays(1))
.Select(_ => ComputeAge(person))
.DistinctUntilChanged()
.Subscribe(newAge => AgeChanged.OnNext(newAge));
}
private static int ComputeAge(Person person)
{
return DateTimeOffset.Now.Year - person.DateOfBirth.Year;
}
}
private void TransformOnObservableVsAutoRefresh(IObservableCache<Person, string> cache)
{
// Option 1
cache
.Connect()
.Transform(person =>
(Person: person, AgeService: new AgeService(person)))
.TransformOnObservable(tuple => tuple
.AgeService
.AgeChanged
.Select(age => (tuple.Person, Age: age)))
.Subscribe( /*TODO*/ );
// Option 2
cache
.Connect()
.Transform(person =>
(Person: person, AgeService: new AgeService(person)))
.AutoRefreshOnObservable(tuple => tuple.AgeService.AgeChanged)
.Transform(tuple =>
(tuple.Person, tuple.AgeService.AgeChanged.Value),
transformOnRefresh: true)
.Subscribe( /*TODO*/ );
}
} As far as I can tell, the two options behave the same. If that's the case, is there any reason to prefer one over the other? Thanks again |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Ciao guys
Just wondering if the two operators are any different, or if they effectively behave the same. In case they're equivalent, is there any rule of thumb to follow when deciding which of the two to use?
Thanks
Lorenzo
Beta Was this translation helpful? Give feedback.
All reactions