From 2746e568435f84547e558c695da026ddd6d35b6c Mon Sep 17 00:00:00 2001 From: Johan Laanstra Date: Fri, 1 Feb 2013 19:27:18 +0100 Subject: [PATCH] Added ListenIncludeLatest method to MessageBus to receive last sended message. --- ReactiveUI/Interfaces.cs | 13 +++++++++++++ ReactiveUI/MessageBus.cs | 19 ++++++++++++++++++- 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/ReactiveUI/Interfaces.cs b/ReactiveUI/Interfaces.cs index 9b14a98b1d..f63e835d7d 100644 --- a/ReactiveUI/Interfaces.cs +++ b/ReactiveUI/Interfaces.cs @@ -294,6 +294,19 @@ public interface IMessageBus : IEnableLogger /// IObservable Listen(string contract = null); + /// + /// ListenIncludeLatest provides an Observable that will fire whenever a Message is + /// provided for this object via RegisterMessageSource or SendMessage and fire the + /// last provided Message immediately if applicable, or null. + /// + /// The type of the message to listen to. + /// A unique string to distinguish messages with + /// identical types (i.e. "MyCoolViewModel") - if the message type is + /// only used for one purpose, leave this as null. + /// An Observable representing the notifications posted to the + /// message bus. + IObservable ListenIncludeLatest(string contract = null); + /// /// Determines if a particular message Type is registered. /// diff --git a/ReactiveUI/MessageBus.cs b/ReactiveUI/MessageBus.cs index d577b424e1..9400f1e356 100644 --- a/ReactiveUI/MessageBus.cs +++ b/ReactiveUI/MessageBus.cs @@ -54,6 +54,23 @@ public IObservable Listen(string contract = null) { this.Log().Info("Listening to {0}:{1}", typeof (T), contract); + return SetupSubjectIfNecessary(contract).Skip(1); + } + + /// + /// Listen provides an Observable that will fire whenever a Message is + /// provided for this object via RegisterMessageSource or SendMessage. + /// + /// The type of the message to listen to. + /// A unique string to distinguish messages with + /// identical types (i.e. "MyCoolViewModel") - if the message type is + /// only used for one purpose, leave this as null. + /// An Observable representing the notifications posted to the + /// message bus. + public IObservable ListenIncludeLatest(string contract = null) + { + this.Log().Info("Listening to {0}:{1}", typeof(T), contract); + return SetupSubjectIfNecessary(contract); } @@ -126,7 +143,7 @@ ISubject SetupSubjectIfNecessary(string contract) return; } - ret = new ScheduledSubject(GetScheduler(tuple)); + ret = new ScheduledSubject(GetScheduler(tuple), null, new BehaviorSubject(default(T))); mb[tuple] = new NotAWeakReference(ret); });