浏览代码

[RN] Make react-native-img-cache optional at bundle time

master
Lyubo Marinov 7 年前
父节点
当前提交
2818520c8f

+ 29
- 18
react/features/base/participants/components/Avatar.native.js 查看文件

@@ -74,8 +74,9 @@ export default class Avatar extends Component {
74 74
         // uri
75 75
         const prevURI = this.props && this.props.uri;
76 76
         const nextURI = nextProps && nextProps.uri;
77
+        const assignState = !this.state;
77 78
 
78
-        if (prevURI !== nextURI || !this.state) {
79
+        if (prevURI !== nextURI || assignState) {
79 80
             const nextState = {
80 81
                 backgroundColor: this._getBackgroundColor(nextProps),
81 82
 
@@ -92,10 +93,10 @@ export default class Avatar extends Component {
92 93
                 source: _DEFAULT_SOURCE
93 94
             };
94 95
 
95
-            if (this.state) {
96
-                this.setState(nextState);
97
-            } else {
96
+            if (assignState) {
98 97
                 this.state = nextState;
98
+            } else {
99
+                this.setState(nextState);
99 100
             }
100 101
 
101 102
             // XXX @lyubomir: My logic for the character # bellow is as follows:
@@ -113,22 +114,32 @@ export default class Avatar extends Component {
113 114
             // an image retrieval action.
114 115
             if (nextURI && !nextURI.startsWith('#')) {
115 116
                 const nextSource = { uri: nextURI };
117
+                const observer = () => {
118
+                    this._unmounted || this.setState((prevState, props) => {
119
+                        if (props.uri === nextURI
120
+                                && (!prevState.source
121
+                                    || prevState.source.uri !== nextURI)) {
122
+                            return { source: nextSource };
123
+                        }
124
+
125
+                        return {};
126
+                    });
127
+                };
116 128
 
117 129
                 // Wait for the source/URI to load.
118
-                ImageCache.get().on(
119
-                    nextSource,
120
-                    /* observer */ () => {
121
-                        this._unmounted || this.setState((prevState, props) => {
122
-                            if (props.uri === nextURI
123
-                                    && (!prevState.source
124
-                                        || prevState.source.uri !== nextURI)) {
125
-                                return { source: nextSource };
126
-                            }
127
-
128
-                            return {};
129
-                        });
130
-                    },
131
-                    /* immutable */ true);
130
+                if (ImageCache) {
131
+                    ImageCache.get().on(
132
+                        nextSource,
133
+                        observer,
134
+                        /* immutable */ true);
135
+                } else if (assignState) {
136
+                    this.state = {
137
+                        ...this.state,
138
+                        source: nextSource
139
+                    };
140
+                } else {
141
+                    observer();
142
+                }
132 143
             }
133 144
         }
134 145
     }

+ 1
- 1
react/features/mobile/image-cache/functions.js 查看文件

@@ -27,5 +27,5 @@ function _onLoad() {
27 27
  * @returns {void}
28 28
  */
29 29
 export function prefetch(source) {
30
-    ImageCache.get().on(source, /* observer */ _onLoad, /* immutable */ true);
30
+    ImageCache && ImageCache.get().on(source, _onLoad, /* immutable */ true);
31 31
 }

+ 1
- 1
react/features/mobile/image-cache/middleware.js 查看文件

@@ -39,7 +39,7 @@ MiddlewareRegistry.register(({ getState }) => next => action => {
39 39
     case APP_WILL_MOUNT:
40 40
     case CONFERENCE_FAILED:
41 41
     case CONFERENCE_LEFT:
42
-        ImageCache.get().clear();
42
+        ImageCache && ImageCache.get().clear();
43 43
         break;
44 44
 
45 45
     case PARTICIPANT_ID_CHANGED:

+ 16
- 0
react/features/mobile/image-cache/react-native-img-cache.no.js 查看文件

@@ -0,0 +1,16 @@
1
+// XXX The third-party react-native modules react-native-fetch-blob utilizes the
2
+// same HTTP library as react-native i.e. okhttp. Unfortunately, that means that
3
+// the versions of okhttp on which react-native and react-native-fetch-blob
4
+// depend may have incompatible APIs. Such an incompatibility will be made
5
+// apparent at compile time and the developer doing the compilation may choose
6
+// to not compile react-native-fetch-blob's source code.
7
+
8
+// XXX The choice between the use of react-native-img-cache could've been done
9
+// at runtime based on whether NativeModules.RNFetchBlob is defined if only
10
+// react-native-fetch-blob would've completely protected itself. At the time of
11
+// this writing its source code appears to be attempting to protect itself from
12
+// missing native binaries but that protection is incomplete and there's a
13
+// TypeError.
14
+
15
+import { Image } from 'react-native';
16
+export { Image as CachedImage, undefined as ImageCache };

正在加载...
取消
保存