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,11 @@
|
||||
using System;
|
||||
|
||||
namespace _Project.Develop.Runtime.Utils.ReactiveManagement
|
||||
{
|
||||
public interface IReadOnlyVariable<T>
|
||||
{
|
||||
T Value { get; }
|
||||
|
||||
IDisposable Subscribe(Action<T, T> action);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: abc67e68747846d3aaf1474b04c77c1b
|
||||
timeCreated: 1769096939
|
||||
@@ -0,0 +1,61 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace _Project.Develop.Runtime.Utils.ReactiveManagement
|
||||
{
|
||||
public class ReactiveVariable<T> : IReadOnlyVariable<T> where T : IEquatable<T>
|
||||
{
|
||||
private readonly List<Subscriber<T, T>> _subscribers = new ();
|
||||
private readonly List<Subscriber<T, T>> _toAddList = new ();
|
||||
private readonly List<Subscriber<T, T>> _toRemoveList = new ();
|
||||
|
||||
public ReactiveVariable() => _value = default(T);
|
||||
|
||||
public ReactiveVariable(T value) => _value = value;
|
||||
|
||||
private T _value;
|
||||
|
||||
public T Value
|
||||
{
|
||||
get => _value;
|
||||
set
|
||||
{
|
||||
T oldValue = _value;
|
||||
_value = value;
|
||||
|
||||
if (_value.Equals(oldValue) == false)
|
||||
Invoke(oldValue, _value);
|
||||
}
|
||||
}
|
||||
|
||||
public IDisposable Subscribe(Action<T, T> action)
|
||||
{
|
||||
Subscriber<T, T> subscriber = new (action, RemoveSubscriber);
|
||||
_toAddList.Add(subscriber);
|
||||
|
||||
return subscriber;
|
||||
}
|
||||
|
||||
private void RemoveSubscriber(Subscriber<T, T> subscriber) => _toRemoveList.Add(subscriber);
|
||||
|
||||
private void Invoke(T oldValue, T newValue)
|
||||
{
|
||||
if(_toAddList.Count > 0)
|
||||
{
|
||||
_subscribers.AddRange(_toAddList);
|
||||
_toAddList.Clear();
|
||||
}
|
||||
|
||||
if(_toRemoveList.Count > 0)
|
||||
{
|
||||
foreach (Subscriber<T, T> subscriber in _toRemoveList)
|
||||
_subscribers.Remove(subscriber);
|
||||
|
||||
_toRemoveList.Clear();
|
||||
}
|
||||
|
||||
foreach (Subscriber<T, T> subscriber in _subscribers)
|
||||
subscriber.Invoke(oldValue, newValue);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 97e9e838b860437cabd97a319c2b6598
|
||||
timeCreated: 1769096939
|
||||
Reference in New Issue
Block a user