Skip to content

Commit

Permalink
Merge pull request #418 from JetBrains/im/357.mapped-property-advise
Browse files Browse the repository at this point in the history
(#357) MappedProperty: should produce the current value on Advise
  • Loading branch information
Iliya-usov authored Jul 11, 2023
2 parents 1493639 + c240afc commit 6a58715
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 3 deletions.
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);
}

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);
});
}
}

0 comments on commit 6a58715

Please sign in to comment.