-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
65 lines (58 loc) · 1.72 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
(() => {
const requestAsync = (url, method, data) => new Promise(requestConsumer => {
const request = new XMLHttpRequest();
request.open(method || "GET", url, true);
request.addEventListener("readystatechange", () => {
const {
readyState,
status,
} = request || {};
if(4 != readyState) return;
alert(status);
if(200 != status) return;
requestConsumer(request);
}, false);
request.send(data);
});
const getTextAsync = url => requestAsync(url)
.then(request => {
const { responseText } = request || {};
return responseText;
})
;
const iterateAsync = (iterator, consumerAsync) => new Promise(iterated => {
if(! iterator) return iterated();
if(! iterator.next) return iterated();
if(! consumerAsync) return iterated();
const loop = () => {
const next = iterator.next();
if(! next) return iterated();
if(next.done) return iterated();
consumerAsync(next.value).then(loop);
};
loop();
});
iterateAsync(
[
"https://unpkg.com/react@16/umd/react.development.js",
"https://unpkg.com/react-dom@16/umd/react-dom.development.js",
"https://unpkg.com/babel-standalone@6.15.0/babel.min.js",
][Symbol.iterator](),
url => new Promise(iterated => {
const cached = localStorage.getItem(url);
new Promise(sourceConsumer => {
if(cached) return sourceConsumer(cached);
alert(url);
getTextAsync(url).then(sourceConsumer);
})
.then(source => {
localStorage.setItem(url, source || "");
(new Function(source || ""))();
iterated();
});
})
).then(loaded => {
alert(window.React);
});
})()
;