1var fakeSlowNetwork;
2
3(function() {
4 var lsKey = 'fake-slow-network';
5 var networkFakeDiv = document.querySelector('.network-fake');
6 var checkbox = networkFakeDiv.querySelector('input');
7
8 fakeSlowNetwork = Number(localStorage.getItem(lsKey)) || 0;
9
10 networkFakeDiv.style.display = 'block';
11 checkbox.checked = !!fakeSlowNetwork;
12
13 checkbox.addEventListener('change', function() {
14 localStorage.setItem(lsKey, Number(checkbox.checked));
15 location.reload();
16 });
17}());
18
19function spawn(generatorFunc) {
20 function continuer(verb, arg) {
21 var result;
22 try {
23 result = generator[verb](arg);
24 } catch (err) {
25 return Promise.reject(err);
26 }
27 if (result.done) {
28 return result.value;
29 } else {
30 return Promise.resolve(result.value).then(callback, errback);
31 }
32 }
33 var generator = generatorFunc();
34 var callback = continuer.bind(continuer, "next");
35 var errback = continuer.bind(continuer, "throw");
36 return callback();
37}
38
39function wait(ms) {
40 return new Promise(function(resolve) {
41 setTimeout(resolve, ms);
42 });
43}
44
45function get(url) {
46 // Return a new promise.
47 // We do all the work within the constructor callback.
48 var fakeNetworkWait = wait(3000 * Math.random() * fakeSlowNetwork);
49
50 var requestPromise = new Promise(function(resolve, reject) {
51 // Do the usual XHR stuff
52 var req = new XMLHttpRequest();
53 req.open('get', url);
54
55 req.onload = function() {
56 // 'load' triggers for 404s etc
57 // so check the status
58 if (req.status == 200) {
59 // Resolve the promise with the response text
60 resolve(req.response);
61 }
62 else {
63 // Otherwise reject with the status text
64 reject(Error(req.statusText));
65 }
66 };
67
68 // Handle network errors
69 req.onerror = function() {
70 reject(Error("Network Error"));
71 };
72
73 // Make the request
74 req.send();
75 });
76
77 return Promise.all([fakeNetworkWait, requestPromise]).then(function(results) {
78 return results[1];
79 });
80}
81
82function getJson(url) {
83 return get(url).then(JSON.parse);
84}
85
86function getSync(url) {
87 var startTime = Date.now();
88 var waitTime = 3000 * Math.random() * fakeSlowNetwork;
89
90 var req = new XMLHttpRequest();
91 req.open('get', url, false);
92 req.send();
93
94 while (waitTime > Date.now() - startTime);
95
96 if (req.status == 200) {
97 return req.response;
98 }
99 else {
100 throw Error(req.statusText || "Request failed");
101 }
102}
103
104function getJsonSync(url) {
105 return JSON.parse(getSync(url));
106}
107
108function getJsonCallback(url, callback) {
109 getJson(url).then(function(response) {
110 callback(undefined, response);
111 }, function(err) {
112 callback(err);
113 });
114}
115
116var storyDiv = document.querySelector('.story');
117
118function addHtmlToPage(content) {
119 var div = document.createElement('div');
120 div.innerHTML = content;
121 storyDiv.appendChild(div);
122}
123
124function addTextToPage(content) {
125 var p = document.createElement('p');
126 p.textContent = content;
127 storyDiv.appendChild(p);
128}