-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
ActionFluxParam.cs
72 lines (69 loc) · 3.21 KB
/
ActionFluxParam.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/*
Copyright (c) 2023 Xavier Arpa López Thomas Peter ('Kingdox')
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
using System;
using System.Collections.Generic;
namespace Kingdox.UniFlux.Core.Internal
{
///<summary>
/// This class represents an implementation of an IFlux interface with a TKey key and an action without parameters.
///</summary>
internal sealed class ActionFluxParam<TKey, TValue> : IFluxParam<TKey, TValue, Action<TValue>>
{
/// <summary>
/// A dictionary that stores functions with parameters
/// </summary>
internal readonly Dictionary<TKey, HashSet<Action<TValue>>> dictionary = new Dictionary<TKey, HashSet<Action<TValue>>>();
/// <summary>
/// A Read Only dictionary wich contains dictionary field
/// </summary>
internal readonly IReadOnlyDictionary<TKey, HashSet<Action<TValue>>> dictionary_read = null;
/// <summary>
/// Constructor of ActionFluxParam
/// </summary>
public ActionFluxParam()
{
dictionary_read = dictionary;
}
///<summary>
/// Subscribes an event to the action dictionary if the given condition is met
///</summary>
///<param name="condition">Condition that must be true to subscribe the event</param>
///<param name="key">Key of the event to subscribe</param>
///<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.TryGetValue(key, out var values))
{
if (condition) values.Add(action);
else values.Remove(action);
}
else if (condition) dictionary.Add(key, new HashSet<Action<TValue>>(){action});
}
///<summary>
/// Triggers the function stored in the dictionary with the specified key and set the parameter as argument
///</summary>
void IFluxParam<TKey, TValue, Action<TValue>>.Dispatch(TKey key, TValue param)
{
if(dictionary_read.TryGetValue(key, out var _actions))
{
foreach (var item in _actions) item.Invoke(param);
}
}
}
}