Skip to content

Commit

Permalink
RP
Browse files Browse the repository at this point in the history
  • Loading branch information
neuecc committed Mar 1, 2024
1 parent 71440bf commit f8aa64f
Show file tree
Hide file tree
Showing 2 changed files with 129 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/R3/ReactiveProperty.cs
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,8 @@ public void Dispose()
var p = Interlocked.Exchange(ref parent, null);
if (p == null) return;

// keep this.Next for dispose on iterating

if (this.Previous == this) // single list
{
p.root = null;
Expand Down
128 changes: 127 additions & 1 deletion tests/R3.Tests/ReactivePropertyTest.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace R3.Tests;
using R3.Collections;

namespace R3.Tests;

public class ReactivePropertyTest
{
Expand Down Expand Up @@ -106,4 +108,128 @@ public void CheckNode()
rp.Value = 6;
list6.AssertEqual([5, 6]);
}

[Fact]
public void NodeDisposeCheck()
{
{
var rp = new ReactiveProperty<int>(1);

LiveList<int> list1;
LiveList<int> list2;
LiveList<int> list3 = null!;
LiveList<int> list4 = null!;
LiveList<int> list5;

list1 = rp.ToLiveList();
list2 = rp.Take(2).Do(_ => { list3?.Dispose(); list4?.Dispose(); }).ToLiveList();
list3 = rp.ToLiveList();
list4 = rp.ToLiveList();
list5 = rp.ToLiveList();

rp.Value = 10;

list1.AssertEqual([1, 10]);
list2.AssertEqual([1, 10]);
list3.AssertEqual([1]);
list4.AssertEqual([1]);
list5.AssertEqual([1, 10]);
}
{
// Dispose only self.
var rp = new ReactiveProperty<int>(1);

LiveList<int> list1;
LiveList<int> list2;
LiveList<int> list3 = null!;
LiveList<int> list4 = null!;
LiveList<int> list5;

list1 = rp.ToLiveList();
list2 = rp.Take(2).Do(_ => { /* list3?.Dispose(); list4?.Dispose(); */}).ToLiveList();
list3 = rp.ToLiveList();
list4 = rp.ToLiveList();
list5 = rp.ToLiveList();

rp.Value = 10;

list1.AssertEqual([1, 10]);
list2.AssertEqual([1, 10]);
list3.AssertEqual([1, 10]);
list4.AssertEqual([1, 10]);
list5.AssertEqual([1, 10]);

rp.Value = 20;

list1.AssertEqual([1, 10, 20]);
list2.AssertEqual([1, 10]);
list3.AssertEqual([1, 10, 20]);
list4.AssertEqual([1, 10, 20]);
list5.AssertEqual([1, 10, 20]);
}
{
// Dispose only next one.
var rp = new ReactiveProperty<int>(1);

LiveList<int> list1;
LiveList<int> list2;
LiveList<int> list3 = null!;
LiveList<int> list4 = null!;
LiveList<int> list5;

list1 = rp.ToLiveList();
list2 = rp.Do(_ => { list3?.Dispose(); /* list4?.Dispose(); */}).ToLiveList();
list3 = rp.ToLiveList();
list4 = rp.ToLiveList();
list5 = rp.ToLiveList();

rp.Value = 10;

list1.AssertEqual([1, 10]);
list2.AssertEqual([1, 10]);
list3.AssertEqual([1]);
list4.AssertEqual([1, 10]);
list5.AssertEqual([1, 10]);

rp.Value = 20;

list1.AssertEqual([1, 10, 20]);
list2.AssertEqual([1, 10, 20]);
list3.AssertEqual([1]);
list4.AssertEqual([1, 10, 20]);
list5.AssertEqual([1, 10, 20]);
}
{
// Dispose only next next one.
var rp = new ReactiveProperty<int>(1);

LiveList<int> list1;
LiveList<int> list2;
LiveList<int> list3 = null!;
LiveList<int> list4 = null!;
LiveList<int> list5;

list1 = rp.ToLiveList();
list2 = rp.Do(_ => { list4?.Dispose(); }).ToLiveList();
list3 = rp.ToLiveList();
list4 = rp.ToLiveList();
list5 = rp.ToLiveList();

rp.Value = 10;

list1.AssertEqual([1, 10]);
list2.AssertEqual([1, 10]);
list3.AssertEqual([1, 10]);
list4.AssertEqual([1]);
list5.AssertEqual([1, 10]);

rp.Value = 20;

list1.AssertEqual([1, 10, 20]);
list2.AssertEqual([1, 10, 20]);
list3.AssertEqual([1, 10, 20]);
list4.AssertEqual([1]);
list5.AssertEqual([1, 10, 20]);
}
}
}

0 comments on commit f8aa64f

Please sign in to comment.