-
-
Notifications
You must be signed in to change notification settings - Fork 43
/
app.js
365 lines (321 loc) · 12.8 KB
/
app.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
if ("serviceWorker" in navigator) {
window.addEventListener("load", function() {
navigator.serviceWorker
.register("/serviceWorker.js")
.then(res => console.log("service worker registered"))
.catch(err => console.log("service worker not registered", err))
})
}
// Get the time represented in the
// format of hh:mm:ss(24 hours clock)
const getTimeClock = (showMs) => {
const now = new Date();
let h = now.getHours();
let m = now.getMinutes();
let s = now.getSeconds();
let ms = now.getMilliseconds();
m = padWithZero(m);
s = padWithZero(s);
return showMs ? `${h}:${m}:${s}:${ms}` : `${h}:${m}:${s}`;
};
// Pad the single digit numbers with zero
const padWithZero = (num) => {
if (num < 10) {num = "0" + num};
return num;
}
// Get the delay values for all the colors
let redDelay = document.getElementById("red-delays-id").value;
let greenDelay = document.getElementById("green-delays-id").value;;
let blueDelay = document.getElementById("blue-delays-id").value;;
console.log(`Initial delays: red - ${redDelay}, green - ${greenDelay}, blue - ${blueDelay}`);
// Get the reject flag for all the colors
let rejectRed = document.getElementById("red-error-id").checked;
let rejectGreen = document.getElementById("green-error-id").checked;
let rejectBlue = document.getElementById("blue-error-id").checked;
console.log(`Initial rejections: red - ${rejectRed}, green - ${rejectGreen}, blue - ${rejectBlue}`);
// Retain Logs from LS
let retainLogs = window.localStorage.getItem('promiviz-persist-logs') || false;
document.getElementById('persist-logs').checked = (retainLogs === 'true');
// Call this function when a delay is set for a color
const selectDelay = () => {
redDelay = document.getElementById("red-delays-id").value;
greenDelay = document.getElementById("green-delays-id").value;;
blueDelay = document.getElementById("blue-delays-id").value;;
console.log(`Delays: red - ${redDelay}, green - ${greenDelay}, blue - ${blueDelay}`);
selectValue();
}
// Call this function when a reject flag is set for a color
const rejectColor = () => {
rejectRed = document.getElementById("red-error-id").checked;
rejectGreen = document.getElementById("green-error-id").checked;
rejectBlue = document.getElementById("blue-error-id").checked;
console.log(`Rejections: red - ${rejectRed}, green - ${rejectGreen}, blue - ${rejectBlue}`);
selectValue();
}
// Call this method when a Promise API is selected
const selectValue = () => {
clear();
if (!retainLogs) {
clearLogs();
}
const apiSelectBox = document.getElementById("apis-list-id");
const selected = apiSelectBox.value;
console.log(selected);
switch (selected) {
case 'all':
explain(
`Promise.all takes an <b>array of promises</b> and returns a new promise.
The new promise resolves when <b>all input promises are resolved</b>. In the
example below, notice the time when all promises resolve. It will be
almost equal to the <b>max time</b> taken by a promise(color).`
);
handleAll();
break;
case 'any':
explain(
`Promise.any takes an <b>array of promises</b> and returns a new promise.
It resolves when <b>any</b> of the input promises is fulfilled. In the example below,
notice <b>any</b> of the promises resolves when you apply the same delay.`
);
handleAny();
break;
case 'race':
explain(
`Promise.race takes <b>an array of promises</b> and returns a new promise.
It resolves when the first promise settles(result/error). In the example below,
notice the promise with the <b>least</b> delay resolves first and in turn, the <b>race</b> is over.`
);
handleRace();
break;
case 'allSettled':
explain(
`Promise.allSettled takes an <b>array of promises</b> and returns a new promise.
It resolves when all promises <b> are settled(result/error)</b>. In the
example below, notice the time when all promises settle. It will be
almost equal to the <b>max time</b> taken by a promise(color).`
);
handleAllSettled();
break;
case 'handleResolve':
explain(
`Promise.resolve creates a <b>resolved</b> promise. In the example below, we resolve
the promises one by one!`
);
handleResolve();
break
default:
explain(``);
break;
}
};
// Set the value, color, and animation when a Promise Resolves
const setValues = (color, value, duration) => {
document.getElementById(`${color}-id`).innerHTML = value;
document.getElementById(`${color}-id`).parentElement.setAttribute('class', value);
document.getElementById(`${color}-id`).style.animationName = value;
document.getElementById(`${color}-id`).style.animationDuration = duration;
}
// Clear all the value, color, animations
const clear = () => {
const colors = ['red', 'green', 'blue'];
colors.forEach(color => {
setValues(color, '', '0s');
});
}
// Clear all the logs from the log board
const clearLogs = () => {
const logElem = document.getElementById('log-id');
logElem.innerHTML = '';
}
const persistLogs = () => {
const cb = document.getElementById('persist-logs');
if(cb.checked) {
retainLogs = true;
} else {
retainLogs = false;
}
window.localStorage.setItem('promiviz-persist-logs', retainLogs)
console.log(`Persistence: ${retainLogs}`);
}
// Add logs to the log board
const log = (msg, isError) => {
const logElem = document.getElementById('log-id');
const logListElem = document.createElement('li');
isError ? logListElem.setAttribute('class', 'error') : logListElem.removeAttribute('class');
logListElem.innerHTML = `<span><b>${getTimeClock()} -</b> ${msg}</span>`;
logElem.appendChild(logListElem);
}
const setTheme = (theme) => {
const logElem = document.getElementById('log-container');
const itemToRemove = logElem.classList.item(1);
logElem.classList.remove(itemToRemove);
logElem.classList.add(theme);
const body = document.body;
body.classList.remove(itemToRemove);
body.classList.add(theme);
const config = document.getElementsByClassName("config");
config[0].classList.remove(itemToRemove);
config[0].classList.add(theme);
}
// toggle side nav
const toggleNav = () => {
const navElem = document.getElementById("sidenav-id");
if (navElem.classList.contains('active')) {
navElem.classList.remove('active');
} else {
navElem.classList.add('active');
}
}
// close the nav bar
const closeNav = () => {
const navElem = document.getElementById("sidenav-id");
navElem.classList.remove('active');
}
const explain = msg => {
const explainElem = document.getElementById("explanation-id");
explainElem.innerHTML = msg;
}
// Handle the Promise.all() API
const handleAll = async () => {
try {
log(`🕛 Started handling all the colors using Promise.all([red, green, blue])`);
const red = new Promise((resolve, reject) => {
setTimeout(() => {
rejectRed ? reject('red') : resolve('red');
}, redDelay);
});
const green = new Promise((resolve, reject) => {
setTimeout(() => {
rejectGreen ? reject('green') : resolve('green');
}, greenDelay);
});
const blue = new Promise((resolve, reject) => {
setTimeout(() => {
rejectBlue ? reject('blue') : resolve('blue');
}, blueDelay);
});
const colors = await Promise.all([red, green, blue]);
console.log(colors);
colors.forEach(color => {
setValues(color, color, '3s');
})
log(`✔️ Finished handling all the colors using Promise.all([red, green, blue])`);
}catch(err) {
log(`❌ Rejected the color ${err}. Rejecting All.`, true);
};
};
// Handle the Promise.any() API
const handleAny = async () => {
try {
log(`🕛 Started handling any of the colors using Promise.any([red, green, blue])`);
const green = new Promise((resolve, reject) => {
setTimeout(() => {
rejectGreen ? reject('green') : resolve('green');
}, greenDelay);
});
const red = new Promise((resolve, reject) => {
setTimeout(() => {
rejectRed ? reject('red') : resolve('red');
}, redDelay);
});
const blue = new Promise((resolve, reject) => {
setTimeout(() => {
rejectBlue ? reject('blue') : resolve('blue');
}, blueDelay);
});
const color = await Promise.any([red, green, blue]);
log(`${color} got picked up! Lucky one.`);
setValues(color, color, '3s');
log(`✔️Finished handling any of the colors using Promise.any([red, green, blue])`);
}catch(err) {
log(`❌ Rejected the color ${err}.`, true);
};
};
// Handle the Promise.race() API
const handleRace = async () => {
try{
log(`🕛 Started racing of the colors using Promise.race([red, green, blue])`);
const blue = new Promise((resolve, reject) => {
setTimeout(() => {
rejectBlue ? reject('blue') : resolve('blue');
}, blueDelay);
});
const red = new Promise((resolve, reject) => {
setTimeout(() => {
rejectRed ? reject('red') : resolve('red');
}, redDelay);
});
const green = new Promise((resolve, reject) => {
setTimeout(() => {
rejectGreen ? reject('green') : resolve('green');
}, greenDelay);
});
const color = await Promise.race([ green, red, blue]);
log(`${color} own the race! Great champ.`);
setValues(color, color, '3s');
log(`✔️ Finished racing of the colors using Promise.race([red, green, blue])`);
} catch(err) {
log(`❌ Rejected the color ${err}. Rejecting All`, true);
}
};
// Handle the Promise.allSettled() API
const handleAllSettled = async () => {
log(`🕛 Started settling of the colors using Promise.allSettled([red, green, blue])`);
const red = new Promise((resolve, reject) => {
setTimeout(() => {
rejectRed ? reject('red') : resolve('red');
}, redDelay);
});
const green = new Promise((resolve, reject) => {
setTimeout(() => {
rejectGreen ? reject('green') : resolve('green');
}, greenDelay);
});
const blue = new Promise((resolve, reject) => {
setTimeout(() => {
rejectBlue ? reject('blue') : resolve('blue');
}, blueDelay);
});
const colors = await Promise.allSettled([red, green, blue]);
console.log(colors);
for (const {status, value, reason} of colors) {
if (status === 'rejected') {
log(`❌ Rejected the color ${reason}.`, true);
} else if (status === 'fulfilled') {
setValues(value, value, '3s');
}
}
log(`✔️ Finished settling of the colors using Promise.allSettled([red, green, blue])`);
};
// Handle the Promise.resolve() API
const handleResolve = async () => {
try {
log(`🕛 Resolving all colors individually with Promise.resolve(color => red, green, blue)`);
const red = new Promise((resolve, reject) => {
setTimeout(() => {
rejectRed ? reject('red') : resolve('red');
}, redDelay);
});
const green = new Promise((resolve, reject) => {
setTimeout(() => {
rejectGreen ? reject('green') : resolve('green');
}, greenDelay);
});
const blue = new Promise((resolve, reject) => {
setTimeout(() => {
rejectBlue ? reject('blue') : resolve('blue');
}, blueDelay);
});
const redResolved = await Promise.resolve(red);
log(`✔️ Finished resolving ${redResolved} using Promise.resolve(${redResolved})`);
setValues(redResolved, redResolved, '3s');
const greenResolved = await Promise.resolve(green);
log(`✔️ Finished resolving ${greenResolved} using Promise.resolve(${greenResolved})`);
setValues(greenResolved, greenResolved, '3s');
const blueResolved = await Promise.resolve(blue);
log(`✔️ Finished resolving ${blueResolved} using Promise.resolve(${blueResolved})`);
setValues(blueResolved, blueResolved, '3s');
}catch(err) {
log(`❌ Rejected the color ${err}.`, true);
};
};