Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

middleware.js 2.1KB

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