Cysharp / Cysharp/ObservableCollections

ObserveCountChanged misses count if observer has side effect

Open
#120 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
C#
Stars
1k
Forks
73
Avg merge
5d 4h
Merged PRs (30d)
2

Description

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}

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by locating the ObserveCountChanged implementation and the existing test named ObserveCountChanged_WithSideEffect. Run that test to reproduce the missing notification when the observer clears the collection; done means the test passes with events containing [1, 0] and the collection count remaining 0.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
backend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
65/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.