Files
Iris/src/js/services/pusher/middleware.js

58 lines
1.7 KiB
JavaScript
Raw Normal View History

2016-10-27 16:58:11 +13:00
var actions = require('./actions.js')
const PusherMiddleware = (function(){
// container for the actual Mopidy socket
var socket = null;
// handle all manner of socket messages
const handleMessage = (ws, store, message) => {
switch( message.action ){
default:
store.dispatch({ type: 'PUSHER_'+message.action.toUpperCase(), data: message.data })
}
}
/**
* Middleware
*
* This behaves like an action interceptor. We listen for specific actions
* and handle special functionality. If the action is not in our switch, then
* it just proceeds to the next middleware, or default functionality
**/
return store => next => action => {
switch(action.type) {
case 'PUSHER_CONNECT':
if(socket != null) socket.close();
store.dispatch({ type: 'PUSHER_CONNECTING' });
var state = store.getState();
socket = new WebSocket(
'ws://'+state.mopidy.host+':'+state.pusher.port+'/pusher'
);
socket.onopen = function(){
store.dispatch({ type: 'PUSHER_CONNECTED' });
};
socket.onmessage = function(message){
var message = JSON.parse(message.data);
handleMessage( socket, store, message )
};
break;
// This action is irrelevant to us, pass it on to the next middleware
default:
return next(action);
}
}
})();
export default PusherMiddleware