reactivity
This commit is contained in:
parent
c7f3deab57
commit
c71d2b8356
@ -7,23 +7,19 @@ namespace DTLib.Reactive
|
||||
{
|
||||
List<T> Storage = new();
|
||||
public event EventHandlerAsync<T> ElementAdded;
|
||||
bool StoreData = false;
|
||||
|
||||
SafeMutex StorageMutex = new();
|
||||
public int Length { get { return StorageMutex.Execute(() => Storage.Count); } }
|
||||
|
||||
public ReactiveStream() { }
|
||||
public ReactiveStream(bool storeData) => StoreData = storeData;
|
||||
|
||||
public void Add(T elem)
|
||||
{
|
||||
if (StoreData) StorageMutex.Execute(() => Storage.Add(elem));
|
||||
StorageMutex.Execute(() => Storage.Add(elem));
|
||||
ElementAdded?.Invoke(this, elem);
|
||||
}
|
||||
|
||||
public void Clear()
|
||||
{
|
||||
if (StoreData) StorageMutex.Execute(() => Storage.Clear());
|
||||
else throw new Exception("Can't clear ReactiveStream because StoreData==false");
|
||||
}
|
||||
public void Get(int index) => StorageMutex.Execute(() => Storage[index]);
|
||||
|
||||
public void Clear() => StorageMutex.Execute(() => Storage.Clear());
|
||||
}
|
||||
}
|
||||
|
||||
19
SafeMutex.cs
19
SafeMutex.cs
@ -14,10 +14,7 @@ namespace DTLib
|
||||
try
|
||||
{
|
||||
exception = null;
|
||||
Mutex.WaitOne();
|
||||
action();
|
||||
Mutex.ReleaseMutex();
|
||||
isReleased = true;
|
||||
Execute(action);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
@ -28,8 +25,18 @@ namespace DTLib
|
||||
|
||||
public void Execute(Action action)
|
||||
{
|
||||
Execute(action, out Exception exception);
|
||||
exception?.Throw();
|
||||
Mutex.WaitOne();
|
||||
action();
|
||||
Mutex.ReleaseMutex();
|
||||
isReleased = true;
|
||||
}
|
||||
public T Execute<T>(Func<T> action)
|
||||
{
|
||||
Mutex.WaitOne();
|
||||
var rezult = action();
|
||||
Mutex.ReleaseMutex();
|
||||
isReleased = true;
|
||||
return rezult;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user