diff --git a/CHANGELOG.md b/CHANGELOG.md index e2e72d2..b9c9940 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,14 @@ # Changelog All notable changes to this package will be documented in this file. +## [1.2.2] - 2023-04-20 + +### Fixed +- The FuncFlux and FuncFluxParam as design is planned to only add one per key, but you can add multiple keys instead, thats why we reduce the complexity of optimization to just use Func< TResult > as TStorage + +### Removed +- Removed dictionary_read in ActionFlux, ActionFluxParam, FuncFlux and FuncFluxParam + ## [1.2.1] - 2023-04-17 ### Fixed diff --git a/Runtime/Core/Internal/ActionFlux.cs b/Runtime/Core/Internal/ActionFlux.cs index 9abfb7b..291c275 100644 --- a/Runtime/Core/Internal/ActionFlux.cs +++ b/Runtime/Core/Internal/ActionFlux.cs @@ -34,18 +34,6 @@ internal sealed class ActionFlux : IFlux // internal Dictionary dictionary = new Dictionary(); // internal Dictionary> dictionary = new Dictionary>(); internal Dictionary> dictionary = new Dictionary>(); - /// - /// A Read Only dictionary wich contains dictionary field - /// - // internal readonly IReadOnlyDictionary dictionary_read = null; - internal readonly IReadOnlyDictionary> dictionary_read = null; - /// - /// Constructor of ActionFLux - /// - public ActionFlux() - { - dictionary_read = dictionary; - } /// /// Subscribes an event to the action dictionary if the given condition is met /// @@ -54,36 +42,19 @@ public ActionFlux() ///Action to execute when the event is triggered void IStore.Store(in bool condition, TKey key, Action action) { - if(dictionary_read.TryGetValue(key, out var values)) + if(dictionary.TryGetValue(key, out var values)) { if (condition) values.Add(action); else values.Remove(action); } else if (condition) dictionary.Add(key, new HashSet(){action}); - // if(dictionary_read.TryGetValue(key, out var values)) - // { - // if (condition) values.Add(action); - // else values.Remove(action); - // } - // else if (condition) dictionary.Add(key, new List(){action}); - // if(dictionary_read.ContainsKey(key)) - // { - // if (condition) dictionary[key] += action; - // else dictionary[key] -= action; - // } - // else if (condition) dictionary.Add(key, action); } /// /// Triggers the function stored in the dictionary with the specified key. /// void IFlux.Dispatch(TKey key) { - // if(dictionary_read.TryGetValue(key, out var _actions)) _actions?.Invoke(); - // if(dictionary_read.TryGetValue(key, out var _actions)) - // { - // for (int i = 0; i < _actions.Count; i++) _actions[i].Invoke(); - // } - if(dictionary_read.TryGetValue(key, out var _actions)) + if(dictionary.TryGetValue(key, out var _actions)) { foreach (var item in _actions) item.Invoke(); } diff --git a/Runtime/Core/Internal/ActionFluxParam.cs b/Runtime/Core/Internal/ActionFluxParam.cs index bfa764b..d2f19ad 100644 --- a/Runtime/Core/Internal/ActionFluxParam.cs +++ b/Runtime/Core/Internal/ActionFluxParam.cs @@ -51,12 +51,6 @@ public ActionFluxParam() ///Action to execute when the event is triggered void IStore>.Store(in bool condition, TKey key, Action action) { - // if(dictionary_read.ContainsKey(key)) - // { - // if (condition) dictionary[key] += action; - // else dictionary[key] -= action; - // } - // else if (condition) dictionary.Add(key, action); if(dictionary_read.TryGetValue(key, out var values)) { if (condition) values.Add(action); @@ -69,11 +63,6 @@ public ActionFluxParam() /// void IFluxParam>.Dispatch(TKey key, TValue param) { - // if(dictionary_read.TryGetValue(key, out var _actions)) _actions?.Invoke(param); - // if(dictionary_read.TryGetValue(key, out var _actions)) - // { - // for (int i = 0; i < _actions.Count; i++) _actions[i].Invoke(param); - // } if(dictionary_read.TryGetValue(key, out var _actions)) { foreach (var item in _actions) item.Invoke(param); diff --git a/Runtime/Core/Internal/FuncFlux.cs b/Runtime/Core/Internal/FuncFlux.cs index b9628d8..3751617 100644 --- a/Runtime/Core/Internal/FuncFlux.cs +++ b/Runtime/Core/Internal/FuncFlux.cs @@ -34,36 +34,24 @@ internal sealed class FuncFlux : IFluxReturn /// A dictionary that stores functions with no parameters and a return value of type `TReturn`. /// - internal readonly Dictionary>> dictionary = new Dictionary>>(); - /// - /// A Read Only dictionary wich contains dictionary field - /// - internal readonly IReadOnlyDictionary>> dictionary_read = null; - /// - /// Constructor of FuncFlux - /// - public FuncFlux() - { - dictionary_read = dictionary; - } + internal readonly Dictionary> dictionary = new Dictionary>(); /// /// Subscribes the provided function to the dictionary with the specified key when `condition` is true. /// If `condition` is false and the dictionary contains the specified key, the function is removed from the dictionary. /// void IStore>.Store(in bool condition, TKey key, Func func) { - // if(dictionary_read.ContainsKey(key)) - // { - // if (condition) dictionary[key] += func; - // else dictionary[key] -= func; - // } - // else if (condition) dictionary.Add(key, func); - if(dictionary_read.TryGetValue(key, out var values)) + if(dictionary.TryGetValue(key, out var values)) { - if (condition) values.Add(func); - else values.Remove(func); + if (condition) dictionary[key] += func; + else + { + values -= func; + if (values is null) dictionary.Remove(key); + else dictionary[key] = values; + } } - else if (condition) dictionary.Add(key, new List>(){func}); + else if (condition) dictionary.Add(key, func); } // /// Triggers the function stored in the dictionary with the specified key and returns its return value. @@ -71,14 +59,9 @@ public FuncFlux() /// TReturn IFluxReturn>.Dispatch(TKey key) { - // if(dictionary_read.TryGetValue(key, out var _actions)) return _actions.Invoke(); - if(dictionary_read.TryGetValue(key, out var _actions)) + if(dictionary.TryGetValue(key, out var _actions)) { - for (int i = 0; i < _actions.Count - 1; i++) - { - _actions[i].Invoke(); - } - return _actions[_actions.Count-1].Invoke(); + return _actions.Invoke(); } return default; } diff --git a/Runtime/Core/Internal/FuncFluxParam.cs b/Runtime/Core/Internal/FuncFluxParam.cs index d98a23a..584c996 100644 --- a/Runtime/Core/Internal/FuncFluxParam.cs +++ b/Runtime/Core/Internal/FuncFluxParam.cs @@ -35,36 +35,24 @@ internal sealed class FuncFluxParam : IFluxParamReturn /// A dictionary that stores functions with one parameter of type `TParam` and a return value of type `TReturn`. /// - internal readonly Dictionary>> dictionary = new Dictionary>>(); - /// - /// A Read Only dictionary wich contains dictionary field - /// - internal readonly IReadOnlyDictionary>> dictionary_read = null; - /// - /// Constructor of FuncFlux - /// - public FuncFluxParam() - { - dictionary_read = dictionary; - } + internal readonly Dictionary> dictionary = new Dictionary>(); /// /// Subscribes the provided function to the dictionary with the specified key when `condition` is true. /// If `condition` is false and the dictionary contains the specified key, the function is removed from the dictionary. /// void IStore>.Store(in bool condition, TKey key, Func func) { - // if(dictionary_read.ContainsKey(key)) - // { - // if (condition) dictionary[key] += func; - // else dictionary[key] -= func; - // } - // else if (condition) dictionary.Add(key, func); - if(dictionary_read.TryGetValue(key, out var values)) + if(dictionary.TryGetValue(key, out var values)) { - if (condition) values.Add(func); - else values.Remove(func); + if (condition) dictionary[key] += func; + else + { + values -= func; + if (values is null) dictionary.Remove(key); + else dictionary[key] = values; + } } - else if (condition) dictionary.Add(key, new List>(){func}); + else if (condition) dictionary.Add(key, func); } /// /// Triggers the function stored in the dictionary with the specified key and parameter, and returns its return value. @@ -72,18 +60,9 @@ public FuncFluxParam() /// TReturn IFluxParamReturn>.Dispatch(TKey key, TParam param) { - // if(dictionary_read.TryGetValue(key, out var _actions)) return _actions.Invoke(param); - // if(dictionary_read.TryGetValue(key, out var _actions)) - // { - // for (int i = 0; i < _actions.Count; i++) _actions[i].Invoke(param); - // } - if(dictionary_read.TryGetValue(key, out var _actions)) + if(dictionary.TryGetValue(key, out var _actions)) { - for (int i = 0; i < _actions.Count - 1; i++) - { - _actions[i].Invoke(param); - } - return _actions[_actions.Count-1].Invoke(param); + return _actions.Invoke(param); } return default; } diff --git a/package.json b/package.json index d7e3b09..61d76c7 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "name": "com.kingdox.uniflux", "displayName": "UniFlux", "author": "Xavier Thomas Peter Arpa López ('Kingdox')", - "version": "1.2.1", + "version": "1.2.2", "unity": "2019.1", "description": "Provides Flux flow integration to Unity.", "keywords": [