Skip to content

ObserveCountChanged misses count if observer has side effect #120

Description

@awesomestmason

If an observer modifies the underlying collection in ObserveCountChanged, the new count will not be published.

    [Fact]
    public void ObserveCountChanged_WithSideEffect()
    {
        var events = new List<int>();
        var collection = new ObservableList<int>([]);

        using var _ = collection.ObserveCountChanged().Subscribe(count =>
        {
            events.Add(count);
            // Side effect - when count is 1, clear the list
            if(count == 1) collection.Clear();
        });

        events.Should().BeEmpty();

        collection.Add(12);

        // collection: { }  (empty)
        // events: { 1 }    (expected:  { 1, 0 }

        collection.Count.Should().Be(0);   
        events.Should().BeEquivalentTo([1, 0]); // <- fails here  
    }
Expected events to be a collection with 2 item(s), but {1} contains 1 item(s) less than {1, 0}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions