Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(#357) MappedProperty: should produce the current value on Advise #418

Merged
merged 2 commits into from
Jul 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions rd-net/Lifetimes/Collections/Viewable/ReactiveEx.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Threading.Tasks;
using JetBrains.Core;
using JetBrains.Lifetimes;
Expand Down Expand Up @@ -378,7 +377,7 @@ public MappedProperty(IViewableProperty<T> source, Func<T, R> map)
Change = new MappedSink<T, R>(source.Change, myMap);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This fix is not correct because Cange shouldn't provide an initial state, usually it is a signal.
I recommend rewriting the Advise method in the MappedProperty class as follows:

public void Advise(Lifetime lifetime, Action<R> handler) => mySource.Advise(lifetime, x => handler(myMap(x)));

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup, thanks, I'll take a look.

}

public void Advise(Lifetime lifetime, Action<R> handler) => Change.Advise(lifetime, handler);
public void Advise(Lifetime lifetime, Action<R> handler) => mySource.Advise(lifetime, v => handler(myMap(v)));

public ISource<R> Change { get; }

Expand Down Expand Up @@ -430,4 +429,4 @@ public static Task<T> NextValueAsync<T>(this ISource<T> source, Lifetime lifetim
}
#endif
}
}
}
30 changes: 30 additions & 0 deletions rd-net/Test.Lifetimes/Collections/Viewable/ViewablePropertyTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using JetBrains.Collections.Viewable;
using JetBrains.Lifetimes;
using NUnit.Framework;

namespace Test.Lifetimes.Collections.Viewable;

public class ViewablePropertyTest
{
[TestCase]
public void TestAdvise()
{
var prop = new ViewableProperty<int>(123);
Lifetime.Using(lt =>
{
int? value = null;
prop.Advise(lt, x => value = x);
Assert.AreEqual(123, value);
});
Lifetime.Using(lt =>
{
int? value = null;
var mapped = prop.Select(x => x + 1);
mapped.Advise(lt, x => value = x);
Assert.AreEqual(124, value);

prop.Value = 125;
Assert.AreEqual(126, value);
});
}
}
Loading