You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Lidarr/src/Lidarr.Http/LidarrRestModuleWithSignalR.cs

89 lines
2.7 KiB

11 years ago
using NzbDrone.Core.Datastore;
using NzbDrone.Core.Datastore.Events;
11 years ago
using NzbDrone.Core.Messaging.Events;
11 years ago
using NzbDrone.SignalR;
using Lidarr.Http.REST;
11 years ago
namespace Lidarr.Http
11 years ago
{
public abstract class LidarrRestModuleWithSignalR<TResource, TModel> : LidarrRestModule<TResource>, IHandle<ModelEvent<TModel>>
11 years ago
where TResource : RestResource, new()
where TModel : ModelBase, new()
11 years ago
{
private readonly IBroadcastSignalRMessage _signalRBroadcaster;
11 years ago
protected LidarrRestModuleWithSignalR(IBroadcastSignalRMessage signalRBroadcaster)
11 years ago
{
_signalRBroadcaster = signalRBroadcaster;
11 years ago
}
protected LidarrRestModuleWithSignalR(IBroadcastSignalRMessage signalRBroadcaster, string resource)
11 years ago
: base(resource)
{
_signalRBroadcaster = signalRBroadcaster;
11 years ago
}
11 years ago
public void Handle(ModelEvent<TModel> message)
{
if (!_signalRBroadcaster.IsConnected) return;
11 years ago
if (message.Action == ModelAction.Deleted || message.Action == ModelAction.Sync)
{
BroadcastResourceChange(message.Action);
}
BroadcastResourceChange(message.Action, message.Model.Id);
}
protected void BroadcastResourceChange(ModelAction action, int id)
{
if (!_signalRBroadcaster.IsConnected) return;
if (action == ModelAction.Deleted)
{
BroadcastResourceChange(action, new TResource { Id = id });
}
else
{
var resource = GetResourceById(id);
BroadcastResourceChange(action, resource);
}
}
11 years ago
protected void BroadcastResourceChange(ModelAction action, TResource resource)
{
if (!_signalRBroadcaster.IsConnected) return;
if (GetType().Namespace.Contains("V1"))
11 years ago
{
var signalRMessage = new SignalRMessage
{
Name = Resource,
Body = new ResourceChangeMessage<TResource>(resource, action),
Action = action
};
11 years ago
_signalRBroadcaster.BroadcastMessage(signalRMessage);
}
11 years ago
}
11 years ago
protected void BroadcastResourceChange(ModelAction action)
{
if (!_signalRBroadcaster.IsConnected) return;
if (GetType().Namespace.Contains("V1"))
11 years ago
{
var signalRMessage = new SignalRMessage
{
Name = Resource,
Body = new ResourceChangeMessage<TResource>(action),
Action = action
};
11 years ago
_signalRBroadcaster.BroadcastMessage(signalRMessage);
}
11 years ago
}
}
}