From 390d5c832423075e3c72c49a05bc5f9dd126ba19 Mon Sep 17 00:00:00 2001 From: Paul Stamp Date: Wed, 11 Sep 2024 14:17:36 +0100 Subject: [PATCH] - SharedData.ClearData is now Obsolete and will be removed in a future release. Use SharedData.ClearAllData instead. - Added SharedData.ClearAllData method to clear all shared data. - Added SharedData.RemoveData(string key) method to remove a specific key from the shared data. - Added SharedData.GetKeys() method to return all keys in the shared data. --- CHANGELOG.md | 6 ++++++ Runtime/Data/SharedData.cs | 44 +++++++++++++++++++++++++++++++++++++- package.json | 2 +- 3 files changed, 50 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index de7d768..6102b7e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ # Change Log +## [0.8.3-beta] - Sept 11, 2024 +- SharedData.ClearData is now Obsolete and will be removed in a future release. Use SharedData.ClearAllData instead. +- Added SharedData.ClearAllData method to clear all shared data. +- Added SharedData.RemoveData(string key) method to remove a specific key from the shared data. +- Added SharedData.GetKeys() method to return all keys in the shared data. + ## [0.8.2-beta] - Sept 09, 2024 - Added TryGetData and HasData methods to the SharedData class diff --git a/Runtime/Data/SharedData.cs b/Runtime/Data/SharedData.cs index 18ed578..67af9c0 100644 --- a/Runtime/Data/SharedData.cs +++ b/Runtime/Data/SharedData.cs @@ -8,11 +8,17 @@ public interface ISharedData { event Action OnDataChanged; event Action OnDataCleared; + event Action OnDataRemoved; T GetData(string key); bool TryGetData(string key, out T value); bool HasData(string key); void SetData(string key, T value); + void RemoveData(string key); + void ClearAllData(); + IEnumerable GetKeys(); + + [Obsolete("Use ClearAllData() instead. This method will be removed in a future version.")] void ClearData(); } @@ -20,6 +26,7 @@ public class SharedData : ISharedData { public event Action OnDataChanged; public event Action OnDataCleared; + public event Action OnDataRemoved; private readonly Dictionary _data = new Dictionary(); private readonly ReaderWriterLockSlim _lock = new ReaderWriterLockSlim(); @@ -95,7 +102,23 @@ public void SetData(string key, T value) } } - public void ClearData() + public void RemoveData(string key) + { + _lock.EnterWriteLock(); + try + { + if (_data.Remove(key)) + { + OnDataRemoved?.Invoke(key); + } + } + finally + { + _lock.ExitWriteLock(); + } + } + + public void ClearAllData() { _lock.EnterWriteLock(); try @@ -108,5 +131,24 @@ public void ClearData() _lock.ExitWriteLock(); } } + + [Obsolete("Use ClearAllData() instead. This method will be removed in a future version.")] + public void ClearData() + { + ClearAllData(); + } + + public IEnumerable GetKeys() + { + _lock.EnterReadLock(); + try + { + return _data.Keys; + } + finally + { + _lock.ExitReadLock(); + } + } } } \ No newline at end of file diff --git a/package.json b/package.json index 4a39617..02abfc8 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "com.nonatomic.visualstatemachinev2", - "version": "0.8.2-beta", + "version": "0.8.3-beta", "displayName": "Visual State Machine V2", "description": "Visual State Machine V2", "unity": "2022.3",