Skip to content

Instantly share code, notes, and snippets.

@cburgdorf
Created March 2, 2012 22:31
Show Gist options
  • Save cburgdorf/1961910 to your computer and use it in GitHub Desktop.
Save cburgdorf/1961910 to your computer and use it in GitHub Desktop.
generic brain melt
public static class SignalRRxExtensions
{
public static void PublishOnClient<THub, T>(this IObservable<T> observable, Expression<Func<THub, dynamic>> expression) where THub : Hub, new ()
{
var connectionManager = AspNetHost.DependencyResolver.Resolve<IConnectionManager>();
dynamic clients = connectionManager.GetClients<THub>();
var memberExpression = expression.Body as MemberExpression;
if (memberExpression == null)
{
throw new ArgumentException("'expression' should be a member expression");
}
observable.Subscribe(x => clients.Invoke(memberExpression.Member.Name.CamelCase() + "OnNext", x));
}
static string CamelCase(this string value)
{
if (value == null)
{
throw new ArgumentNullException("value");
}
return String.Join(".", value.Split('.').Select(n => Char.ToLower(n[0]) + n.Substring(1)));
}
}
Usage of the extension method goes like that:
Observable.Interval(TimeSpan.FromSeconds(2)).PublishOnClient<TicketHub, long>(x => x.SomeValue);
I would rather have it without the second type parameter like that:
Observable.Interval(TimeSpan.FromSeconds(2)).PublishOnClient<TicketHub>(x => x.SomeValue);
But how could that be when I want the extension method to apply on Observable<T>?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment