You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

middleware.ts 2.2KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. import { IStore } from '../app/types';
  2. import { SET_CONFIG } from '../base/config/actionTypes';
  3. import { ADD_KNOWN_DOMAINS } from '../base/known-domains/actionTypes';
  4. import MiddlewareRegistry from '../base/redux/MiddlewareRegistry';
  5. import { equals } from '../base/redux/functions';
  6. import { APP_STATE_CHANGED } from '../mobile/background/actionTypes';
  7. import { REFRESH_CALENDAR } from './actionTypes';
  8. import { setCalendarAuthorization } from './actions';
  9. import { _fetchCalendarEntries, isCalendarEnabled } from './functions';
  10. MiddlewareRegistry.register(store => next => action => {
  11. const { getState } = store;
  12. if (!isCalendarEnabled(getState)) {
  13. return next(action);
  14. }
  15. switch (action.type) {
  16. case ADD_KNOWN_DOMAINS: {
  17. // XXX Fetch new calendar entries only when an actual domain has
  18. // become known.
  19. const oldValue = getState()['features/base/known-domains'];
  20. const result = next(action);
  21. const newValue = getState()['features/base/known-domains'];
  22. equals(oldValue, newValue)
  23. || _fetchCalendarEntries(store, false, false);
  24. return result;
  25. }
  26. case APP_STATE_CHANGED: {
  27. const result = next(action);
  28. _maybeClearAccessStatus(store, action);
  29. return result;
  30. }
  31. case SET_CONFIG: {
  32. const result = next(action);
  33. _fetchCalendarEntries(store, false, false);
  34. return result;
  35. }
  36. case REFRESH_CALENDAR: {
  37. const result = next(action);
  38. _fetchCalendarEntries(
  39. store, action.isInteractive, action.forcePermission);
  40. return result;
  41. }
  42. }
  43. return next(action);
  44. });
  45. /**
  46. * Clears the calendar access status when the app comes back from the
  47. * background. This is needed as some users may never quit the app, but puts it
  48. * into the background and we need to try to request for a permission as often
  49. * as possible, but not annoyingly often.
  50. *
  51. * @param {Object} store - The redux store.
  52. * @param {Object} action - The Redux action.
  53. * @private
  54. * @returns {void}
  55. */
  56. function _maybeClearAccessStatus(store: IStore, { appState }: { appState: string; }) {
  57. appState === 'background'
  58. && store.dispatch(setCalendarAuthorization(undefined));
  59. }