2017-09-04 02:20:56 +00:00
|
|
|
import $ from 'jquery';
|
2017-09-21 02:05:00 +00:00
|
|
|
import 'signalr';
|
2017-09-04 02:20:56 +00:00
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
import { Component } from 'react';
|
|
|
|
import { connect } from 'react-redux';
|
|
|
|
import { createSelector } from 'reselect';
|
2017-10-07 06:47:28 +00:00
|
|
|
import { repopulatePage } from 'Utilities/pagePopulator';
|
2018-08-08 00:57:15 +00:00
|
|
|
import titleCase from 'Utilities/String/titleCase';
|
2018-08-31 03:07:50 +00:00
|
|
|
import { fetchCommands, updateCommand, finishCommand } from 'Store/Actions/commandActions';
|
2017-09-04 02:20:56 +00:00
|
|
|
import { setAppValue, setVersion } from 'Store/Actions/appActions';
|
|
|
|
import { update, updateItem, removeItem } from 'Store/Actions/baseActions';
|
|
|
|
import { fetchHealth } from 'Store/Actions/systemActions';
|
|
|
|
import { fetchQueue, fetchQueueDetails } from 'Store/Actions/queueActions';
|
|
|
|
|
|
|
|
function getState(status) {
|
|
|
|
switch (status) {
|
|
|
|
case 0:
|
|
|
|
return 'connecting';
|
|
|
|
case 1:
|
|
|
|
return 'connected';
|
|
|
|
case 2:
|
|
|
|
return 'reconnecting';
|
|
|
|
case 4:
|
|
|
|
return 'disconnected';
|
|
|
|
default:
|
|
|
|
throw new Error(`invalid status ${status}`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-16 02:24:33 +00:00
|
|
|
function isAppDisconnected(disconnectedTime) {
|
|
|
|
if (!disconnectedTime) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Math.floor(new Date().getTime() / 1000) - disconnectedTime > 180;
|
|
|
|
}
|
|
|
|
|
2018-08-08 00:57:15 +00:00
|
|
|
function getHandlerName(name) {
|
|
|
|
name = titleCase(name);
|
|
|
|
name = name.replace('/', '');
|
|
|
|
|
|
|
|
return `handle${name}`;
|
|
|
|
}
|
|
|
|
|
2017-09-04 02:20:56 +00:00
|
|
|
function createMapStateToProps() {
|
|
|
|
return createSelector(
|
|
|
|
(state) => state.app.isReconnecting,
|
2017-10-07 06:47:28 +00:00
|
|
|
(state) => state.app.isDisconnected,
|
2017-09-04 02:20:56 +00:00
|
|
|
(state) => state.queue.paged.isPopulated,
|
2017-10-07 06:47:28 +00:00
|
|
|
(isReconnecting, isDisconnected, isQueuePopulated) => {
|
2017-09-04 02:20:56 +00:00
|
|
|
return {
|
|
|
|
isReconnecting,
|
2017-10-07 06:47:28 +00:00
|
|
|
isDisconnected,
|
2017-09-04 02:20:56 +00:00
|
|
|
isQueuePopulated
|
|
|
|
};
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
const mapDispatchToProps = {
|
2018-08-31 03:07:50 +00:00
|
|
|
dispatchFetchCommands: fetchCommands,
|
|
|
|
dispatchUpdateCommand: updateCommand,
|
|
|
|
dispatchFinishCommand: finishCommand,
|
|
|
|
dispatchSetAppValue: setAppValue,
|
|
|
|
dispatchSetVersion: setVersion,
|
|
|
|
dispatchUpdate: update,
|
|
|
|
dispatchUpdateItem: updateItem,
|
|
|
|
dispatchRemoveItem: removeItem,
|
|
|
|
dispatchFetchHealth: fetchHealth,
|
|
|
|
dispatchFetchQueue: fetchQueue,
|
|
|
|
dispatchFetchQueueDetails: fetchQueueDetails
|
2017-09-04 02:20:56 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class SignalRConnector extends Component {
|
|
|
|
|
|
|
|
//
|
|
|
|
// Lifecycle
|
|
|
|
|
|
|
|
constructor(props, context) {
|
|
|
|
super(props, context);
|
|
|
|
|
2017-09-21 02:05:00 +00:00
|
|
|
this.signalRconnectionOptions = { transport: ['webSockets', 'longPolling'] };
|
2017-09-04 02:20:56 +00:00
|
|
|
this.signalRconnection = null;
|
2017-11-26 20:09:45 +00:00
|
|
|
this.retryInterval = 1;
|
2017-09-04 02:20:56 +00:00
|
|
|
this.retryTimeoutId = null;
|
2017-11-16 02:24:33 +00:00
|
|
|
this.disconnectedTime = null;
|
2017-09-04 02:20:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
componentDidMount() {
|
2017-10-15 06:17:53 +00:00
|
|
|
console.log('Starting signalR');
|
2017-09-04 02:20:56 +00:00
|
|
|
|
2018-01-26 03:01:53 +00:00
|
|
|
this.signalRconnection = $.connection('/signalr', { apiKey: window.Lidarr.apiKey });
|
2017-09-04 02:20:56 +00:00
|
|
|
|
|
|
|
this.signalRconnection.stateChanged(this.onStateChanged);
|
|
|
|
this.signalRconnection.received(this.onReceived);
|
|
|
|
this.signalRconnection.reconnecting(this.onReconnecting);
|
|
|
|
this.signalRconnection.disconnected(this.onDisconnected);
|
|
|
|
|
|
|
|
this.signalRconnection.start(this.signalRconnectionOptions);
|
|
|
|
}
|
|
|
|
|
|
|
|
componentWillUnmount() {
|
2018-08-08 00:57:15 +00:00
|
|
|
if (this.retryTimeoutId) {
|
|
|
|
this.retryTimeoutId = clearTimeout(this.retryTimeoutId);
|
|
|
|
}
|
|
|
|
|
2017-09-04 02:20:56 +00:00
|
|
|
this.signalRconnection.stop();
|
|
|
|
this.signalRconnection = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Control
|
|
|
|
|
|
|
|
retryConnection = () => {
|
2017-11-16 02:24:33 +00:00
|
|
|
if (isAppDisconnected(this.disconnectedTime)) {
|
2017-10-28 20:13:32 +00:00
|
|
|
this.setState({
|
|
|
|
isDisconnected: true
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-09-04 02:20:56 +00:00
|
|
|
this.retryTimeoutId = setTimeout(() => {
|
2018-08-08 00:57:15 +00:00
|
|
|
if (!this.signalRconnection) {
|
|
|
|
console.error('signalR: Connection was disposed');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-09-04 02:20:56 +00:00
|
|
|
this.signalRconnection.start(this.signalRconnectionOptions);
|
2017-11-26 20:09:45 +00:00
|
|
|
this.retryInterval = Math.min(this.retryInterval + 1, 10);
|
2017-09-04 02:20:56 +00:00
|
|
|
}, this.retryInterval * 1000);
|
|
|
|
}
|
|
|
|
|
|
|
|
handleMessage = (message) => {
|
|
|
|
const {
|
|
|
|
name,
|
|
|
|
body
|
|
|
|
} = message;
|
|
|
|
|
2018-08-08 00:57:15 +00:00
|
|
|
const handler = this[getHandlerName(name)];
|
2017-09-04 02:20:56 +00:00
|
|
|
|
2018-08-08 00:57:15 +00:00
|
|
|
if (handler) {
|
|
|
|
handler(body);
|
2017-09-24 05:10:24 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-08-08 00:57:15 +00:00
|
|
|
console.error(`signalR: Unable to find handler for ${name}`);
|
2017-09-04 02:20:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
handleCalendar = (body) => {
|
|
|
|
if (body.action === 'updated') {
|
2018-08-31 03:07:50 +00:00
|
|
|
this.props.dispatchUpdateItem({
|
2017-09-04 02:20:56 +00:00
|
|
|
section: 'calendar',
|
|
|
|
updateOnly: true,
|
2017-10-07 06:47:28 +00:00
|
|
|
...body.resource
|
|
|
|
});
|
2017-09-04 02:20:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
handleCommand = (body) => {
|
2018-08-31 03:07:50 +00:00
|
|
|
if (body.action === 'sync') {
|
|
|
|
this.props.dispatchFetchCommands();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-09-04 02:20:56 +00:00
|
|
|
const resource = body.resource;
|
2018-08-31 03:07:50 +00:00
|
|
|
const status = resource.status;
|
2017-09-04 02:20:56 +00:00
|
|
|
|
2017-09-17 20:39:13 +00:00
|
|
|
// Both sucessful and failed commands need to be
|
|
|
|
// completed, otherwise they spin until they timeout.
|
|
|
|
|
2018-08-31 03:07:50 +00:00
|
|
|
if (status === 'completed' || status === 'failed') {
|
|
|
|
this.props.dispatchFinishCommand(resource);
|
2017-09-04 02:20:56 +00:00
|
|
|
} else {
|
2018-08-31 03:07:50 +00:00
|
|
|
this.props.dispatchUpdateCommand(resource);
|
2017-09-04 02:20:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-01 06:05:24 +00:00
|
|
|
handleAlbum = (body) => {
|
2017-09-04 02:20:56 +00:00
|
|
|
if (body.action === 'updated') {
|
2018-08-31 03:07:50 +00:00
|
|
|
this.props.dispatchUpdateItem({
|
2018-01-01 06:05:24 +00:00
|
|
|
section: 'albums',
|
2017-09-04 02:20:56 +00:00
|
|
|
updateOnly: true,
|
2017-10-07 06:47:28 +00:00
|
|
|
...body.resource
|
|
|
|
});
|
2017-09-04 02:20:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-24 05:10:24 +00:00
|
|
|
handleTrack = (body) => {
|
2018-02-16 04:03:44 +00:00
|
|
|
if (body.action === 'updated') {
|
2018-08-31 03:07:50 +00:00
|
|
|
this.props.dispatchUpdateItem({
|
2018-02-16 04:03:44 +00:00
|
|
|
section: 'tracks',
|
|
|
|
updateOnly: true,
|
2017-10-07 06:47:28 +00:00
|
|
|
...body.resource
|
|
|
|
});
|
2017-09-24 05:10:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-25 02:58:13 +00:00
|
|
|
handleTrackFile = (body) => {
|
2018-01-26 03:01:53 +00:00
|
|
|
const section = 'trackFiles';
|
|
|
|
|
2017-09-04 02:20:56 +00:00
|
|
|
if (body.action === 'updated') {
|
2018-08-31 03:07:50 +00:00
|
|
|
this.props.dispatchUpdateItem({ section, ...body.resource });
|
2018-01-26 03:01:53 +00:00
|
|
|
} else if (body.action === 'deleted') {
|
2018-08-31 03:07:50 +00:00
|
|
|
this.props.dispatchRemoveItem({ section, id: body.resource.id });
|
2017-09-04 02:20:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-08 00:57:15 +00:00
|
|
|
handleHealth = () => {
|
2018-08-31 03:07:50 +00:00
|
|
|
this.props.dispatchFetchHealth();
|
2017-09-04 02:20:56 +00:00
|
|
|
}
|
|
|
|
|
2017-10-07 22:38:31 +00:00
|
|
|
handleArtist = (body) => {
|
2017-09-04 02:20:56 +00:00
|
|
|
const action = body.action;
|
2017-10-07 22:38:31 +00:00
|
|
|
const section = 'artist';
|
2017-09-04 02:20:56 +00:00
|
|
|
|
|
|
|
if (action === 'updated') {
|
2018-08-31 03:07:50 +00:00
|
|
|
this.props.dispatchUpdateItem({ section, ...body.resource });
|
2017-09-04 02:20:56 +00:00
|
|
|
} else if (action === 'deleted') {
|
2018-08-31 03:07:50 +00:00
|
|
|
this.props.dispatchRemoveItem({ section, id: body.resource.id });
|
2017-09-04 02:20:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-08 00:57:15 +00:00
|
|
|
handleQueue = () => {
|
2017-09-04 02:20:56 +00:00
|
|
|
if (this.props.isQueuePopulated) {
|
2018-08-31 03:07:50 +00:00
|
|
|
this.props.dispatchFetchQueue();
|
2017-09-04 02:20:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-08 00:57:15 +00:00
|
|
|
handleQueueDetails = () => {
|
2018-08-31 03:07:50 +00:00
|
|
|
this.props.dispatchFetchQueueDetails();
|
2017-09-04 02:20:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
handleQueueStatus = (body) => {
|
2018-08-31 03:07:50 +00:00
|
|
|
this.props.dispatchUpdate({ section: 'queue.status', data: body.resource });
|
2017-09-04 02:20:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
handleVersion = (body) => {
|
2017-09-21 02:05:00 +00:00
|
|
|
const version = body.Version;
|
2017-09-04 02:20:56 +00:00
|
|
|
|
2018-08-31 03:07:50 +00:00
|
|
|
this.props.dispatchSetVersion({ version });
|
2017-09-04 02:20:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
handleWantedCutoff = (body) => {
|
|
|
|
if (body.action === 'updated') {
|
2018-08-31 03:07:50 +00:00
|
|
|
this.props.dispatchUpdateItem({
|
2017-09-04 02:20:56 +00:00
|
|
|
section: 'cutoffUnmet',
|
|
|
|
updateOnly: true,
|
2017-10-07 06:47:28 +00:00
|
|
|
...body.resource
|
|
|
|
});
|
2017-09-04 02:20:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
handleWantedMissing = (body) => {
|
|
|
|
if (body.action === 'updated') {
|
2018-08-31 03:07:50 +00:00
|
|
|
this.props.dispatchUpdateItem({
|
2017-09-04 02:20:56 +00:00
|
|
|
section: 'missing',
|
|
|
|
updateOnly: true,
|
2017-10-07 06:47:28 +00:00
|
|
|
...body.resource
|
|
|
|
});
|
2017-09-04 02:20:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-08 00:57:15 +00:00
|
|
|
handleSystemTask = () => {
|
|
|
|
// No-op for now, we may want this later
|
|
|
|
}
|
|
|
|
|
2017-09-04 02:20:56 +00:00
|
|
|
//
|
|
|
|
// Listeners
|
|
|
|
|
|
|
|
onStateChanged = (change) => {
|
|
|
|
const state = getState(change.newState);
|
2018-08-08 00:57:15 +00:00
|
|
|
console.log(`signalR: ${state}`);
|
2017-09-04 02:20:56 +00:00
|
|
|
|
|
|
|
if (state === 'connected') {
|
2017-11-16 02:24:33 +00:00
|
|
|
// Clear disconnected time
|
|
|
|
this.disconnectedTime = null;
|
|
|
|
|
2018-08-31 03:07:50 +00:00
|
|
|
const {
|
|
|
|
dispatchFetchCommands,
|
|
|
|
dispatchSetAppValue
|
|
|
|
} = this.props;
|
|
|
|
|
2017-10-07 06:47:28 +00:00
|
|
|
// Repopulate the page (if a repopulator is set) to ensure things
|
|
|
|
// are in sync after reconnecting.
|
|
|
|
|
|
|
|
if (this.props.isReconnecting || this.props.isDisconnected) {
|
2018-08-31 03:07:50 +00:00
|
|
|
dispatchFetchCommands();
|
2017-10-07 06:47:28 +00:00
|
|
|
repopulatePage();
|
|
|
|
}
|
|
|
|
|
2018-08-31 03:07:50 +00:00
|
|
|
dispatchSetAppValue({
|
2017-09-04 02:20:56 +00:00
|
|
|
isConnected: true,
|
|
|
|
isReconnecting: false,
|
2018-01-14 22:11:37 +00:00
|
|
|
isDisconnected: false,
|
|
|
|
isRestarting: false
|
2017-09-04 02:20:56 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
this.retryInterval = 5;
|
|
|
|
|
|
|
|
if (this.retryTimeoutId) {
|
|
|
|
clearTimeout(this.retryTimeoutId);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
onReceived = (message) => {
|
2018-08-08 00:57:15 +00:00
|
|
|
console.debug('signalR: received', message.name, message.body);
|
2017-09-04 02:20:56 +00:00
|
|
|
|
|
|
|
this.handleMessage(message);
|
|
|
|
}
|
|
|
|
|
|
|
|
onReconnecting = () => {
|
2018-01-26 03:01:53 +00:00
|
|
|
if (window.Lidarr.unloading) {
|
2017-09-04 02:20:56 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-11-16 02:24:33 +00:00
|
|
|
if (!this.disconnectedTime) {
|
|
|
|
this.disconnectedTime = Math.floor(new Date().getTime() / 1000);
|
|
|
|
}
|
|
|
|
|
2018-08-31 03:07:50 +00:00
|
|
|
this.props.dispatchSetAppValue({
|
2017-09-04 02:20:56 +00:00
|
|
|
isReconnecting: true
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
onDisconnected = () => {
|
2018-01-26 03:01:53 +00:00
|
|
|
if (window.Lidarr.unloading) {
|
2017-10-02 03:05:28 +00:00
|
|
|
return;
|
2017-09-04 02:20:56 +00:00
|
|
|
}
|
2017-10-02 03:05:28 +00:00
|
|
|
|
2017-11-16 02:24:33 +00:00
|
|
|
if (!this.disconnectedTime) {
|
|
|
|
this.disconnectedTime = Math.floor(new Date().getTime() / 1000);
|
|
|
|
}
|
|
|
|
|
2018-08-31 03:07:50 +00:00
|
|
|
this.props.dispatchSetAppValue({
|
2017-10-02 03:05:28 +00:00
|
|
|
isConnected: false,
|
2017-11-16 02:24:33 +00:00
|
|
|
isReconnecting: true,
|
|
|
|
isDisconnected: isAppDisconnected(this.disconnectedTime)
|
2017-10-02 03:05:28 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
this.retryConnection();
|
2017-09-04 02:20:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Render
|
|
|
|
|
|
|
|
render() {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
SignalRConnector.propTypes = {
|
|
|
|
isReconnecting: PropTypes.bool.isRequired,
|
2017-10-07 06:47:28 +00:00
|
|
|
isDisconnected: PropTypes.bool.isRequired,
|
2017-09-04 02:20:56 +00:00
|
|
|
isQueuePopulated: PropTypes.bool.isRequired,
|
2018-08-31 03:07:50 +00:00
|
|
|
dispatchFetchCommands: PropTypes.func.isRequired,
|
|
|
|
dispatchUpdateCommand: PropTypes.func.isRequired,
|
|
|
|
dispatchFinishCommand: PropTypes.func.isRequired,
|
|
|
|
dispatchSetAppValue: PropTypes.func.isRequired,
|
|
|
|
dispatchSetVersion: PropTypes.func.isRequired,
|
|
|
|
dispatchUpdate: PropTypes.func.isRequired,
|
|
|
|
dispatchUpdateItem: PropTypes.func.isRequired,
|
|
|
|
dispatchRemoveItem: PropTypes.func.isRequired,
|
|
|
|
dispatchFetchHealth: PropTypes.func.isRequired,
|
|
|
|
dispatchFetchQueue: PropTypes.func.isRequired,
|
|
|
|
dispatchFetchQueueDetails: PropTypes.func.isRequired
|
2017-09-04 02:20:56 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export default connect(createMapStateToProps, mapDispatchToProps)(SignalRConnector);
|