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.5KB

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