Skip to content

Commit 6a58715

Browse files
authored
Merge pull request #418 from JetBrains/im/357.mapped-property-advise
(#357) MappedProperty: should produce the current value on Advise
2 parents 1493639 + c240afc commit 6a58715

File tree

2 files changed

+32
-3
lines changed

2 files changed

+32
-3
lines changed

rd-net/Lifetimes/Collections/Viewable/ReactiveEx.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using System;
22
using System.Collections.Generic;
3-
using System.Diagnostics.CodeAnalysis;
43
using System.Threading.Tasks;
54
using JetBrains.Core;
65
using JetBrains.Lifetimes;
@@ -378,7 +377,7 @@ public MappedProperty(IViewableProperty<T> source, Func<T, R> map)
378377
Change = new MappedSink<T, R>(source.Change, myMap);
379378
}
380379

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

383382
public ISource<R> Change { get; }
384383

@@ -430,4 +429,4 @@ public static Task<T> NextValueAsync<T>(this ISource<T> source, Lifetime lifetim
430429
}
431430
#endif
432431
}
433-
}
432+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
using JetBrains.Collections.Viewable;
2+
using JetBrains.Lifetimes;
3+
using NUnit.Framework;
4+
5+
namespace Test.Lifetimes.Collections.Viewable;
6+
7+
public class ViewablePropertyTest
8+
{
9+
[TestCase]
10+
public void TestAdvise()
11+
{
12+
var prop = new ViewableProperty<int>(123);
13+
Lifetime.Using(lt =>
14+
{
15+
int? value = null;
16+
prop.Advise(lt, x => value = x);
17+
Assert.AreEqual(123, value);
18+
});
19+
Lifetime.Using(lt =>
20+
{
21+
int? value = null;
22+
var mapped = prop.Select(x => x + 1);
23+
mapped.Advise(lt, x => value = x);
24+
Assert.AreEqual(124, value);
25+
26+
prop.Value = 125;
27+
Assert.AreEqual(126, value);
28+
});
29+
}
30+
}

0 commit comments

Comments
 (0)