Snapcast groups revamp
This commit is contained in:
@ -8,6 +8,13 @@ export function set(data) {
|
||||
};
|
||||
}
|
||||
|
||||
export function setConnection(data) {
|
||||
return {
|
||||
type: 'MOPIDY_SET_CONNECTION',
|
||||
data,
|
||||
};
|
||||
}
|
||||
|
||||
export function request(method, params = {}, response_callback = null, error_callback = null) {
|
||||
return {
|
||||
type: 'MOPIDY_REQUEST',
|
||||
|
||||
@ -224,7 +224,7 @@ const MopidyMiddleware = (function () {
|
||||
switch (action.type) {
|
||||
case 'MOPIDY_CONNECT':
|
||||
if (socket != null) {
|
||||
socket.close();
|
||||
socket.close();
|
||||
}
|
||||
|
||||
store.dispatch({ type: 'MOPIDY_CONNECTING' });
|
||||
@ -240,10 +240,12 @@ const MopidyMiddleware = (function () {
|
||||
|
||||
case 'MOPIDY_CONNECTED':
|
||||
if (store.getState().ui.allow_reporting) {
|
||||
const hashed_hostname = sha256(window.location.hostname);
|
||||
ReactGA.event({ category: 'Mopidy', action: 'Connected', label: hashed_hostname });
|
||||
}
|
||||
store.dispatch(uiActions.createNotification({ content: 'Mopidy connected' }));
|
||||
ReactGA.event({
|
||||
category: 'Mopidy',
|
||||
action: 'Connected',
|
||||
label: sha256(window.location.hostname),
|
||||
});
|
||||
}
|
||||
next(action);
|
||||
break;
|
||||
|
||||
@ -254,7 +256,6 @@ const MopidyMiddleware = (function () {
|
||||
break;
|
||||
|
||||
case 'MOPIDY_DISCONNECTED':
|
||||
store.dispatch(uiActions.createNotification({ type: 'bad', content: 'Mopidy disconnected' }));
|
||||
helpers.setFavicon('favicon_error.png');
|
||||
break;
|
||||
|
||||
@ -265,6 +266,19 @@ const MopidyMiddleware = (function () {
|
||||
});
|
||||
break;
|
||||
|
||||
case 'MOPIDY_SET_CONNECTION':
|
||||
store.dispatch(mopidyActions.set(action.data));
|
||||
|
||||
// Wait 250 ms and then retry connection
|
||||
setTimeout(
|
||||
() => {
|
||||
store.dispatch(mopidyActions.connect());
|
||||
store.dispatch(pusherActions.connect());
|
||||
},
|
||||
250,
|
||||
);
|
||||
break;
|
||||
|
||||
case 'SET_WINDOW_FOCUS':
|
||||
|
||||
// Focus has just been regained
|
||||
@ -284,21 +298,21 @@ const MopidyMiddleware = (function () {
|
||||
request(socket, store, action.method, action.params)
|
||||
.then(
|
||||
(response) => {
|
||||
if (action.response_callback) {
|
||||
store.dispatch(action.response_callback.call(this, response));
|
||||
}
|
||||
if (action.response_callback) {
|
||||
store.dispatch(action.response_callback.call(this, response));
|
||||
}
|
||||
},
|
||||
(error) => {
|
||||
if (action.error_callback) {
|
||||
store.dispatch(action.error_callback.call(this, error));
|
||||
} else {
|
||||
store.dispatch(coreActions.handleException(
|
||||
'Mopidy request failed',
|
||||
error,
|
||||
action.method,
|
||||
action,
|
||||
));
|
||||
}
|
||||
if (action.error_callback) {
|
||||
store.dispatch(action.error_callback.call(this, error));
|
||||
} else {
|
||||
store.dispatch(coreActions.handleException(
|
||||
'Mopidy request failed',
|
||||
error,
|
||||
action.method,
|
||||
action,
|
||||
));
|
||||
}
|
||||
},
|
||||
);
|
||||
break;
|
||||
|
||||
@ -12,9 +12,12 @@ const spotifyActions = require('../spotify/actions');
|
||||
const snapcastActions = require('../snapcast/actions');
|
||||
|
||||
const PusherMiddleware = (function () {
|
||||
|
||||
// container for the actual websocket
|
||||
let socket = null;
|
||||
|
||||
let reconnectTimer = null;
|
||||
|
||||
// requests pending
|
||||
const deferredRequests = [];
|
||||
|
||||
@ -183,7 +186,7 @@ const PusherMiddleware = (function () {
|
||||
store.dispatch(uiActions.startLoading(id, `pusher_${method}`));
|
||||
|
||||
// Start our 30 second timeout
|
||||
const timeout = setTimeout(
|
||||
setTimeout(
|
||||
() => {
|
||||
store.dispatch(uiActions.stopLoading(id));
|
||||
reject({
|
||||
@ -203,7 +206,7 @@ const PusherMiddleware = (function () {
|
||||
});
|
||||
|
||||
return (store) => (next) => (action) => {
|
||||
const { pusher } = store.getState();
|
||||
const { pusher } = store.getState();
|
||||
|
||||
switch (action.type) {
|
||||
case 'PUSHER_CONNECT':
|
||||
@ -211,12 +214,11 @@ const PusherMiddleware = (function () {
|
||||
socket.close();
|
||||
}
|
||||
|
||||
clearTimeout(reconnectTimer);
|
||||
store.dispatch({ type: 'PUSHER_CONNECTING' });
|
||||
|
||||
var state = store.getState();
|
||||
|
||||
socket = new WebSocket(
|
||||
`ws${window.location.protocol === 'https:' ? 's' : ''}://${state.mopidy.host}:${state.mopidy.port}/iris/ws/`,
|
||||
`ws${window.location.protocol === 'https:' ? 's' : ''}://${store.getState().mopidy.host}:${store.getState().mopidy.port}/iris/ws/`,
|
||||
);
|
||||
|
||||
socket.onopen = () => {
|
||||
@ -231,7 +233,7 @@ const PusherMiddleware = (function () {
|
||||
});
|
||||
|
||||
// attempt to reconnect every 5 seconds
|
||||
setTimeout(() => {
|
||||
reconnectTimer = setTimeout(() => {
|
||||
store.dispatch(pusherActions.connect());
|
||||
}, 5000);
|
||||
};
|
||||
@ -247,17 +249,17 @@ const PusherMiddleware = (function () {
|
||||
};
|
||||
|
||||
socket.onmessage = (message) => {
|
||||
var message = JSON.parse(message.data);
|
||||
handleMessage(socket, store, message);
|
||||
handleMessage(socket, store, JSON.parse(message.data));
|
||||
};
|
||||
|
||||
break;
|
||||
|
||||
case 'PUSHER_CONNECTED':
|
||||
if (store.getState().ui.allow_reporting) {
|
||||
ReactGA.event({ category: 'Pusher', action: 'Connected', label: action.username });
|
||||
}
|
||||
ReactGA.event({ category: 'Pusher', action: 'Connected', label: action.username });
|
||||
}
|
||||
|
||||
clearTimeout(reconnectTimer);
|
||||
store.dispatch(pusherActions.updateConnection());
|
||||
store.dispatch(pusherActions.getConfig());
|
||||
store.dispatch(pusherActions.getRadio());
|
||||
@ -280,21 +282,21 @@ const PusherMiddleware = (function () {
|
||||
request(store, action.method, action.params)
|
||||
.then(
|
||||
(response) => {
|
||||
if (action.response_callback) {
|
||||
action.response_callback.call(this, response);
|
||||
}
|
||||
if (action.response_callback) {
|
||||
action.response_callback.call(this, response);
|
||||
}
|
||||
},
|
||||
(error) => {
|
||||
if (action.error_callback) {
|
||||
action.error_callback.call(this, error);
|
||||
} else {
|
||||
store.dispatch(coreActions.handleException(
|
||||
'Pusher request failed',
|
||||
error,
|
||||
action.method,
|
||||
action,
|
||||
));
|
||||
}
|
||||
if (action.error_callback) {
|
||||
action.error_callback.call(this, error);
|
||||
} else {
|
||||
store.dispatch(coreActions.handleException(
|
||||
'Pusher request failed',
|
||||
error,
|
||||
action.method,
|
||||
action,
|
||||
));
|
||||
}
|
||||
},
|
||||
);
|
||||
break;
|
||||
|
||||
@ -5,6 +5,14 @@ export function set(data) {
|
||||
data,
|
||||
};
|
||||
}
|
||||
|
||||
export function setConnection(data) {
|
||||
return {
|
||||
type: 'SNAPCAST_SET_CONNECTION',
|
||||
data,
|
||||
};
|
||||
}
|
||||
|
||||
export function setEnabled(enabled) {
|
||||
return {
|
||||
type: 'SNAPCAST_SET_ENABLED',
|
||||
|
||||
@ -10,6 +10,7 @@ const snapcastActions = require('./actions');
|
||||
|
||||
const SnapcastMiddleware = (function () {
|
||||
let socket = null;
|
||||
let reconnectTimer = null;
|
||||
|
||||
// requests pending
|
||||
const deferredRequests = [];
|
||||
@ -141,9 +142,8 @@ const SnapcastMiddleware = (function () {
|
||||
switch (action.type) {
|
||||
|
||||
case 'SNAPCAST_CONNECT':
|
||||
if (socket) {
|
||||
socket.close();
|
||||
}
|
||||
if (socket) socket.close();
|
||||
clearTimeout(reconnectTimer);
|
||||
|
||||
store.dispatch({ type: 'SNAPCAST_CONNECTING' });
|
||||
|
||||
@ -164,7 +164,8 @@ const SnapcastMiddleware = (function () {
|
||||
|
||||
// attempt to reconnect every 5 seconds
|
||||
if (store.getState().snapcast.enabled) {
|
||||
setTimeout(() => {
|
||||
clearTimeout(reconnectTimer);
|
||||
reconnectTimer = setTimeout(() => {
|
||||
store.dispatch(snapcastActions.connect());
|
||||
}, 5000);
|
||||
}
|
||||
@ -181,17 +182,18 @@ const SnapcastMiddleware = (function () {
|
||||
};
|
||||
|
||||
socket.onmessage = (message) => {
|
||||
var message = JSON.parse(message.data);
|
||||
handleMessage(socket, store, message);
|
||||
handleMessage(socket, store, JSON.parse(message.data));
|
||||
};
|
||||
break;
|
||||
|
||||
case 'SNAPCAST_CONNECTED':
|
||||
if (store.getState().ui.allow_reporting) {
|
||||
const hashed_hostname = sha256(window.location.hostname);
|
||||
ReactGA.event({ category: 'Snapcast', action: 'Connected', label: hashed_hostname });
|
||||
ReactGA.event({
|
||||
category: 'Snapcast',
|
||||
action: 'Connected',
|
||||
label: sha256(window.location.hostname),
|
||||
});
|
||||
}
|
||||
store.dispatch(uiActions.createNotification({ content: 'Snapcast connected' }));
|
||||
store.dispatch(snapcastActions.getServer());
|
||||
next(action);
|
||||
break;
|
||||
@ -199,11 +201,24 @@ const SnapcastMiddleware = (function () {
|
||||
case 'SNAPCAST_DISCONNECT':
|
||||
if (socket != null) socket.close();
|
||||
socket = null;
|
||||
clearTimeout(reconnectTimer);
|
||||
break;
|
||||
|
||||
case 'SNAPCAST_DISCONNECTED':
|
||||
case 'SNAPCAST_SET_CONNECTION':
|
||||
store.dispatch(snapcastActions.serverLoaded({}));
|
||||
store.dispatch(snapcastActions.clientsLoaded([]));
|
||||
store.dispatch(snapcastActions.groupsLoaded([]));
|
||||
store.dispatch(snapcastActions.streamsLoaded([]));
|
||||
store.dispatch(snapcastActions.set(action.data));
|
||||
|
||||
// Wait 250 ms and then retry connection
|
||||
if (store.getState().snapcast.enabled) {
|
||||
store.dispatch(uiActions.createNotification({ type: 'bad', content: 'Snapcast disconnected' }));
|
||||
setTimeout(
|
||||
() => {
|
||||
store.dispatch(snapcastActions.connect());
|
||||
},
|
||||
250,
|
||||
);
|
||||
}
|
||||
break;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user