Skip to content

Commit b9299a5

Browse files
committed
Added support for TryGetData<T> and HasData to SharedData
1 parent dbdb8c7 commit b9299a5

File tree

3 files changed

+43
-3
lines changed

3 files changed

+43
-3
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# Change Log
22

3+
## [0.8.2-beta] - Sept 09, 2024
4+
- Added TryGetData and HasData methods to the SharedData class
5+
36
## [0.8.1-beta] - Aug 25, 2024
47
- Refactored the CommandState (Available when using the ScriptableCommands package) to better handle exceptions and clean up of cancellation tokens.
58

Runtime/Data/SharedData.cs

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ public interface ISharedData
1010
event Action OnDataCleared;
1111

1212
T GetData<T>(string key);
13+
bool TryGetData<T>(string key, out T value);
14+
bool HasData(string key);
1315
void SetData<T>(string key, T value);
1416
void ClearData();
1517
}
@@ -30,9 +32,9 @@ public T GetData<T>(string key)
3032
{
3133
if (!_data.TryGetValue(key, out var value)) return default;
3234

33-
if (value is T tValue)
35+
if (value is T typedValue)
3436
{
35-
return tValue;
37+
return typedValue;
3638
}
3739

3840
throw new InvalidOperationException($"Attempted to retrieve type {typeof(T)} but data is of type {value.GetType()}");
@@ -42,6 +44,41 @@ public T GetData<T>(string key)
4244
_lock.ExitReadLock();
4345
}
4446
}
47+
48+
public bool TryGetData<T>(string key, out T value)
49+
{
50+
_lock.EnterReadLock();
51+
52+
try
53+
{
54+
if (_data.TryGetValue(key, out var objValue) && objValue is T typedValue)
55+
{
56+
value = typedValue;
57+
return true;
58+
}
59+
60+
value = default;
61+
return false;
62+
}
63+
finally
64+
{
65+
_lock.ExitReadLock();
66+
}
67+
}
68+
69+
public bool HasData(string key)
70+
{
71+
_lock.EnterReadLock();
72+
73+
try
74+
{
75+
return _data.ContainsKey(key);
76+
}
77+
finally
78+
{
79+
_lock.ExitReadLock();
80+
}
81+
}
4582

4683
public void SetData<T>(string key, T value)
4784
{

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "com.nonatomic.visualstatemachinev2",
3-
"version": "0.8.1-beta",
3+
"version": "0.8.2-beta",
44
"displayName": "Visual State Machine V2",
55
"description": "Visual State Machine V2",
66
"unity": "2022.3",

0 commit comments

Comments
 (0)