您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

middleware.js 2.1KB

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