Skip to content

Commit

Permalink
Fix ReactiveProperty push duplicate value on recursive subscribe
Browse files Browse the repository at this point in the history
  • Loading branch information
neuecc committed Mar 4, 2024
1 parent 661b27a commit 0cc629d
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/R3/ReactiveProperty.cs
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,11 @@ void OnNextCore(T value)
if (completeState.IsCompleted) return;

var node = Volatile.Read(ref root);
var last = node?.Previous;
while (node != null)
{
node.Observer.OnNext(value);
if (node == last) return;
node = node.Next;
}
}
Expand All @@ -125,9 +127,11 @@ public void OnErrorResume(Exception error)
OnReceiveError(error);

var node = Volatile.Read(ref root);
var last = node?.Previous;
while (node != null)
{
node.Observer.OnErrorResume(error);
if (node == last) return;
node = node.Next;
}
}
Expand All @@ -146,9 +150,11 @@ public void OnCompleted(Result result)
}

var node = Volatile.Read(ref root);
var last = node?.Previous;
while (node != null)
{
node.Observer.OnCompleted(result);
if (node == last) return;
node = node.Next;
}
}
Expand Down
25 changes: 25 additions & 0 deletions tests/R3.Tests/ReactivePropertyTest.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using R3.Collections;
using System.Collections.Generic;

namespace R3.Tests;

Expand Down Expand Up @@ -232,4 +233,28 @@ public void NodeDisposeCheck()
list5.AssertEqual([1, 10, 20]);
}
}

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

List<LiveList<int>> recList = new();

var list = rp.Do(x =>
{
recList.Add(rp.ToLiveList());
})
.ToLiveList();

list.AssertEqual([0]);
recList[0].AssertEqual([0]);

rp.Value = 99;
list.AssertEqual([0, 99]);
recList[0].AssertEqual([0, 99]);
recList[1].AssertEqual([99]);


}
}

0 comments on commit 0cc629d

Please sign in to comment.