Skip to content

fix: ObserveCountChanged misses count if observer has side effect#121

Open
awesomestmason wants to merge 1 commit intoCysharp:masterfrom
awesomestmason:120-observecountchanged-misses-count
Open

fix: ObserveCountChanged misses count if observer has side effect#121
awesomestmason wants to merge 1 commit intoCysharp:masterfrom
awesomestmason:120-observecountchanged-misses-count

Conversation

@awesomestmason
Copy link
Copy Markdown

The issue is that OnNext can have any number of side effects. If a side effect modifies the collection.Count, then the countPrev will be inaccurate, because the collection.Count has changed. It was not the same value that was actually emitted.

        protected override void Handler(in NotifyCollectionChangedEventArgs<T> eventArgs)
        {
            switch (eventArgs.Action)
            {
                case NotifyCollectionChangedAction.Add:
                case NotifyCollectionChangedAction.Remove:
                case NotifyCollectionChangedAction.Reset when countPrev != collection.Count:
                    observer.OnNext(collection.Count);
                    break;
            }
            countPrev = collection.Count; // Issue: collection.Count be a different value from the emitted value above, if OnNext has side effects.
        }

To fix this, I moved the countPrev assignment to before the emission. That way it is guaranteed that countPrev matches the value that we actually emitted.

@awesomestmason
Copy link
Copy Markdown
Author

closes #120

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant