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

257 lines
7.8 KiB
JavaScript
Raw Normal View History

2017-11-23 17:43:29 +13:00
var helpers = require('../../helpers.js')
const localstorageMiddleware = (function(){
/**
* The actual middleware inteceptor
**/
return store => next => action => {
// proceed as normal first
// this way, any reducers and middleware do their thing BEFORE we store our new state
next(action);
// append our state to a global variable. This gives us access to debug the store at any point
window._store = store
2017-01-16 18:31:06 +13:00
// if debug enabled
if (store.getState().ui.log_actions){
var ignored_actions = [
'START_LOADING',
'STOP_LOADING'
]
// Show non-ignored actions
if (!ignored_actions.includes(action.type)){
console.log(action)
}
}
2017-10-10 13:05:28 +13:00
switch(action.type){
2016-10-31 09:25:15 +13:00
case 'PUSHER_CONNECTED':
2017-11-23 17:43:29 +13:00
helpers.setStorage(
'pusher',
2017-11-23 17:43:29 +13:00
{
2017-02-19 21:27:43 +13:00
connection_id: action.connection_id
2016-10-28 15:58:07 +13:00
}
);
break;
2016-10-31 09:25:15 +13:00
case 'PUSHER_SET_PORT':
2017-11-23 17:43:29 +13:00
helpers.setStorage(
'pusher',
2017-11-23 17:43:29 +13:00
{
port: action.port
}
);
break;
2017-02-19 21:27:43 +13:00
case 'PUSHER_USERNAME_CHANGED':
2017-11-23 17:43:29 +13:00
helpers.setStorage(
'pusher',
2017-11-23 17:43:29 +13:00
{
username: action.username
}
);
2016-10-28 15:58:07 +13:00
break;
case 'MOPIDY_URISCHEMES_FILTERED':
2017-11-23 17:43:29 +13:00
helpers.setStorage(
'mopidy',
2017-11-23 17:43:29 +13:00
{
uri_schemes: action.data
}
);
break;
case 'SPOTIFY_IMPORT_AUTHORIZATION':
case 'SPOTIFY_AUTHORIZATION_GRANTED':
if (action.authorization !== undefined){
var authorization = action.authorization;
} else if (action.data){
var authorization = action.data;
}
2017-11-23 17:43:29 +13:00
helpers.setStorage(
'spotify',
{
authorization: authorization,
access_token: authorization.access_token,
refresh_token: authorization.refresh_token,
token_expiry: authorization.token_expiry
}
);
break;
2016-10-28 15:58:07 +13:00
case 'SPOTIFY_AUTHORIZATION_REVOKED':
2017-11-23 17:43:29 +13:00
helpers.setStorage(
'spotify',
{
authorization: false,
access_token: false,
refresh_token: false,
2016-10-28 15:58:07 +13:00
token_expiry: false
}
);
break;
case 'SPOTIFY_TOKEN_REFRESHED':
2017-11-23 17:43:29 +13:00
helpers.setStorage(
'spotify',
2017-11-23 17:43:29 +13:00
{
access_token: action.data.access_token,
token_expiry: action.data.token_expiry,
provider: action.provider
2016-10-19 14:09:34 +13:00
}
);
break;
2016-10-28 15:58:07 +13:00
case 'SPOTIFY_ME_LOADED':
2017-11-23 17:43:29 +13:00
helpers.setStorage(
'spotify',
2017-11-23 17:43:29 +13:00
{
me: action.me
2017-11-23 17:43:29 +13:00
}
);
break;
case 'LASTFM_ME_LOADED':
helpers.setStorage(
'lastfm',
{
me: action.me
}
);
break;
case 'LASTFM_AUTHORIZATION_GRANTED':
helpers.setStorage(
'lastfm',
{
session: action.data.session
}
);
break;
case 'LASTFM_AUTHORIZATION_REVOKED':
helpers.setStorage(
'lastfm',
{
session: null
}
);
break;
case 'GENIUS_ME_LOADED':
helpers.setStorage(
'genius',
{
me: action.me
}
);
break;
case 'GENIUS_AUTHORIZATION_GRANTED':
helpers.setStorage(
'genius',
{
authorization: action.data,
authorization_code: action.data.authorization_code,
access_token: action.data.access_token
}
);
break;
case 'GENIUS_AUTHORIZATION_REVOKED':
helpers.setStorage(
'genius',
{
me: null,
authorization: null,
authorization_code: null,
access_token: null
}
2016-10-28 15:58:07 +13:00
);
break;
2016-11-21 16:26:47 +13:00
case 'CORE_SET':
2017-11-23 17:43:29 +13:00
helpers.setStorage(
'core',
2017-11-23 17:43:29 +13:00
action.data
);
break
2016-12-09 18:47:10 +13:00
case 'UI_SET':
2017-11-23 17:43:29 +13:00
helpers.setStorage(
'ui',
2017-11-23 17:43:29 +13:00
action.data
);
2017-05-29 16:29:49 +12:00
break
case 'MOPIDY_SET':
helpers.setStorage(
'mopidy',
action.data
);
break;
case 'SPOTIFY_SET':
helpers.setStorage(
'spotify',
action.data
);
break;
2017-05-29 16:29:49 +12:00
case 'SUPPRESS_BROADCAST':
2017-11-24 09:02:42 +13:00
var ui = helpers.getStorage('ui');
if (ui.suppressed_broadcasts !== undefined){
var suppressed_broadcasts = ui.suppressed_broadcasts;
} else {
var suppressed_broadcasts = [];
}
2017-05-29 16:29:49 +12:00
2017-11-24 09:02:42 +13:00
suppressed_broadcasts.push(action.key);
2017-05-29 16:29:49 +12:00
2017-11-24 09:02:42 +13:00
helpers.setStorage(
'ui',
2017-11-24 09:02:42 +13:00
{
suppressed_broadcasts: suppressed_broadcasts
}
2017-05-29 16:29:49 +12:00
);
break
/**
* Experimental saving of stores to localStorage
* This uses way too much storage space (ie 10MB+) so won't work. We need
* to use the IndexedDB engine instead for storing this quantity of data
case 'UPDATE_TRACKS_INDEX':
helpers.setStorage('core', {tracks: action.tracks});
next(action);
break;
case 'UPDATE_ALBUMS_INDEX':
helpers.setStorage('core', {albums: action.albums});
next(action);
break;
case 'UPDATE_ARTISTS_INDEX':
helpers.setStorage('core', {artists: action.artists});
next(action);
break;
case 'UPDATE_PLAYLISTS_INDEX':
helpers.setStorage('core', {playlists: action.playlists});
next(action);
break;
case 'UPDATE_USERS_INDEX':
helpers.setStorage('core', {users: action.users});
next(action);
break;
*/
}
}
})();
export default localstorageMiddleware