lml-main
Version:
This is now a mono repository published into many standalone packages.
75 lines • 3.7 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const lodash_1 = require("lodash");
const rxjs_1 = require("rxjs");
const redux_observable_1 = require("redux-observable");
const cosmo_ui_auth_1 = require("@lml/cosmo-ui-auth");
const actions_1 = require("../actions");
const cosmo_redux_pusher_1 = require("@lml/cosmo-redux-pusher");
const cosmo_redux_api_1 = require("@lml/cosmo-redux-api");
const selectors_1 = require("../../couriers/selectors");
const MINIMUM_POLLING_INTERVAL = 10000;
// todo - should be expoted from pusher package
const COURIER_LOCATION_CHANNEL_KEY = 'ping';
exports.pollCourierLocations = (action$, store) => action$.ofType(cosmo_ui_auth_1.AUTHENTICATED)
.mergeMap(action => rxjs_1.Observable.interval(MINIMUM_POLLING_INTERVAL)
.map(i => {
const state = store.getState();
const lastUpdate = cosmo_redux_api_1.getLastLocationUpdate(state);
// console.log('LAST COURIER LOCATION UPDATE IS', lastUpdate, diffUpdate(lastUpdate))
if (!lastUpdate || diffUpdate(lastUpdate)) {
const ids = selectors_1.getFilteredCourierIds(state);
// console.log('COURIER IDS ARE', ids)
if (ids && ids.length) {
return cosmo_redux_api_1.fetchLocationByIdBatch(ids);
}
}
return ({ type: 'RELOAD_COURIER_LOCATIONS_NOOP' });
}));
/**
* Note we need to wait till the couriers hve been loaded
* before we can get their locations.
* Once we are getting the courierstatus updates through pusher
* we can probably just chain these calls together without
* checking to see if this is the first time
*
* @param action$
* @param store
*/
exports.loadCourierLocationsAtStartUp = (action$, store) => action$.ofType(cosmo_redux_api_1.FETCH_COURIERS_BY_STATUS.SUCCESS)
.map(action => {
const state = store.getState();
const lastUpdate = cosmo_redux_api_1.getLastLocationUpdate(state);
// console.log('COURIER LOCATIONS LAST UPDATE IS', lastUpdate)
if (lastUpdate === null) {
return actions_1.reloadCourierLocations();
}
// this is annoying but i can't think of a way round this right now
return ({ type: 'LOAD_COURIER_LOCATIONS_AT_START_UP_NOOP' });
});
exports.reloadCourierLocationsEpic = (action$, store) => action$.ofType(actions_1.RELOAD_COURIER_LOCATIONS)
.map(action => {
const state = store.getState();
const lastUpdate = cosmo_redux_api_1.getLastLocationUpdate(state) || 0;
// console.log('LAST COURIER LOCATION UPDATE IS', lastUpdate, diffUpdate(lastUpdate))
if (diffUpdate(lastUpdate)) {
const ids = selectors_1.getFilteredCourierIds(state);
// console.log('COURIER IDS ARE', ids)
if (ids && ids.length) {
return cosmo_redux_api_1.fetchLocationByIdBatch(ids);
}
}
return ({ type: 'RELOAD_COURIER_LOCATIONS_NOOP' });
});
exports.handleHealthcheckTimeout = (action$, store) => action$.ofType(cosmo_redux_pusher_1.PUSHER_HEALTHCHECK_TIMEOUT)
.filter(action => lodash_1.includes(action.channels, COURIER_LOCATION_CHANNEL_KEY))
.mapTo(actions_1.reloadCourierLocations());
exports.handleHealthcheckError = (action$, store) => action$.ofType(cosmo_redux_pusher_1.PUSHER_HEALTHCHECK_ERROR)
.filter(action => action.channel === COURIER_LOCATION_CHANNEL_KEY)
.mapTo(actions_1.reloadCourierLocations());
const diffUpdate = (lastUpdate) => (Date.now() - lastUpdate) > MINIMUM_POLLING_INTERVAL;
exports.courierLocationEpics = redux_observable_1.combineEpics(exports.pollCourierLocations,
//loadCourierLocationsAtStartUp,
//reloadCourierLocationsEpic,
exports.handleHealthcheckTimeout, exports.handleHealthcheckError);
//# sourceMappingURL=reload-courier-locations.js.map