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.
Prowlarr/NzbDrone.Api/NzbDroneRestModuleWithSigna...

56 lines
1.8 KiB

11 years ago
using NzbDrone.Api.REST;
using NzbDrone.Core.Datastore;
using NzbDrone.Core.Datastore.Events;
using NzbDrone.Core.Messaging;
11 years ago
using NzbDrone.Core.Messaging.Commands;
using NzbDrone.Core.Messaging.Events;
11 years ago
using NzbDrone.SignalR;
namespace NzbDrone.Api
{
public abstract class NzbDroneRestModuleWithSignalR<TResource, TModel> : NzbDroneRestModule<TResource>, IHandle<ModelEvent<TModel>>
where TResource : RestResource, new()
where TModel : ModelBase
{
private readonly ICommandExecutor _commandExecutor;
11 years ago
protected NzbDroneRestModuleWithSignalR(ICommandExecutor commandExecutor)
11 years ago
{
_commandExecutor = commandExecutor;
11 years ago
}
public void Handle(ModelEvent<TModel> message)
{
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)
{
var resource = GetResourceById(id);
var signalRMessage = new SignalRMessage
{
Name = Resource,
Body = new ResourceChangeMessage<TResource>(resource, action)
};
_commandExecutor.PublishCommand(new BroadcastSignalRMessage(signalRMessage));
11 years ago
}
protected void BroadcastResourceChange(ModelAction action)
{
var signalRMessage = new SignalRMessage
{
Name = Resource,
Body = new ResourceChangeMessage<TResource>(action)
};
_commandExecutor.PublishCommand(new BroadcastSignalRMessage(signalRMessage));
11 years ago
}
}
}