Explorar el Código

[RN] Prefetch remote avatar images 2/2

master
Lyubo Marinov hace 7 años
padre
commit
0836f2cefd
Se han modificado 1 ficheros con 62 adiciones y 1 borrados
  1. 62
    1
      react/features/mobile/image-cache/middleware.js

+ 62
- 1
react/features/mobile/image-cache/middleware.js Ver fichero

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

Loading…
Cancelar
Guardar