2013-09-11 09:33:47 +03:00
|
|
|
using NzbDrone.Api.REST;
|
|
|
|
using NzbDrone.Core.Datastore;
|
|
|
|
using NzbDrone.Core.Datastore.Events;
|
2013-09-14 09:42:09 +03:00
|
|
|
using NzbDrone.Core.Messaging.Commands;
|
|
|
|
using NzbDrone.Core.Messaging.Events;
|
2013-09-11 09:33:47 +03:00
|
|
|
using NzbDrone.SignalR;
|
|
|
|
|
|
|
|
namespace NzbDrone.Api
|
|
|
|
{
|
|
|
|
public abstract class NzbDroneRestModuleWithSignalR<TResource, TModel> : NzbDroneRestModule<TResource>, IHandle<ModelEvent<TModel>>
|
|
|
|
where TResource : RestResource, new()
|
|
|
|
where TModel : ModelBase
|
|
|
|
{
|
2013-09-14 09:36:07 +03:00
|
|
|
private readonly ICommandExecutor _commandExecutor;
|
2013-09-11 09:33:47 +03:00
|
|
|
|
2013-09-14 09:36:07 +03:00
|
|
|
protected NzbDroneRestModuleWithSignalR(ICommandExecutor commandExecutor)
|
2013-09-11 09:33:47 +03:00
|
|
|
{
|
2013-09-14 09:36:07 +03:00
|
|
|
_commandExecutor = commandExecutor;
|
2013-09-11 09:33:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
};
|
|
|
|
|
2013-09-14 09:36:07 +03:00
|
|
|
_commandExecutor.PublishCommand(new BroadcastSignalRMessage(signalRMessage));
|
2013-09-11 09:33:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
protected void BroadcastResourceChange(ModelAction action)
|
|
|
|
{
|
|
|
|
var signalRMessage = new SignalRMessage
|
|
|
|
{
|
|
|
|
Name = Resource,
|
|
|
|
Body = new ResourceChangeMessage<TResource>(action)
|
|
|
|
};
|
|
|
|
|
2013-09-14 09:36:07 +03:00
|
|
|
_commandExecutor.PublishCommand(new BroadcastSignalRMessage(signalRMessage));
|
2013-09-11 09:33:47 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|