Skip to content

Commit

Permalink
Added ListenIncludeLatest method to MessageBus to receive last sended…
Browse files Browse the repository at this point in the history
… message.
  • Loading branch information
jlaanstra committed Mar 19, 2013
1 parent dc29cfc commit 2746e56
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
13 changes: 13 additions & 0 deletions ReactiveUI/Interfaces.cs
Expand Up @@ -294,6 +294,19 @@ public interface IMessageBus : IEnableLogger
/// <returns></returns> /// <returns></returns>
IObservable<T> Listen<T>(string contract = null); IObservable<T> Listen<T>(string contract = null);


/// <summary>
/// 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.
/// </summary>
/// <typeparam name="T">The type of the message to listen to.</typeparam>
/// <param name="contract">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.</param>
/// <returns>An Observable representing the notifications posted to the
/// message bus.</returns>
IObservable<T> ListenIncludeLatest<T>(string contract = null);

/// <summary> /// <summary>
/// Determines if a particular message Type is registered. /// Determines if a particular message Type is registered.
/// </summary> /// </summary>
Expand Down
19 changes: 18 additions & 1 deletion ReactiveUI/MessageBus.cs
Expand Up @@ -54,6 +54,23 @@ public IObservable<T> Listen<T>(string contract = null)
{ {
this.Log().Info("Listening to {0}:{1}", typeof (T), contract); this.Log().Info("Listening to {0}:{1}", typeof (T), contract);


return SetupSubjectIfNecessary<T>(contract).Skip(1);
}

/// <summary>
/// Listen provides an Observable that will fire whenever a Message is
/// provided for this object via RegisterMessageSource or SendMessage.
/// </summary>
/// <typeparam name="T">The type of the message to listen to.</typeparam>
/// <param name="contract">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.</param>
/// <returns>An Observable representing the notifications posted to the
/// message bus.</returns>
public IObservable<T> ListenIncludeLatest<T>(string contract = null)
{
this.Log().Info("Listening to {0}:{1}", typeof(T), contract);

return SetupSubjectIfNecessary<T>(contract); return SetupSubjectIfNecessary<T>(contract);
} }


Expand Down Expand Up @@ -126,7 +143,7 @@ ISubject<T> SetupSubjectIfNecessary<T>(string contract)
return; return;
} }
ret = new ScheduledSubject<T>(GetScheduler(tuple)); ret = new ScheduledSubject<T>(GetScheduler(tuple), null, new BehaviorSubject<T>(default(T)));
mb[tuple] = new NotAWeakReference(ret); mb[tuple] = new NotAWeakReference(ret);
}); });


Expand Down

0 comments on commit 2746e56

Please sign in to comment.