-
Notifications
You must be signed in to change notification settings - Fork 1
Release 0.6.2 #9
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
Changes from all commits
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 | ||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -9,6 +9,16 @@ | |||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
namespace GameLovers | ||||||||||||||||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||||||||||||||||
public enum ObservableUpdateFlag | ||||||||||||||||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||||||||||||||||
// Updates all subsribers that didn't specify the key index | ||||||||||||||||||||||||||||||||||||||||
UpdateOnly, | ||||||||||||||||||||||||||||||||||||||||
// Updates only for subscripers that added their key index | ||||||||||||||||||||||||||||||||||||||||
KeyUpdateOnly, | ||||||||||||||||||||||||||||||||||||||||
// Updates all types of subscribers [This has a high performance cost] | ||||||||||||||||||||||||||||||||||||||||
Both | ||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
/// <summary> | ||||||||||||||||||||||||||||||||||||||||
/// A simple dictionary with the possibility to observe changes to it's elements defined <see cref="ObservableUpdateType"/> rules | ||||||||||||||||||||||||||||||||||||||||
/// </summary> | ||||||||||||||||||||||||||||||||||||||||
|
@@ -18,6 +28,11 @@ public interface IObservableDictionary : IEnumerable | |||||||||||||||||||||||||||||||||||||||
/// Requests the element count of this dictionary | ||||||||||||||||||||||||||||||||||||||||
/// </summary> | ||||||||||||||||||||||||||||||||||||||||
int Count { get; } | ||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
/// <summary> | ||||||||||||||||||||||||||||||||||||||||
/// Defines the configuration for the observable update done when updating elements in this dictionary | ||||||||||||||||||||||||||||||||||||||||
/// </summary> | ||||||||||||||||||||||||||||||||||||||||
ObservableUpdateFlag ObservableUpdateFlag { get; set; } | ||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
/// <inheritdoc cref="IObservableDictionary"/> | ||||||||||||||||||||||||||||||||||||||||
|
@@ -45,23 +60,35 @@ public interface IObservableDictionaryReader<TKey, TValue> : IObservableDictiona | |||||||||||||||||||||||||||||||||||||||
/// <summary> | ||||||||||||||||||||||||||||||||||||||||
/// Observes to this dictionary changes with the given <paramref name="onUpdate"/> | ||||||||||||||||||||||||||||||||||||||||
/// </summary> | ||||||||||||||||||||||||||||||||||||||||
/// <remarks> | ||||||||||||||||||||||||||||||||||||||||
/// It needs the <see cref="this.ObservableUpdateFlag"/> to NOT be set as <see cref="ObservableUpdateFlag.KeyUpdateOnly"/> | ||||||||||||||||||||||||||||||||||||||||
/// </remarks> | ||||||||||||||||||||||||||||||||||||||||
void Observe(Action<TKey, TValue, TValue, ObservableUpdateType> onUpdate); | ||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
/// <summary> | ||||||||||||||||||||||||||||||||||||||||
/// Observes to this dictionary changes with the given <paramref name="onUpdate"/> when the given <paramref name="key"/> | ||||||||||||||||||||||||||||||||||||||||
/// data changes | ||||||||||||||||||||||||||||||||||||||||
/// </summary> | ||||||||||||||||||||||||||||||||||||||||
/// <remarks> | ||||||||||||||||||||||||||||||||||||||||
/// It needs the <see cref="this.ObservableUpdateFlag"/> to NOT be set as <see cref="ObservableUpdateFlag.UpdateOnly"/> | ||||||||||||||||||||||||||||||||||||||||
/// </remarks> | ||||||||||||||||||||||||||||||||||||||||
void Observe(TKey key, Action<TKey, TValue, TValue, ObservableUpdateType> onUpdate); | ||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
/// <inheritdoc cref="Observe(TKey,System.Action{TKey,TValue,TValue,FirstLight.ObservableUpdateType})" /> | ||||||||||||||||||||||||||||||||||||||||
/// <remarks> | ||||||||||||||||||||||||||||||||||||||||
/// It invokes the given <paramref name="onUpdate"/> method before starting to observe to this dictionary | ||||||||||||||||||||||||||||||||||||||||
/// </remarks> | ||||||||||||||||||||||||||||||||||||||||
/// <remarks> | ||||||||||||||||||||||||||||||||||||||||
/// It needs the <see cref="this.ObservableUpdateFlag"/> to NOT be set as <see cref="ObservableUpdateFlag.UpdateOnly"/> | ||||||||||||||||||||||||||||||||||||||||
/// </remarks> | ||||||||||||||||||||||||||||||||||||||||
void InvokeObserve(TKey key, Action<TKey, TValue, TValue, ObservableUpdateType> onUpdate); | ||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
/// <summary> | ||||||||||||||||||||||||||||||||||||||||
/// Stops observing this dictionary with the given <paramref name="onUpdate"/> of any data changes | ||||||||||||||||||||||||||||||||||||||||
/// </summary> | ||||||||||||||||||||||||||||||||||||||||
/// <remarks> | ||||||||||||||||||||||||||||||||||||||||
/// It needs the <see cref="this.ObservableUpdateFlag"/> to NOT be set as <see cref="ObservableUpdateFlag.KeyUpdateOnly"/> | ||||||||||||||||||||||||||||||||||||||||
/// </remarks> | ||||||||||||||||||||||||||||||||||||||||
void StopObserving(Action<TKey, TValue, TValue, ObservableUpdateType> onUpdate); | ||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
/// <summary> | ||||||||||||||||||||||||||||||||||||||||
|
@@ -173,15 +200,18 @@ public class ObservableDictionary<TKey, TValue> : IObservableDictionary<TKey, TV | |||||||||||||||||||||||||||||||||||||||
/// <inheritdoc /> | ||||||||||||||||||||||||||||||||||||||||
public int Count => Dictionary.Count; | ||||||||||||||||||||||||||||||||||||||||
/// <inheritdoc /> | ||||||||||||||||||||||||||||||||||||||||
public ObservableUpdateFlag ObservableUpdateFlag { get; set; } | ||||||||||||||||||||||||||||||||||||||||
/// <inheritdoc /> | ||||||||||||||||||||||||||||||||||||||||
public ReadOnlyDictionary<TKey, TValue> ReadOnlyDictionary => new ReadOnlyDictionary<TKey, TValue>(Dictionary); | ||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
protected virtual IDictionary<TKey, TValue> Dictionary { get; } | ||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
protected ObservableDictionary() { } | ||||||||||||||||||||||||||||||||||||||||
private ObservableDictionary() { } | ||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
public ObservableDictionary(IDictionary<TKey, TValue> dictionary) | ||||||||||||||||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||||||||||||||||
Dictionary = dictionary; | ||||||||||||||||||||||||||||||||||||||||
ObservableUpdateFlag = ObservableUpdateFlag.KeyUpdateOnly; | ||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
/// <inheritdoc cref="Dictionary{TKey,TValue}.this" /> | ||||||||||||||||||||||||||||||||||||||||
|
@@ -227,17 +257,20 @@ public virtual void Add(TKey key, TValue value) | |||||||||||||||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||||||||||||||||
Dictionary.Add(key, value); | ||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
if (_keyUpdateActions.TryGetValue(key, out var actions)) | ||||||||||||||||||||||||||||||||||||||||
if (ObservableUpdateFlag != ObservableUpdateFlag.UpdateOnly && _keyUpdateActions.TryGetValue(key, out var actions)) | ||||||||||||||||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||||||||||||||||
for (var i = 0; i < actions.Count; i++) | ||||||||||||||||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||||||||||||||||
actions[i](key, default, value, ObservableUpdateType.Added); | ||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
for (var i = 0; i < _updateActions.Count; i++) | ||||||||||||||||||||||||||||||||||||||||
if (ObservableUpdateFlag != ObservableUpdateFlag.KeyUpdateOnly) | ||||||||||||||||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||||||||||||||||
_updateActions[i](key, default, value, ObservableUpdateType.Added); | ||||||||||||||||||||||||||||||||||||||||
for (var i = 0; i < _updateActions.Count; i++) | ||||||||||||||||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||||||||||||||||
_updateActions[i](key, default, value, ObservableUpdateType.Added); | ||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
|
@@ -251,17 +284,19 @@ public virtual bool Remove(TKey key) | |||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
Dictionary.Remove(key); | ||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
if (_keyUpdateActions.TryGetValue(key, out var actions)) | ||||||||||||||||||||||||||||||||||||||||
if (ObservableUpdateFlag != ObservableUpdateFlag.UpdateOnly && _keyUpdateActions.TryGetValue(key, out var actions)) | ||||||||||||||||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||||||||||||||||
for (var i = 0; i < actions.Count; i++) | ||||||||||||||||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||||||||||||||||
actions[i](key, value, default, ObservableUpdateType.Removed); | ||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
for (var i = 0; i < _updateActions.Count; i++) | ||||||||||||||||||||||||||||||||||||||||
if (ObservableUpdateFlag != ObservableUpdateFlag.KeyUpdateOnly) | ||||||||||||||||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||||||||||||||||
_updateActions[i](key, value, default, ObservableUpdateType.Removed); | ||||||||||||||||||||||||||||||||||||||||
for (var i = 0; i < _updateActions.Count; i++) | ||||||||||||||||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||||||||||||||||
_updateActions[i](key, value, default, ObservableUpdateType.Removed); | ||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
return true; | ||||||||||||||||||||||||||||||||||||||||
|
@@ -274,11 +309,25 @@ public virtual void Clear() | |||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
Dictionary.Clear(); | ||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
for (var i = 0; i < _updateActions.Count; i++) | ||||||||||||||||||||||||||||||||||||||||
if (ObservableUpdateFlag != ObservableUpdateFlag.UpdateOnly) | ||||||||||||||||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||||||||||||||||
foreach (var data in _keyUpdateActions) | ||||||||||||||||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||||||||||||||||
for (var i = 0; i < data.Value.Count; i++) | ||||||||||||||||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||||||||||||||||
data.Value[i](data.Key, dictionary[data.Key], default, ObservableUpdateType.Removed); | ||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||
Comment on lines
+314
to
+321
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. Prevent possible When invoking key-specific update actions during Apply this diff to fix the issue: - data.Value[i](data.Key, dictionary[data.Key], default, ObservableUpdateType.Removed);
+ if (dictionary.TryGetValue(data.Key, out var previousValue))
+ {
+ data.Value[i](data.Key, previousValue, default, ObservableUpdateType.Removed);
+ } 📝 Committable suggestion
Suggested change
|
||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
if (ObservableUpdateFlag != ObservableUpdateFlag.KeyUpdateOnly) | ||||||||||||||||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||||||||||||||||
foreach (var data in dictionary) | ||||||||||||||||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||||||||||||||||
_updateActions[i](data.Key, data.Value, default, ObservableUpdateType.Removed); | ||||||||||||||||||||||||||||||||||||||||
for (var i = 0; i < _updateActions.Count; i++) | ||||||||||||||||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||||||||||||||||
_updateActions[i](data.Key, data.Value, default, ObservableUpdateType.Removed); | ||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||
|
@@ -380,17 +429,20 @@ protected void InvokeUpdate(TKey key, TValue previousValue) | |||||||||||||||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||||||||||||||||
var value = Dictionary[key]; | ||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
if (_keyUpdateActions.TryGetValue(key, out var actions)) | ||||||||||||||||||||||||||||||||||||||||
if (ObservableUpdateFlag != ObservableUpdateFlag.UpdateOnly && _keyUpdateActions.TryGetValue(key, out var actions)) | ||||||||||||||||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||||||||||||||||
for (var i = 0; i < actions.Count; i++) | ||||||||||||||||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||||||||||||||||
actions[i](key, previousValue, value, ObservableUpdateType.Updated); | ||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
for (var i = 0; i < _updateActions.Count; i++) | ||||||||||||||||||||||||||||||||||||||||
if (ObservableUpdateFlag != ObservableUpdateFlag.KeyUpdateOnly) | ||||||||||||||||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||||||||||||||||
_updateActions[i](key, previousValue, value, ObservableUpdateType.Updated); | ||||||||||||||||||||||||||||||||||||||||
for (var i = 0; i < _updateActions.Count; i++) | ||||||||||||||||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||||||||||||||||
_updateActions[i](key, previousValue, value, ObservableUpdateType.Updated); | ||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||
|
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.
Correct typos in enum member comments
There are spelling errors in the comments for the
ObservableUpdateFlag
enum members: