|
@@ -1,49 +1,55 @@
|
1
|
1
|
/**
|
2
|
|
- * Loads a script from a specific source. React Native cannot load a JS
|
|
2
|
+ * Loads a script from a specific URL. React Native cannot load a JS
|
3
|
3
|
* file/resource/URL via a <script> HTML element, so the implementation
|
4
|
4
|
* fetches the specified src as plain text (e.g. via XMLHttpRequest) and then
|
5
|
5
|
* evaluates the fetched string as JavaScript code (i.e. via the {@link eval}
|
6
|
6
|
* function).
|
7
|
7
|
*
|
8
|
|
- * @param {string} src - The source from the which the script is to be
|
9
|
|
- * (down)loaded. Only absolute URLs are supported.
|
10
|
|
- * @param {Object} options - Additional options.
|
11
|
|
- * @param {boolean} options.async=true - True to asynchronously load the script
|
12
|
|
- * or false to synchronously load the script.
|
|
8
|
+ * @param {string} url - The absolute URL from the which the script is to be
|
|
9
|
+ * (down)loaded.
|
13
|
10
|
* @returns {void}
|
14
|
11
|
*/
|
15
|
|
-export function loadScript(
|
16
|
|
- src,
|
17
|
|
- options = {
|
18
|
|
- async: true
|
19
|
|
- }) {
|
20
|
|
- return new Promise((resolve, reject) => {
|
21
|
|
- // XXX We are using XMLHttpRequest instead of Fetch API only in order
|
22
|
|
- // to be able to do 'sync' requests. If this not needed, this can be
|
23
|
|
- // replaced with much simpler and readable fetch().
|
24
|
|
- const xhr = new XMLHttpRequest();
|
|
12
|
+export function loadScript(url) {
|
|
13
|
+ let fetch;
|
|
14
|
+ const method = 'GET';
|
25
|
15
|
|
26
|
|
- xhr.open('GET', src, options.async);
|
27
|
|
- xhr.responseType = 'text';
|
|
16
|
+ // Prefer the Fetch API. Apart from the fact that we're fetching the
|
|
17
|
+ // specified script as a static resource, the Fetch API provides more
|
|
18
|
+ // detailed errors.
|
|
19
|
+ if (typeof (fetch = window.fetch) === 'function') {
|
|
20
|
+ fetch = fetch(url, { method });
|
|
21
|
+ } else {
|
|
22
|
+ // Otherwise, fall back to the XMLHttpRequest API.
|
|
23
|
+ fetch
|
|
24
|
+ = new Promise(resolve => {
|
|
25
|
+ const xhr = new XMLHttpRequest();
|
28
|
26
|
|
29
|
|
- xhr.onload = () => {
|
30
|
|
- if (xhr.readyState === 4) {
|
31
|
|
- if (xhr.status === 200) {
|
32
|
|
- try {
|
33
|
|
- // eslint-disable-next-line no-eval
|
34
|
|
- eval.call(window, xhr.responseText);
|
35
|
|
- resolve();
|
36
|
|
- } catch (e) {
|
37
|
|
- reject(e);
|
|
27
|
+ xhr.responseType = 'text';
|
|
28
|
+
|
|
29
|
+ xhr.onreadystatechange = () => {
|
|
30
|
+ if (xhr.readyState === 4) {
|
|
31
|
+ resolve(xhr);
|
38
|
32
|
}
|
39
|
|
- } else {
|
40
|
|
- reject(xhr.statusText);
|
41
|
|
- }
|
42
|
|
- }
|
43
|
|
- };
|
|
33
|
+ };
|
|
34
|
+
|
|
35
|
+ xhr.open(method, url, /* async */ true);
|
|
36
|
+ xhr.send();
|
|
37
|
+ });
|
|
38
|
+ }
|
44
|
39
|
|
45
|
|
- xhr.onerror = () => reject(xhr.statusText);
|
|
40
|
+ return (
|
|
41
|
+ fetch
|
|
42
|
+ .then(response => {
|
|
43
|
+ switch (response.status) {
|
|
44
|
+ case 200:
|
|
45
|
+ return response.responseText || response.text();
|
|
46
|
+
|
|
47
|
+ default:
|
|
48
|
+ throw response.statusText;
|
|
49
|
+ }
|
46
|
50
|
|
47
|
|
- xhr.send();
|
48
|
|
- });
|
|
51
|
+ })
|
|
52
|
+ .then(responseText => {
|
|
53
|
+ eval.call(window, responseText); // eslint-disable-line no-eval
|
|
54
|
+ }));
|
49
|
55
|
}
|