Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /* @flow */
  2. import { APP_WILL_MOUNT } from '../../base/app';
  3. import {
  4. getAvatarURL,
  5. getLocalParticipant,
  6. getParticipantById,
  7. PARTICIPANT_ID_CHANGED,
  8. PARTICIPANT_JOINED,
  9. PARTICIPANT_UPDATED
  10. } from '../../base/participants';
  11. import { MiddlewareRegistry } from '../../base/redux';
  12. import { ImageCache, prefetch } from './';
  13. /**
  14. * The indicator which determines whether avatar URLs are to be prefetched in
  15. * the middleware here. Unless/until the implementation starts observing the
  16. * redux store instead of the respective redux actions, the value should very
  17. * likely be {@code false} because the middleware here is pretty much the last
  18. * to get a chance to figure out that an avatar URL may be used. Besides, it is
  19. * somewhat uninformed to download just about anything that may eventually be
  20. * used or not.
  21. *
  22. * @private
  23. * @type {boolean}
  24. */
  25. const _PREFETCH_AVATAR_URLS = false;
  26. /**
  27. * Middleware which captures app startup and conference actions in order to
  28. * clear the image cache.
  29. *
  30. * @returns {Function}
  31. */
  32. MiddlewareRegistry.register(({ getState }) => next => action => {
  33. switch (action.type) {
  34. case APP_WILL_MOUNT:
  35. // XXX CONFERENCE_FAILED/LEFT are no longer used here because they
  36. // are tricky to get right as detectors of the moments in time at which
  37. // CachedImage is not used. Anyway, if ImageCache is to be cleared from
  38. // time to time, SET_LOCATION_URL is a much easier detector of such
  39. // opportune times. Fixes at least one 100%-reproducible case of
  40. // "TypeError: Cannot read property handlers of undefined." Anyway, in
  41. // order to reduce the re-downloading of the same avatars, eventually we
  42. // decided to not clear during the runtime of the app (other that at the
  43. // beginning that is).
  44. ImageCache && ImageCache.get().clear();
  45. break;
  46. case PARTICIPANT_ID_CHANGED:
  47. case PARTICIPANT_JOINED:
  48. case PARTICIPANT_UPDATED: {
  49. if (!_PREFETCH_AVATAR_URLS) {
  50. break;
  51. }
  52. const result = next(action);
  53. // Initiate the downloads of participants' avatars as soon as possible.
  54. // 1. Figure out the participant (instance).
  55. let { participant } = action;
  56. if (participant) {
  57. if (participant.id) {
  58. participant = getParticipantById(getState, participant.id);
  59. } else if (participant.local) {
  60. participant = getLocalParticipant(getState);
  61. } else {
  62. participant = undefined;
  63. }
  64. } else if (action.oldValue && action.newValue) {
  65. participant = getParticipantById(getState, action.newValue);
  66. }
  67. if (participant) {
  68. // 2. Get the participant's avatar URL.
  69. const uri = getAvatarURL(participant);
  70. if (uri) {
  71. // 3. Initiate the download of the participant's avatar.
  72. prefetch({ uri });
  73. }
  74. }
  75. return result;
  76. }
  77. }
  78. return next(action);
  79. });