mirror of
https://github.com/Bragin-Stepan/project-entity.git
synced 2026-03-05 07:41:10 +00:00
init: add project
This commit is contained in:
@@ -0,0 +1,14 @@
|
||||
using System;
|
||||
|
||||
namespace _Project.Develop.Runtime.Utils.ReactiveManagement.Event
|
||||
{
|
||||
public interface IReadOnlyEvent
|
||||
{
|
||||
IDisposable Subscribe(Action action);
|
||||
}
|
||||
|
||||
public interface IReadOnlyEvent<T>
|
||||
{
|
||||
IDisposable Subscribe(Action<T> action);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 39a931a0bd494206bd9b8b1b4003a87e
|
||||
timeCreated: 1770231417
|
||||
@@ -0,0 +1,77 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace _Project.Develop.Runtime.Utils.ReactiveManagement.Event
|
||||
{
|
||||
public class ReactiveEvent<T> : IReadOnlyEvent<T>
|
||||
{
|
||||
private readonly List<Subscriber<T>> _subscribers = new();
|
||||
private readonly List<Subscriber<T>> _toAdd = new();
|
||||
private readonly List<Subscriber<T>> _toRemove = new();
|
||||
|
||||
public IDisposable Subscribe(Action<T> action)
|
||||
{
|
||||
Subscriber<T> subscriber = new Subscriber<T>(action, Remove);
|
||||
_toAdd.Add(subscriber);
|
||||
return subscriber;
|
||||
}
|
||||
|
||||
private void Remove(Subscriber<T> subscriber) => _toRemove.Add(subscriber);
|
||||
|
||||
public void Invoke(T arg)
|
||||
{
|
||||
if (_toAdd.Count > 0)
|
||||
{
|
||||
_subscribers.AddRange(_toAdd);
|
||||
_toAdd.Clear();
|
||||
}
|
||||
|
||||
if (_toRemove.Count > 0)
|
||||
{
|
||||
foreach (Subscriber<T> subscriber in _toRemove)
|
||||
_subscribers.Remove(subscriber);
|
||||
|
||||
_toRemove.Clear();
|
||||
}
|
||||
|
||||
foreach (Subscriber<T> subscriber in _subscribers)
|
||||
subscriber.Invoke(arg);
|
||||
}
|
||||
}
|
||||
|
||||
public class ReactiveEvent : IReadOnlyEvent
|
||||
{
|
||||
private readonly List<Subscriber> _subscribers = new();
|
||||
private readonly List<Subscriber> _toAdd = new();
|
||||
private readonly List<Subscriber> _toRemove = new();
|
||||
|
||||
public IDisposable Subscribe(Action action)
|
||||
{
|
||||
Subscriber subscriber = new Subscriber(action, Remove);
|
||||
_toAdd.Add(subscriber);
|
||||
return subscriber;
|
||||
}
|
||||
|
||||
private void Remove(Subscriber subscriber) => _toRemove.Add(subscriber);
|
||||
|
||||
public void Invoke()
|
||||
{
|
||||
if (_toAdd.Count > 0)
|
||||
{
|
||||
_subscribers.AddRange(_toAdd);
|
||||
_toAdd.Clear();
|
||||
}
|
||||
|
||||
if (_toRemove.Count > 0)
|
||||
{
|
||||
foreach (Subscriber subscriber in _toRemove)
|
||||
_subscribers.Remove(subscriber);
|
||||
|
||||
_toRemove.Clear();
|
||||
}
|
||||
|
||||
foreach (Subscriber subscriber in _subscribers)
|
||||
subscriber.Invoke();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 3e72d59ed75643f287ae8c5fe58ce025
|
||||
timeCreated: 1770231387
|
||||
Reference in New Issue
Block a user