-
Notifications
You must be signed in to change notification settings - Fork 1
Release 0.6.5 #12
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Release 0.6.5 #12
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ | |
using System.Collections; | ||
using System.Collections.Generic; | ||
using System.Collections.ObjectModel; | ||
using System.Linq; | ||
using UnityEngine.UIElements; | ||
|
||
// ReSharper disable once CheckNamespace | ||
|
@@ -275,16 +276,18 @@ public virtual bool Remove(TKey key) | |
|
||
if (ObservableUpdateFlag != ObservableUpdateFlag.UpdateOnly && _keyUpdateActions.TryGetValue(key, out var actions)) | ||
{ | ||
for (var i = 0; i < actions.Count; i++) | ||
var listCopy = actions.ToList(); | ||
for (var i = 0; i < listCopy.Count; i++) | ||
{ | ||
actions[i](key, value, default, ObservableUpdateType.Removed); | ||
listCopy[i](key, value, default, ObservableUpdateType.Removed); | ||
} | ||
} | ||
if (ObservableUpdateFlag != ObservableUpdateFlag.KeyUpdateOnly) | ||
{ | ||
for (var i = 0; i < _updateActions.Count; i++) | ||
var listCopy = _updateActions.ToList(); | ||
for (var i = 0; i < listCopy.Count; i++) | ||
{ | ||
_updateActions[i](key, value, default, ObservableUpdateType.Removed); | ||
listCopy[i](key, value, default, ObservableUpdateType.Removed); | ||
} | ||
} | ||
|
||
|
@@ -294,31 +297,34 @@ public virtual bool Remove(TKey key) | |
/// <inheritdoc /> | ||
public virtual void Clear() | ||
{ | ||
var dictionary = new Dictionary<TKey, TValue>(Dictionary); | ||
|
||
Dictionary.Clear(); | ||
|
||
if (ObservableUpdateFlag != ObservableUpdateFlag.UpdateOnly) | ||
{ | ||
foreach (var data in _keyUpdateActions) | ||
// Create a copy in case that one of the callbacks modifies the list (Ex: removing a subscriber) | ||
var copy = new Dictionary<TKey, IList<Action<TKey, TValue, TValue, ObservableUpdateType>>>(_keyUpdateActions); | ||
|
||
foreach (var data in copy) | ||
{ | ||
for (var i = 0; i < data.Value.Count; i++) | ||
var listCopy = data.Value.ToList(); | ||
for (var i = 0; i < listCopy.Count; i++) | ||
{ | ||
data.Value[i](data.Key, dictionary[data.Key], default, ObservableUpdateType.Removed); | ||
listCopy[i](data.Key, Dictionary[data.Key], default, ObservableUpdateType.Removed); | ||
} | ||
} | ||
} | ||
|
||
if (ObservableUpdateFlag != ObservableUpdateFlag.KeyUpdateOnly) | ||
{ | ||
foreach (var data in dictionary) | ||
foreach (var data in Dictionary) | ||
{ | ||
for (var i = 0; i < _updateActions.Count; i++) | ||
var listCopy = _updateActions.ToList(); | ||
for (var i = 0; i < listCopy.Count; i++) | ||
{ | ||
_updateActions[i](data.Key, data.Value, default, ObservableUpdateType.Removed); | ||
listCopy[i](data.Key, data.Value, default, ObservableUpdateType.Removed); | ||
} | ||
} | ||
} | ||
|
||
Dictionary.Clear(); | ||
Comment on lines
307
to
+334
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The 387: - actions.Value.RemoveAt(i);
+ actions.Value.RemoveAt(i);
+ break; 397: - _updateActions.RemoveAt(i);
+ _updateActions.RemoveAt(i);
+ break;
Comment on lines
307
to
+334
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The 387: 397:
Comment on lines
307
to
+334
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The 387: 397: |
||
} | ||
|
||
/// <inheritdoc /> | ||
|
@@ -371,6 +377,7 @@ public void StopObserving(Action<TKey, TValue, TValue, ObservableUpdateType> onU | |
if (actions.Value[i] == onUpdate) | ||
{ | ||
actions.Value.RemoveAt(i); | ||
break; | ||
} | ||
} | ||
} | ||
Comment on lines
384
to
390
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
The code for removing an action from the private void RemoveActionFromList(Action<TKey, TValue, TValue, ObservableUpdateType> onUpdate,
IDictionary<TKey, IList<Action<TKey, TValue, TValue, ObservableUpdateType>>> actionsDictionary)
{
foreach (var actions in actionsDictionary.Values)
{
- for (var i = 0; i < actions.Count; i++)
+ actions.Remove(onUpdate);
+ }
+}
private void RemoveActionFromList(Action<TKey, TValue, TValue, ObservableUpdateType> onUpdate,
IList<Action<TKey, TValue, TValue, ObservableUpdateType>> actionsList)
{
- if (actions[i] == onUpdate)
- {
- actions.RemoveAt(i);
- }
- }
-}
-
-private int AdjustIndex(int index, Action<TKey, TValue, TValue, ObservableUpdateType> action,
- IList<Action<TKey, TValue, TValue, ObservableUpdateType>> list)
-{
- if (index < list.Count && list[index] == action)
- {
- return index;
- }
-
- for (var i = index - 1; i > -1; i--)
- {
- if (list[index] == action)
- {
- return i;
- }
- }
-
- return index + 1;
+ actionsList.Remove(onUpdate);
} |
||
|
@@ -380,6 +387,7 @@ public void StopObserving(Action<TKey, TValue, TValue, ObservableUpdateType> onU | |
if (_updateActions[i] == onUpdate) | ||
{ | ||
_updateActions.RemoveAt(i); | ||
break; | ||
} | ||
} | ||
} | ||
|
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -125,6 +125,21 @@ public void StopObserveCheck() | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
_caller.DidNotReceive().Call(Arg.Any<int>(), Arg.Any<int>(), Arg.Any<int>(), Arg.Any<ObservableUpdateType>()); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
[Test] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
public void StopObserve_MultipleCalls_StopsOnlyOne() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
_list.Observe(_caller.Call); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
_list.Observe(_caller.Call); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
_list.StopObserving(_caller.Call); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
_list.Add(_previousValue); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
_list[_index] = _previousValue; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
_list.RemoveAt(_index); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
_caller.Received().Call(Arg.Any<int>(), Arg.Any<int>(), Arg.Any<int>(), Arg.Any<ObservableUpdateType>()); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Strengthen test assertions for better verification While the test correctly verifies that observers remain active after one StopObserving call, the assertions could be more specific to ensure correct behavior. Consider this improved implementation: [Test]
-public void StopObserve_MultipleCalls_StopsOnlyOne()
+public void StopObserve_WhenCalledOnce_RemovesOnlyOneObserverInstance()
{
_list.Observe(_caller.Call);
_list.Observe(_caller.Call);
_list.StopObserving(_caller.Call);
_list.Add(_previousValue);
_list[_index] = _previousValue;
_list.RemoveAt(_index);
- _caller.Received().Call(Arg.Any<int>(), Arg.Any<int>(), Arg.Any<int>(), Arg.Any<ObservableUpdateType>());
+ // Verify each operation was observed exactly once (not twice)
+ _caller.Received(1).Call(Arg.Any<int>(), Arg.Is(0), Arg.Is(_previousValue), ObservableUpdateType.Added);
+ _caller.Received(1).Call(_index, _previousValue, _previousValue, ObservableUpdateType.Updated);
+ _caller.Received(1).Call(_index, _previousValue, 0, ObservableUpdateType.Removed);
} This improves the test by:
📝 Committable suggestion
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
[Test] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
public void StopObservingAllCheck() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
@@ -137,7 +152,7 @@ public void StopObservingAllCheck() | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
[Test] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
public void StopObservingAll_MultipleCalls_Check() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
public void StopObservingAll_MultipleCalls_StopsAll() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
_list.Observe(_caller.Call); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
_list.Observe(_caller.Call); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's a syntax error in the
add
function. Thereturn
keyword is misspelled asretrn
. It should be corrected as follows: