using System;
namespace Unosquare.Swan.Components {
///
/// Base class for messages that provides weak reference storage of the sender.
///
public abstract class MessageHubMessageBase
: IMessageHubMessage {
///
/// Store a WeakReference to the sender just in case anyone is daft enough to
/// keep the message around and prevent the sender from being collected.
///
private readonly WeakReference _sender;
///
/// Initializes a new instance of the class.
///
/// The sender.
/// sender.
protected MessageHubMessageBase(Object sender) {
if(sender == null) {
throw new ArgumentNullException(nameof(sender));
}
this._sender = new WeakReference(sender);
}
///
/// The sender of the message, or null if not supported by the message implementation.
///
public Object Sender => this._sender?.Target;
}
///
/// Generic message with user specified content.
///
/// Content type to store.
public class MessageHubGenericMessage
: MessageHubMessageBase {
///
/// Initializes a new instance of the class.
///
/// The sender.
/// The content.
public MessageHubGenericMessage(Object sender, TContent content)
: base(sender) => this.Content = content;
///
/// Contents of the message.
///
public TContent Content {
get; protected set;
}
}
}