Skip to content

Commit

Permalink
-
Browse files Browse the repository at this point in the history
  • Loading branch information
xavierarpa committed Apr 20, 2023
1 parent 45806cc commit e1a3a82
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 105 deletions.
8 changes: 8 additions & 0 deletions 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
Expand Down
33 changes: 2 additions & 31 deletions Runtime/Core/Internal/ActionFlux.cs
Expand Up @@ -34,18 +34,6 @@ internal sealed class ActionFlux<TKey> : IFlux<TKey, Action>
// internal Dictionary<TKey, Action> dictionary = new Dictionary<TKey, Action>();
// internal Dictionary<TKey, List<Action>> dictionary = new Dictionary<TKey, List<Action>>();
internal Dictionary<TKey, HashSet<Action>> dictionary = new Dictionary<TKey, HashSet<Action>>();
/// <summary>
/// A Read Only dictionary wich contains dictionary field
/// </summary>
// internal readonly IReadOnlyDictionary<TKey, Action> dictionary_read = null;
internal readonly IReadOnlyDictionary<TKey, HashSet<Action>> dictionary_read = null;
/// <summary>
/// Constructor of ActionFLux
/// </summary>
public ActionFlux()
{
dictionary_read = dictionary;
}
///<summary>
/// Subscribes an event to the action dictionary if the given condition is met
///</summary>
Expand All @@ -54,36 +42,19 @@ public ActionFlux()
///<param name="action">Action to execute when the event is triggered</param>
void IStore<TKey, Action>.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>(){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>(){action});
// if(dictionary_read.ContainsKey(key))
// {
// if (condition) dictionary[key] += action;
// else dictionary[key] -= action;
// }
// else if (condition) dictionary.Add(key, action);
}
///<summary>
/// Triggers the function stored in the dictionary with the specified key.
///</summary>
void IFlux<TKey, Action>.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();
}
Expand Down
11 changes: 0 additions & 11 deletions Runtime/Core/Internal/ActionFluxParam.cs
Expand Up @@ -51,12 +51,6 @@ public ActionFluxParam()
///<param name="action">Action to execute when the event is triggered</param>
void IStore<TKey, Action<TValue>>.Store(in bool condition, TKey key, Action<TValue> 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);
Expand All @@ -69,11 +63,6 @@ public ActionFluxParam()
///</summary>
void IFluxParam<TKey, TValue, Action<TValue>>.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);
Expand Down
41 changes: 12 additions & 29 deletions Runtime/Core/Internal/FuncFlux.cs
Expand Up @@ -34,51 +34,34 @@ internal sealed class FuncFlux<TKey, TReturn> : IFluxReturn<TKey, TReturn, Func<
/// <summary>
/// A dictionary that stores functions with no parameters and a return value of type `TReturn`.
/// </summary>
internal readonly Dictionary<TKey, List<Func<TReturn>>> dictionary = new Dictionary<TKey, List<Func<TReturn>>>();
/// <summary>
/// A Read Only dictionary wich contains dictionary field
/// </summary>
internal readonly IReadOnlyDictionary<TKey, List<Func<TReturn>>> dictionary_read = null;
/// <summary>
/// Constructor of FuncFlux
/// </summary>
public FuncFlux()
{
dictionary_read = dictionary;
}
internal readonly Dictionary<TKey, Func<TReturn>> dictionary = new Dictionary<TKey, Func<TReturn>>();
/// <summary>
/// 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.
/// </summary>
void IStore<TKey, Func<TReturn>>.Store(in bool condition, TKey key, Func<TReturn> 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<TReturn>>(){func});
else if (condition) dictionary.Add(key, func);
}
// <summary>
/// Triggers the function stored in the dictionary with the specified key and returns its return value.
/// If the dictionary does not contain the specified key, returns the default value of type `TReturn`.
/// </summary>
TReturn IFluxReturn<TKey, TReturn, Func<TReturn>>.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;
}
Expand Down
45 changes: 12 additions & 33 deletions Runtime/Core/Internal/FuncFluxParam.cs
Expand Up @@ -35,55 +35,34 @@ internal sealed class FuncFluxParam<TKey, TParam, TReturn> : IFluxParamReturn<TK
/// <summary>
/// A dictionary that stores functions with one parameter of type `TParam` and a return value of type `TReturn`.
/// </summary>
internal readonly Dictionary<TKey, List<Func<TParam, TReturn>>> dictionary = new Dictionary<TKey, List<Func<TParam, TReturn>>>();
/// <summary>
/// A Read Only dictionary wich contains dictionary field
/// </summary>
internal readonly IReadOnlyDictionary<TKey, List<Func<TParam, TReturn>>> dictionary_read = null;
/// <summary>
/// Constructor of FuncFlux
/// </summary>
public FuncFluxParam()
{
dictionary_read = dictionary;
}
internal readonly Dictionary<TKey, Func<TParam, TReturn>> dictionary = new Dictionary<TKey, Func<TParam, TReturn>>();
/// <summary>
/// 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.
/// </summary>
void IStore<TKey, Func<TParam, TReturn>>.Store(in bool condition, TKey key, Func<TParam, TReturn> 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<TParam, TReturn>>(){func});
else if (condition) dictionary.Add(key, func);
}
/// <summary>
/// Triggers the function stored in the dictionary with the specified key and parameter, and returns its return value.
/// If the dictionary does not contain the specified key, returns the default value of type `TReturn`.
/// </summary>
TReturn IFluxParamReturn<TKey, TParam, TReturn, Func<TParam, TReturn>>.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;
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -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": [
Expand Down

0 comments on commit e1a3a82

Please sign in to comment.