Commit a62daf02 authored by Ben Lambell's avatar Ben Lambell

Improve messaging API

parent 8c47ac93
......@@ -14,11 +14,11 @@ using Recurse.Common.Serialization;
namespace Recurse.Common.Collections.Weak
{
[Serializable]
public class WeakDictionary<TKey, TValue>
public class SerializableWeakDictionary<TKey, TValue>
{
private readonly IDictionary<Key, TValue> _implementation;
public WeakDictionary()
public SerializableWeakDictionary()
{
_implementation = new SerializableDictionary<Key, TValue>();
}
......
using System;
using System.Collections.Generic;
using Recurse.Common.Utils;
namespace Recurse.Common.Messaging
{
public class ActionReceiver : Equatable, IReceiver
{
private readonly Action _action;
protected override IEnumerable<object> EqualityKeys
{
get { yield return _action; }
}
public ActionReceiver(Action action)
{
_action = action;
}
public void Receive() => _action();
}
public class ActionReceiver<TValue> : Equatable, IReceiver<TValue>
{
private readonly Action<TValue> _action;
protected override IEnumerable<object> EqualityKeys
{
get { yield return _action; }
}
public ActionReceiver(Action<TValue> action)
{
_action = action;
}
public void Receive(TValue value) => _action(value);
}
}
......@@ -9,7 +9,7 @@
namespace Recurse.Common.Messaging
{
public interface IReceiverCollection<in TReceiver>
public interface ITargetCollection<in TReceiver>
{
/// <summary>
/// Adds a weak reference to the given receiver.
......
......@@ -5,7 +5,7 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
using System;
namespace Recurse.Common.Messaging
......
using System;
using System.Runtime.Serialization;
using Recurse.Persist.Serializables;
namespace Recurse.Common.Messaging
{
[Serializable]
public class ActionReceiver : SerializableAction, IReceiver
{
public static IReceiver Create(Action action) => new ActionReceiver(action);
public static IReceiver<T> Create<T>(Action<T> action) => new ActionReceiver<T>(action);
public ActionReceiver(Action action) : base(action)
{
}
public ActionReceiver(SerializationInfo info, StreamingContext context) : base(info, context)
{
}
public void Receive() => Delegate();
}
[Serializable]
public class ActionReceiver<TValue> : SerializableAction<TValue>, IReceiver<TValue>
{
public ActionReceiver(Action<TValue> action) : base(action)
{
}
public ActionReceiver(SerializationInfo info, StreamingContext context) : base(info, context)
{
}
public void Receive(TValue value) => Delegate(value);
}
}
......@@ -8,15 +8,15 @@
namespace Recurse.Common.Messaging
{
public interface ISender : IReceiverCollection<IReceiver>
public interface IReceiverCollection : ITargetCollection<IReceiver>
{
}
public interface ISender<out TValue> : IReceiverCollection<IReceiver<TValue>>
public interface IReceiverCollection<out TValue> : ITargetCollection<IReceiver<TValue>>
{
}
public interface ISender<out TOwner, out TValue> : IReceiverCollection<IReceiver<TOwner, TValue>>
public interface IReceiverCollection<out TOwner, out TValue> : ITargetCollection<IReceiver<TOwner, TValue>>
{
}
}
\ No newline at end of file
......@@ -2,24 +2,24 @@
namespace Recurse.Common.Messaging
{
public static class MessagingExtensions
public static class ReceiverExtensions
{
public static void Add(this IReceiverCollection<IReceiver> receivers, Action action)
public static void Add(this IReceiverCollection receivers, Action action)
=> receivers.Add(new ActionReceiver(action));
public static void AddWeak(this IReceiverCollection<IReceiver> receivers, Action action)
public static void AddWeak(this IReceiverCollection receivers, Action action)
=> receivers.AddWeak(new ActionReceiver(action));
public static void Remove(this IReceiverCollection<IReceiver> receivers, Action action)
public static void Remove(this IReceiverCollection receivers, Action action)
=> receivers.Remove(new ActionReceiver(action));
public static void Add<TValue>(this IReceiverCollection<IReceiver<TValue>> receivers, Action<TValue> action)
public static void Add<TValue>(this IReceiverCollection<TValue> receivers, Action<TValue> action)
=> receivers.Add(new ActionReceiver<TValue>(action));
public static void AddWeak<TValue>(this IReceiverCollection<IReceiver<TValue>> receivers, Action<TValue> action)
public static void AddWeak<TValue>(this IReceiverCollection<TValue> receivers, Action<TValue> action)
=> receivers.AddWeak(new ActionReceiver<TValue>(action));
public static void Remove<TValue>(this IReceiverCollection<IReceiver<TValue>> receivers, Action<TValue> action)
public static void Remove<TValue>(this IReceiverCollection<TValue> receivers, Action<TValue> action)
=> receivers.Remove(new ActionReceiver<TValue>(action));
}
}
using System;
namespace Recurse.Common.Messaging
{
[Serializable]
public class Relay : TargetCollection<IReceiver>, IReceiver, IReceiverCollection
{
public virtual void Receive() => ForEach(receiver => receiver.Receive());
}
[Serializable]
public class Relay<TValue> : TargetCollection<IReceiver<TValue>>, IReceiver<TValue>, IReceiverCollection<TValue>
{
public virtual void Receive(TValue value) => ForEach(receiver => receiver.Receive(value));
}
[Serializable]
public class Relay<TOwner, TValue> : TargetCollection<IReceiver<TOwner, TValue>>, IReceiver<TOwner, TValue>, IReceiverCollection<TOwner, TValue>
{
public virtual void Receive(TOwner owner, TValue value) => ForEach(receiver => receiver.Receive(owner, value));
}
}
using System;
namespace Recurse.Common.Messaging
{
[Serializable]
public class Relay : ReceiverCollection<IReceiver>, IReceiver, ISender
{
public void Receive() => ForEach(receiver => receiver.Receive());
}
[Serializable]
public class Relay<TValue> : ReceiverCollection<IReceiver<TValue>>, IReceiver<TValue>, ISender<TValue>
{
public void Receive(TValue value) => ForEach(receiver => receiver.Receive(value));
}
[Serializable]
public class Relay<TOwner, TValue> : ReceiverCollection<IReceiver<TOwner, TValue>>, IReceiver<TOwner, TValue>, ISender<TOwner, TValue>
{
public void Receive(TOwner owner, TValue value) => ForEach(receiver => receiver.Receive(owner, value));
}
}
......@@ -14,7 +14,7 @@ using System.Linq;
namespace Recurse.Common.Messaging
{
[Serializable]
public class ReceiverCollection<TReceiver> : IReceiverCollection<TReceiver>, IEnumerable<TReceiver>
public class TargetCollection<TReceiver> : ITargetCollection<TReceiver>, IEnumerable<TReceiver>
{
private List<object> _receivers;
......
......@@ -6,8 +6,19 @@ namespace Recurse.Persist.Serializables
[Serializable]
public class SerializableAction : SerializableDelegate<Action>
{
public static SerializableAction Get(Action action)
=> action != null ? new SerializableAction(action) : null;
public static SerializableAction<T1> Get<T1>(Action<T1> action)
=> action != null ? new SerializableAction<T1>(action) : null;
public static SerializableAction<T1, T2> Get<T1, T2>(Action<T1, T2> action)
=> action != null ? new SerializableAction<T1, T2>(action) : null;
new protected Action Delegate => (Action)base.Delegate;
protected override Type[] ArgumentTypes => new Type[0];
public SerializableAction(Action action) : base(action)
{
}
......@@ -16,7 +27,7 @@ namespace Recurse.Persist.Serializables
{
}
protected override Type[] ArgumentTypes => new Type[0];
public void Invoke() => Delegate.Invoke();
}
[Serializable]
......@@ -24,6 +35,8 @@ namespace Recurse.Persist.Serializables
{
new protected Action<T0> Delegate => (Action<T0>)base.Delegate;
protected override Type[] ArgumentTypes => new[] { typeof(T0) };
public SerializableAction(Action<T0> action) : base(action)
{
}
......@@ -32,7 +45,7 @@ namespace Recurse.Persist.Serializables
{
}
protected override Type[] ArgumentTypes => new[] { typeof(T0) };
public void Invoke(T0 t0) => Delegate.Invoke(t0);
}
[Serializable]
......@@ -40,6 +53,8 @@ namespace Recurse.Persist.Serializables
{
new protected Action<T0, T1> Delegate => (Action<T0, T1>)base.Delegate;
protected override Type[] ArgumentTypes => new[] { typeof(T0), typeof(T1) };
public SerializableAction(Action<T0, T1> action) : base(action)
{
}
......@@ -48,6 +63,6 @@ namespace Recurse.Persist.Serializables
{
}
protected override Type[] ArgumentTypes => new[] { typeof(T0), typeof(T1) };
public void Invoke(T0 t0, T1 t1) => Delegate.Invoke(t0, t1);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment