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.js 2.5KB

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