Skip to content

Commit 4aac019

Browse files
sahrensfacebook-github-bot
authored andcommitted
add DEBUG_NETWORK_SEND_DELAY for simulating slow network
Summary: It can be a pain to debug slow network issues, especially with the iOS simulator which doesn't have a network link conditioner. This makes it really easy and predictable by simply adding a `setTimeout` around calling `sendRequest`. Changelog: [General] [Added] - DEBUG_NETWORK_SEND_DELAY can be used to simulate slow network. Reviewed By: PeteTheHeat Differential Revision: D19236911 fbshipit-source-id: 14762c7e0c6408a8364aa569c482729a7a1fe740
1 parent 674b591 commit 4aac019

File tree

1 file changed

+25
-16
lines changed

1 file changed

+25
-16
lines changed

Libraries/Network/XMLHttpRequest.js

+25-16
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ const base64 = require('base64-js');
1818
const invariant = require('invariant');
1919
const warning = require('fbjs/lib/warning');
2020

21+
const DEBUG_NETWORK_SEND_DELAY: false = false; // Set to a number of milliseconds when debugging
22+
2123
export type NativeResponseType = 'base64' | 'blob' | 'text';
2224
export type ResponseType =
2325
| ''
@@ -511,22 +513,29 @@ class XMLHttpRequest extends (EventTarget(...XHR_EVENTS): any) {
511513
nativeResponseType = 'blob';
512514
}
513515

514-
invariant(this._method, 'Request method needs to be defined.');
515-
invariant(this._url, 'Request URL needs to be defined.');
516-
RCTNetworking.sendRequest(
517-
this._method,
518-
this._trackingName,
519-
this._url,
520-
this._headers,
521-
data,
522-
/* $FlowFixMe(>=0.78.0 site=react_native_android_fb) This issue was found
523-
* when making Flow check .android.js files. */
524-
nativeResponseType,
525-
incrementalEvents,
526-
this.timeout,
527-
this.__didCreateRequest.bind(this),
528-
this.withCredentials,
529-
);
516+
const doSend = () => {
517+
invariant(this._method, 'Request method needs to be defined.');
518+
invariant(this._url, 'Request URL needs to be defined.');
519+
RCTNetworking.sendRequest(
520+
this._method,
521+
this._trackingName,
522+
this._url,
523+
this._headers,
524+
data,
525+
/* $FlowFixMe(>=0.78.0 site=react_native_android_fb) This issue was found
526+
* when making Flow check .android.js files. */
527+
nativeResponseType,
528+
incrementalEvents,
529+
this.timeout,
530+
this.__didCreateRequest.bind(this),
531+
this.withCredentials,
532+
);
533+
};
534+
if (DEBUG_NETWORK_SEND_DELAY) {
535+
setTimeout(doSend, DEBUG_NETWORK_SEND_DELAY);
536+
} else {
537+
doSend();
538+
}
530539
}
531540

532541
abort(): void {

0 commit comments

Comments
 (0)