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