signalr-episodes

pull/4/head
Mark McDowall 11 years ago
parent 6a10382e7b
commit b6693a20a9

@ -1,16 +1,25 @@
using System.Collections.Generic; using System;
using System.Collections.Generic;
using NzbDrone.Api.Mapping; using NzbDrone.Api.Mapping;
using NzbDrone.Api.REST; using NzbDrone.Api.REST;
using NzbDrone.Core.Datastore.Events;
using NzbDrone.Core.Download;
using NzbDrone.Core.MediaFiles.Events;
using NzbDrone.Core.Messaging.Commands;
using NzbDrone.Core.Messaging.Events;
using NzbDrone.Core.Tv; using NzbDrone.Core.Tv;
namespace NzbDrone.Api.Episodes namespace NzbDrone.Api.Episodes
{ {
public class EpisodeModule : NzbDroneRestModule<EpisodeResource> public class EpisodeModule : NzbDroneRestModuleWithSignalR<EpisodeResource, Episode>,
IHandle<EpisodeGrabbedEvent>,
IHandle<EpisodeDownloadedEvent>
{ {
private readonly IEpisodeService _episodeService; private readonly IEpisodeService _episodeService;
public EpisodeModule(IEpisodeService episodeService) public EpisodeModule(ICommandExecutor commandExecutor, IEpisodeService episodeService)
: base("/episodes") : base(commandExecutor, "episodes")
{ {
_episodeService = episodeService; _episodeService = episodeService;
@ -40,5 +49,21 @@ namespace NzbDrone.Api.Episodes
{ {
return _episodeService.GetEpisode(id).InjectTo<EpisodeResource>(); return _episodeService.GetEpisode(id).InjectTo<EpisodeResource>();
} }
public void Handle(EpisodeGrabbedEvent message)
{
foreach (var episode in message.Episode.Episodes)
{
BroadcastResourceChange(ModelAction.Updated, episode.Id);
}
}
public void Handle(EpisodeDownloadedEvent message)
{
foreach (var episode in message.Episode.Episodes)
{
BroadcastResourceChange(ModelAction.Updated, episode.Id);
}
}
} }
} }

@ -18,6 +18,12 @@ namespace NzbDrone.Api
_commandExecutor = commandExecutor; _commandExecutor = commandExecutor;
} }
protected NzbDroneRestModuleWithSignalR(ICommandExecutor commandExecutor, string resource)
: base(resource)
{
_commandExecutor = commandExecutor;
}
public void Handle(ModelEvent<TModel> message) public void Handle(ModelEvent<TModel> message)
{ {
if (message.Action == ModelAction.Deleted || message.Action == ModelAction.Sync) if (message.Action == ModelAction.Deleted || message.Action == ModelAction.Sync)

@ -16,7 +16,7 @@ namespace NzbDrone.Core.Datastore.Events
public enum ModelAction public enum ModelAction
{ {
Unknow = 0, Unknown = 0,
Created = 1, Created = 1,
Updated = 2, Updated = 2,
Deleted = 3, Deleted = 3,

@ -1,8 +1,10 @@
'use strict'; 'use strict';
define( define(
[ [
'underscore',
'backbone',
'signalR' 'signalR'
], function () { ], function (_, Backbone) {
_.extend(Backbone.Collection.prototype, { _.extend(Backbone.Collection.prototype, {
bindSignalR: function () { bindSignalR: function () {
@ -13,14 +15,14 @@ define(
var model = new collection.model(options.resource, {parse: true}); var model = new collection.model(options.resource, {parse: true});
collection.add(model, {merge: true}); collection.add(model, {merge: true});
console.log(options.action + ": %O", options.resource); console.log(options.action + ': {0}}'.format(options.resource));
}; };
require( require(
[ [
'app' 'app'
], function (app) { ], function (app) {
collection.listenTo(app.vent, 'server:' + collection.url.replace('/api/', ''), processMessage) collection.listenTo(app.vent, 'server:' + collection.url.replace('/api/', ''), processMessage);
}); });
return this; return this;
@ -28,7 +30,6 @@ define(
unbindSignalR: function () { unbindSignalR: function () {
}}); }});
}); });

@ -171,7 +171,7 @@ define(
this.seasons.show(new LoadingView()); this.seasons.show(new LoadingView());
this.seasonCollection = new SeasonCollection(this.model.get('seasons')); this.seasonCollection = new SeasonCollection(this.model.get('seasons'));
this.episodeCollection = new EpisodeCollection({ seriesId: this.model.id }); this.episodeCollection = new EpisodeCollection({ seriesId: this.model.id }).bindSignalR();
this.episodeFileCollection = new EpisodeFileCollection({ seriesId: this.model.id }); this.episodeFileCollection = new EpisodeFileCollection({ seriesId: this.model.id });
$.when(this.episodeCollection.fetch(), this.episodeFileCollection.fetch()).done(function () { $.when(this.episodeCollection.fetch(), this.episodeFileCollection.fetch()).done(function () {

@ -24,7 +24,7 @@ define(
} }
}; };
this.signalRconnection = $.connection("/signalr"); this.signalRconnection = $.connection('/signalr');
this.signalRconnection.stateChanged(function (change) { this.signalRconnection.stateChanged(function (change) {
console.debug('SignalR: [{0}]'.format(getStatus(change.newState))); console.debug('SignalR: [{0}]'.format(getStatus(change.newState)));
@ -36,7 +36,7 @@ define(
'app' 'app'
], function (app) { ], function (app) {
app.vent.trigger('server:' + message.name, message.body); app.vent.trigger('server:' + message.name, message.body);
}) });
}); });
this.signalRconnection.start({ transport: this.signalRconnection.start({ transport:

Loading…
Cancel
Save