2017-08-26 16:04:28 +02:00
|
|
|
import { UPDATE_RESOURCES, RESOURCES_REMOVED } from '../actions/resources';
|
2017-09-08 06:55:47 +02:00
|
|
|
import { SOCKET_STATE, SOCKET_UPDATE } from '../actions/socket';
|
2017-08-26 16:04:28 +02:00
|
|
|
|
2017-12-29 20:56:41 +01:00
|
|
|
function hack(old, _new) {
|
|
|
|
if (old && old.type == "torrent") {
|
|
|
|
if (old.progress != 1 && _new.progress == 1) {
|
|
|
|
Notification && new Notification("Download complete: " + _new.name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return _new;
|
|
|
|
}
|
|
|
|
|
2017-08-26 16:04:28 +02:00
|
|
|
export default function resourceReducer(type) {
|
|
|
|
return (state = {}, action) => {
|
2018-02-26 08:17:36 +01:00
|
|
|
let ns = {...state};
|
2017-08-26 16:04:28 +02:00
|
|
|
switch (action.type) {
|
|
|
|
case UPDATE_RESOURCES:
|
2018-02-26 08:17:36 +01:00
|
|
|
action.resources
|
2018-03-10 15:48:10 +01:00
|
|
|
.filter(r => r.type === type)
|
2018-02-26 08:17:36 +01:00
|
|
|
.map(r => ns[r.id] = hack(state[r.id], { ...state[r.id], ...r }));
|
|
|
|
return ns;
|
2017-08-26 16:04:28 +02:00
|
|
|
case RESOURCES_REMOVED:
|
2018-02-26 08:17:36 +01:00
|
|
|
action.ids
|
|
|
|
.map(id => delete ns[id]);
|
|
|
|
return ns;
|
2017-09-08 06:55:47 +02:00
|
|
|
case SOCKET_UPDATE:
|
|
|
|
const _state = action.state;
|
|
|
|
return _state === SOCKET_STATE.CONNECTING ? {} : state;
|
2017-08-26 16:04:28 +02:00
|
|
|
}
|
|
|
|
return state;
|
|
|
|
};
|
|
|
|
}
|