Skip to content

Instantly share code, notes, and snippets.

@glebov21
Created February 2, 2018 08:36
Show Gist options
  • Save glebov21/aed15e7cdde0bc2dd4d0a81c6b0dac33 to your computer and use it in GitHub Desktop.
Save glebov21/aed15e7cdde0bc2dd4d0a81c6b0dac33 to your computer and use it in GitHub Desktop.
EventsAggregator
using Autogramma.CombApp;
using Autogramma.CombApp.Events;
using System;
using System.Collections.Generic;
public class EventsAggregator
{
public abstract class AbstractEventAggregator<TAction>
{
private List<WeakReference> _listeners = new List<WeakReference>();
public void Listen(TAction action)
{
if (!Contains(action))
_listeners.Add(new WeakReference(action));
}
public void Unlisten(TAction action)
{
var actionInListeners = Find(action);
if (actionInListeners != null)
_listeners.Remove(actionInListeners);
}
private bool Contains(TAction action)
{
return Find(action) != null;
}
private WeakReference Find(TAction action)
{
return _listeners.Find(wr => object.Equals(wr.Target, action));
}
protected IEnumerable<TAction> GetAllAndRemoveNotAlivedSubscribers()
{
List<WeakReference> notAlived = null;
foreach(var listener in _listeners)
{
if (listener.IsAlive)
yield return ((TAction)listener.Target);
else
{
if(notAlived == null)
notAlived = new List<WeakReference>();
notAlived.Add(listener);
}
}
if (notAlived != null)
notAlived.ForEach(x => _listeners.Remove(x));
}
}
public class TestEventAggregator : AbstractEventAggregator<Action>
{
public void Fire()
{
foreach (var subscriber in GetAllAndRemoveNotAlivedSubscribers())
subscriber();
}
}
public class TestEventAggregator<TParam1> : AbstractEventAggregator<Action<TParam1>>
{
public void Fire(TParam1 value1)
{
foreach (var subscriber in GetAllAndRemoveNotAlivedSubscribers())
subscriber(value1);
}
}
public class TestEventAggregator<TParam1, TParam2> : AbstractEventAggregator<Action<TParam1, TParam2>>
{
public void Fire(TParam1 value1, TParam2 value2)
{
foreach (var subscriber in GetAllAndRemoveNotAlivedSubscribers())
subscriber(value1, value2);
}
}
public class TestEventAggregator<TParam1, TParam2, TParam3> : AbstractEventAggregator<Action<TParam1, TParam2, TParam3>>
{
public void Fire(TParam1 value1, TParam2 value2, TParam3 value3)
{
foreach (var subscriber in GetAllAndRemoveNotAlivedSubscribers())
subscriber(value1, value2, value3);
}
}
public void Initialize()
{
}
public static TestEventAggregator<ColorPalette> ChangeColorPaletteSignal = new TestEventAggregator<ColorPalette>();
public static TestEventAggregator<TMPro.TMP_FontAsset> ChangeFontSignal = new TestEventAggregator<TMPro.TMP_FontAsset>();
public static TestEventAggregator<Localization> ChangeLocalizationSignal = new TestEventAggregator<Localization>();
public static TestEventAggregator LeftBtnDownSignal = new TestEventAggregator();
public static TestEventAggregator RightBtnDownSignal = new TestEventAggregator();
public static TestEventAggregator UpBtnDownSignal = new TestEventAggregator();
public static TestEventAggregator DownBtnDownSignal = new TestEventAggregator();
public static TestEventAggregator EnterBtnDownSignal = new TestEventAggregator();
public static TestEventAggregator<bool> Cleaning3DChangedSignal = new TestEventAggregator<bool>();
public static TestEventAggregator<bool> WetSensorChangedSignal = new TestEventAggregator<bool>();
public static TestEventAggregator<int> CropChangedSignal = new TestEventAggregator<int>();
public static TestEventAggregator<int> EngineModelChangedSignal = new TestEventAggregator<int>();
public static TestEventAggregator ControlPanelWasHiddenSignal = new TestEventAggregator();
public static TestEventAggregator<float> HarvesterAdapterLenChangeSignal = new TestEventAggregator<float>();
public static TestEventAggregator<float> HarvesterAdapterMaxLenChangeSignal = new TestEventAggregator<float>();
public static TestEventAggregator<bool> ThreshSenChangedSignal = new TestEventAggregator<bool>();
public static TestEventAggregator CurrentDateStrChangedSignal = new TestEventAggregator();
public static TestEventAggregator<DateTime> CurrentDateChangedSignal = new TestEventAggregator<DateTime>();
public static TestEventAggregator<Events.Event> AddEventSignal = new TestEventAggregator<Events.Event>();
public static TestEventAggregator<Events.Event> DebugEventSignal = new TestEventAggregator<Events.Event>();
public static TestEventAggregator<DateTime> TimeChangedSignal = new TestEventAggregator<DateTime>();
public static TestEventAggregator SetNewDateTimeSignal = new TestEventAggregator();
public static TestEventAggregator<string> ReceiveNewMessageSignal = new TestEventAggregator<string>();
public static TestEventAggregator<bool> HaveNewMessageStateChangedSignal = new TestEventAggregator<bool>();
/// <summary>
/// Эвент, который вызывается при закрытии приложения в редакторе (в Android не срабатывает)
/// </summary>
public static TestEventAggregator OnEditorApplicationQuitSignal = new TestEventAggregator();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment