reactivity

This commit is contained in:
Timerix 2021-10-04 22:31:14 +03:00
parent c7f3deab57
commit c71d2b8356
3 changed files with 19 additions and 16 deletions

View File

@ -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());
}
}

View File

@ -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;
}
}
}