Cysharp / R3

The new future of dotnet/reactive and UniRx.
MIT License
2.25k stars 98 forks source link

Reactive property doesn't react to the changes during initial subscription #251

Closed orcharddweller closed 1 month ago

orcharddweller commented 2 months ago

Amazing library! I've found one weird thing. I'm not sure if this is a bug, but ReactiveProperty will ignore changes to its value if the changes happen based on the value pre-subscription.

example bellow

using R3;
using UnityEngine;

public class AsyncTest : MonoBehaviour
{
    private readonly ReactiveProperty<int> a = new();

    private void Start()
    {
        var d = Disposable.CreateBuilder();

        a.Do(v => Debug.Log($"a {v}")).Do(v =>
        {
            if (v < 20)
            {
                a.Value += 1;
            }
        }).Subscribe().AddTo(ref d);

        Debug.Log($"a == 1: {a.CurrentValue == 1}"); // this should be 20, not 1!

        a.Value = 0; // this 

        Debug.Log($"a == 20: {a.CurrentValue == 20}"); // this is correct

        d.RegisterTo(destroyCancellationToken);
    }
}
neuecc commented 2 months ago

Thank you. While the first value is being issued, it is still treated as though the Subscribe has not been completed. This is the specification.