From f8aa64f7871c884d37bb487ff9dfceec1c289ada Mon Sep 17 00:00:00 2001 From: neuecc Date: Fri, 1 Mar 2024 12:50:55 +0900 Subject: [PATCH] RP --- src/R3/ReactiveProperty.cs | 2 + tests/R3.Tests/ReactivePropertyTest.cs | 128 ++++++++++++++++++++++++- 2 files changed, 129 insertions(+), 1 deletion(-) diff --git a/src/R3/ReactiveProperty.cs b/src/R3/ReactiveProperty.cs index 9af158b4..d6225535 100644 --- a/src/R3/ReactiveProperty.cs +++ b/src/R3/ReactiveProperty.cs @@ -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; diff --git a/tests/R3.Tests/ReactivePropertyTest.cs b/tests/R3.Tests/ReactivePropertyTest.cs index 92c29bfb..b857b0d9 100644 --- a/tests/R3.Tests/ReactivePropertyTest.cs +++ b/tests/R3.Tests/ReactivePropertyTest.cs @@ -1,4 +1,6 @@ -namespace R3.Tests; +using R3.Collections; + +namespace R3.Tests; public class ReactivePropertyTest { @@ -106,4 +108,128 @@ public void CheckNode() rp.Value = 6; list6.AssertEqual([5, 6]); } + + [Fact] + public void NodeDisposeCheck() + { + { + var rp = new ReactiveProperty(1); + + LiveList list1; + LiveList list2; + LiveList list3 = null!; + LiveList list4 = null!; + LiveList 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(1); + + LiveList list1; + LiveList list2; + LiveList list3 = null!; + LiveList list4 = null!; + LiveList 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(1); + + LiveList list1; + LiveList list2; + LiveList list3 = null!; + LiveList list4 = null!; + LiveList 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(1); + + LiveList list1; + LiveList list2; + LiveList list3 = null!; + LiveList list4 = null!; + LiveList 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]); + } + } }