reactivity

This commit is contained in:
2021-10-03 00:17:31 +03:00
parent ced9f72279
commit a55d71973b
6 changed files with 98 additions and 0 deletions

View File

@@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DTLib.Reactive
{
public class ReactiveListener<T> : ReactiveWorker<T>
{
}
}

View File

@@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DTLib.Reactive
{
public class ReactiveProvider<T> : ReactiveWorker<T>
{
}
}

View File

@@ -0,0 +1,32 @@
using System;
using System.Collections.Generic;
using System.Collections;
using System.Collections.ObjectModel;
using System.Text;
using System.Threading.Tasks;
namespace DTLib.Reactive
{
public class ReactiveStream<T>
{
List<T> Storage = new();
public event EventHandlerAsync<T> ElementAdded;
bool StoreData = false;
SafeMutex StorageAccess = new();
public ReactiveStream() { }
public ReactiveStream(bool storeData) => StoreData = storeData;
public void Add(T elem)
{
if (StoreData) StorageAccess.Execute(() => Storage.Add(elem));
ElementAdded?.Invoke(this, elem);
}
public void Clear()
{
if (StoreData) StorageAccess.Execute(() => Storage.Clear());
}
}
}

View File

@@ -0,0 +1,18 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DTLib.Reactive
{
public abstract class ReactiveWorker<T>
{
List<ReactiveStream<T>> Streams = new();
SafeMutex StreamCollectionAccess = new();
public void Join(ReactiveStream<T> stream) => StreamCollectionAccess.Execute(()=>Streams.Add(stream));
public void Leave(ReactiveStream<T> stream) => StreamCollectionAccess.Execute(() => Streams.Remove(stream));
}
}