Преглед изворни кода

[RN] Keep track of ongoing network requests

Works only for XHR requests, which is the only network request mobile performs
(WebRTC traffic aside). The fetch API is implemented on top of XHR, so that's
covered too.

Requests are kept in the redux store until they complete, at which point they
are removed.
master
Saúl Ibarra Corretgé пре 7 година
родитељ
комит
d669a6c73c

+ 1
- 0
react/features/app/components/App.native.js Прегледај датотеку

@@ -8,6 +8,7 @@ import '../../mobile/audio-mode';
8 8
 import '../../mobile/background';
9 9
 import '../../mobile/external-api';
10 10
 import '../../mobile/full-screen';
11
+import '../../mobile/net-interceptor';
11 12
 import '../../mobile/permissions';
12 13
 import '../../mobile/proximity';
13 14
 import '../../mobile/wake-lock';

+ 11
- 0
react/features/mobile/net-interceptor/actionTypes.js Прегледај датотеку

@@ -0,0 +1,11 @@
1
+/**
2
+ * The type of redux action to update the pending network requests mapping.
3
+ *
4
+ * {
5
+ *     type: UPDATE_NETWORK_REQUESTS,
6
+ *     requests: Object
7
+ * }
8
+ *
9
+ * @protected
10
+ */
11
+export const UPDATE_NETWORK_REQUESTS = Symbol('UPDATE_NETWORK_REQUESTS');

+ 75
- 0
react/features/mobile/net-interceptor/functions.js Прегледај датотеку

@@ -0,0 +1,75 @@
1
+import _ from 'lodash';
2
+import XHRInterceptor from 'react-native/Libraries/Network/XHRInterceptor';
3
+
4
+import { UPDATE_NETWORK_REQUESTS } from './actionTypes';
5
+
6
+/**
7
+ * Global index for keeping track of XHR requests.
8
+ * @type {number}
9
+ */
10
+let reqIndex = 0;
11
+
12
+/**
13
+ * Starts intercepting network requests. Only XHR requests are supported at the
14
+ * moment.
15
+ *
16
+ * Ongoing request information is kept in redux, and it's removed as soon as a
17
+ * given request is complete.
18
+ *
19
+ * @param {Object} store - The redux store.
20
+ * @returns {void}
21
+ */
22
+export function startNetInterception({ dispatch, getState }) {
23
+    XHRInterceptor.setOpenCallback((method, url, xhr) => {
24
+        xhr._reqIndex = reqIndex++;
25
+
26
+        const requests = getState()['features/net-interceptor'].requests || {};
27
+        const newRequests = _.cloneDeep(requests);
28
+        const request = {
29
+            method,
30
+            url
31
+        };
32
+
33
+        newRequests[xhr._reqIndex] = request;
34
+
35
+        dispatch({
36
+            type: UPDATE_NETWORK_REQUESTS,
37
+            requests: newRequests
38
+        });
39
+    });
40
+
41
+    XHRInterceptor.setResponseCallback((...args) => {
42
+        const xhr = args.slice(-1)[0];
43
+
44
+        if (typeof xhr._reqIndex === 'undefined') {
45
+            return;
46
+        }
47
+
48
+        const requests = getState()['features/net-interceptor'].requests || {};
49
+        const newRequests = _.cloneDeep(requests);
50
+
51
+        delete newRequests[xhr._reqIndex];
52
+
53
+        dispatch({
54
+            type: UPDATE_NETWORK_REQUESTS,
55
+            requests: newRequests
56
+        });
57
+    });
58
+
59
+    XHRInterceptor.enableInterception();
60
+}
61
+
62
+/**
63
+ * Stops intercepting network requests.
64
+ *
65
+ * @param {Object} store - The redux store.
66
+ * @returns {void}
67
+ */
68
+export function stopNetInterception({ dispatch }) {
69
+    XHRInterceptor.disableInterception();
70
+
71
+    dispatch({
72
+        type: UPDATE_NETWORK_REQUESTS,
73
+        requests: {}
74
+    });
75
+}

+ 2
- 0
react/features/mobile/net-interceptor/index.js Прегледај датотеку

@@ -0,0 +1,2 @@
1
+import './middleware';
2
+import './reducer';

+ 25
- 0
react/features/mobile/net-interceptor/middleware.js Прегледај датотеку

@@ -0,0 +1,25 @@
1
+import { APP_WILL_MOUNT, APP_WILL_UNMOUNT } from '../../app';
2
+import { MiddlewareRegistry } from '../../base/redux';
3
+
4
+import { startNetInterception, stopNetInterception } from './functions';
5
+
6
+/**
7
+ * Middleware which captures app startup and conference actions in order to
8
+ * clear the image cache.
9
+ *
10
+ * @returns {Function}
11
+ */
12
+MiddlewareRegistry.register(store => next => action => {
13
+    const result = next(action);
14
+
15
+    switch (action.type) {
16
+    case APP_WILL_MOUNT:
17
+        startNetInterception(store);
18
+        break;
19
+    case APP_WILL_UNMOUNT:
20
+        stopNetInterception(store);
21
+        break;
22
+    }
23
+
24
+    return result;
25
+});

+ 15
- 0
react/features/mobile/net-interceptor/reducer.js Прегледај датотеку

@@ -0,0 +1,15 @@
1
+import { ReducerRegistry } from '../../base/redux';
2
+
3
+import { UPDATE_NETWORK_REQUESTS } from './actionTypes';
4
+
5
+ReducerRegistry.register('features/net-interceptor', (state = {}, action) => {
6
+    switch (action.type) {
7
+    case UPDATE_NETWORK_REQUESTS:
8
+        return {
9
+            ...state,
10
+            requests: action.requests
11
+        };
12
+    }
13
+
14
+    return state;
15
+});

Loading…
Откажи
Сачувај