|
@@ -1,7 +1,7 @@
|
1
|
1
|
/**
|
2
|
2
|
* Loads a script from a specific URL. React Native cannot load a JS
|
3
|
|
- * file/resource/URL via a <script> HTML element, so the implementation
|
4
|
|
- * fetches the specified src as plain text (e.g. via XMLHttpRequest) and then
|
|
3
|
+ * file/resource/URL via a <script> HTML element, so the implementation
|
|
4
|
+ * fetches the specified src as plain text using fetch() and then
|
5
|
5
|
* evaluates the fetched string as JavaScript code (i.e. via the {@link eval}
|
6
|
6
|
* function).
|
7
|
7
|
*
|
|
@@ -10,35 +10,8 @@
|
10
|
10
|
* @returns {void}
|
11
|
11
|
*/
|
12
|
12
|
export function loadScript(url) {
|
13
|
|
- let fetch;
|
14
|
|
- const method = 'GET';
|
15
|
|
-
|
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();
|
26
|
|
-
|
27
|
|
- xhr.responseType = 'text';
|
28
|
|
-
|
29
|
|
- xhr.onreadystatechange = () => {
|
30
|
|
- if (xhr.readyState === 4) {
|
31
|
|
- resolve(xhr);
|
32
|
|
- }
|
33
|
|
- };
|
34
|
|
-
|
35
|
|
- xhr.open(method, url, /* async */ true);
|
36
|
|
- xhr.send();
|
37
|
|
- });
|
38
|
|
- }
|
39
|
|
-
|
40
|
13
|
return (
|
41
|
|
- fetch
|
|
14
|
+ fetch(url, { method: 'GET' })
|
42
|
15
|
.then(response => {
|
43
|
16
|
switch (response.status) {
|
44
|
17
|
case 200:
|