diff --git a/CHANGELOG.md b/CHANGELOG.md
index 7845d98..7a17c28 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -4,6 +4,13 @@ All notable changes to this package will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
+## [0.6.2] - 2024-11-02
+
+- Added the *ObservableUpdateFlag* to help performance when updating subscribers to the *ObservableDictionary*. By default is set *ObservableUpdateFlag.KeyUpdateOnly*
+
+**Fix**:
+- Fixed an issue that would no setup Remove update action to Subscribers when calling *Clear* on the *ObservableDictionary*
+
## [0.6.1] - 2024-11-01
**Fix**:
diff --git a/Runtime/ObservableDictionary.cs b/Runtime/ObservableDictionary.cs
index 9168028..5c3352b 100644
--- a/Runtime/ObservableDictionary.cs
+++ b/Runtime/ObservableDictionary.cs
@@ -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
+ }
+
///
/// A simple dictionary with the possibility to observe changes to it's elements defined rules
///
@@ -18,6 +28,11 @@ public interface IObservableDictionary : IEnumerable
/// Requests the element count of this dictionary
///
int Count { get; }
+
+ ///
+ /// Defines the configuration for the observable update done when updating elements in this dictionary
+ ///
+ ObservableUpdateFlag ObservableUpdateFlag { get; set; }
}
///
@@ -45,23 +60,35 @@ public interface IObservableDictionaryReader : IObservableDictiona
///
/// Observes to this dictionary changes with the given
///
+ ///
+ /// It needs the to NOT be set as
+ ///
void Observe(Action onUpdate);
///
/// Observes to this dictionary changes with the given when the given
/// data changes
///
+ ///
+ /// It needs the to NOT be set as
+ ///
void Observe(TKey key, Action onUpdate);
///
///
/// It invokes the given method before starting to observe to this dictionary
///
+ ///
+ /// It needs the to NOT be set as
+ ///
void InvokeObserve(TKey key, Action onUpdate);
///
/// Stops observing this dictionary with the given of any data changes
///
+ ///
+ /// It needs the to NOT be set as
+ ///
void StopObserving(Action onUpdate);
///
@@ -173,15 +200,18 @@ public class ObservableDictionary : IObservableDictionary
public int Count => Dictionary.Count;
///
+ public ObservableUpdateFlag ObservableUpdateFlag { get; set; }
+ ///
public ReadOnlyDictionary ReadOnlyDictionary => new ReadOnlyDictionary(Dictionary);
protected virtual IDictionary Dictionary { get; }
- protected ObservableDictionary() { }
+ private ObservableDictionary() { }
public ObservableDictionary(IDictionary dictionary)
{
Dictionary = dictionary;
+ ObservableUpdateFlag = ObservableUpdateFlag.KeyUpdateOnly;
}
///
@@ -227,7 +257,7 @@ 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++)
{
@@ -235,9 +265,12 @@ public virtual void Add(TKey key, TValue value)
}
}
- 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);
+ }
+ }
+ }
+
+ 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,7 +429,7 @@ 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++)
{
@@ -388,9 +437,12 @@ protected void InvokeUpdate(TKey key, TValue previousValue)
}
}
- 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);
+ }
}
}
}
diff --git a/package.json b/package.json
index a5dae5c..71d0472 100644
--- a/package.json
+++ b/package.json
@@ -2,7 +2,7 @@
"name": "com.gamelovers.dataextensions",
"displayName": "Unity Data Type Extensions",
"author": "Miguel Tomas",
- "version": "0.6.1",
+ "version": "0.6.2",
"unity": "2022.3",
"license": "MIT",
"description": "This package extends various sets of data types to be used in any type of data containers or persistent serializable data",