Update: UserHeldDevices to Repository Monitor

This commit is contained in:
Gary Sharp
2013-05-06 20:28:28 +10:00
parent e213c398db
commit d63b9ebc4e
10 changed files with 73 additions and 53 deletions
-7
View File
@@ -13,13 +13,6 @@ namespace Disco.BI.Extensions
{
public static class JobExtensions
{
public static void BroadcastUpdate(this Job j)
{
if (j.UserId != null)
Interop.SignalRHandlers.UserHeldDevices.UserJobUpdated(j.UserId);
}
public static JobAttachment CreateAttachment(this Job Job, DiscoDataContext dbContext, User CreatorUser, string Filename, string MimeType, string Comments, Stream Content, DocumentTemplate DocumentTemplate = null, byte[] PdfThumbnail = null)
{
if (string.IsNullOrEmpty(MimeType) || MimeType.Equals("unknown/unknown", StringComparison.InvariantCultureIgnoreCase))
@@ -3,19 +3,39 @@ using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.AspNet.SignalR;
using System.Reactive.Linq;
using Disco.Data.Repository.Monitor;
using Disco.Models.Repository;
namespace Disco.BI.Interop.SignalRHandlers
{
public class UserHeldDevices : PersistentConnection
{
private static bool subscribed = false;
private static object subscribeLock = new object();
internal static void UserJobUpdated(string JobUserId)
static UserHeldDevices()
{
var connectionManager = GlobalHost.ConnectionManager;
var connectionContext = connectionManager.GetConnectionContext<UserHeldDevices>();
if (connectionContext != null)
connectionContext.Connection.Broadcast(JobUserId);
if (!subscribed)
lock (subscribeLock)
if (!subscribed)
{
Disco.Data.Repository.Monitor.RepositoryMonitor.StreamAfterCommit.Where(e => e.EntityType == typeof(Job)).Subscribe(UserJobUpdated);
subscribed = true;
}
}
private static void UserJobUpdated(RepositoryMonitorEvent e)
{
Job j = (Job)e.Entity;
if (j.UserId != null)
{
var connectionManager = GlobalHost.ConnectionManager;
var connectionContext = connectionManager.GetConnectionContext<UserHeldDevices>();
if (connectionContext != null)
connectionContext.Connection.Broadcast(j.UserId);
}
}
}
}