-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
66 lines (56 loc) · 1.88 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
66
import { fromEvent, merge, timer } from 'https://ga.jspm.io/npm:[email protected]/dist/esm5/index.js'
import {
map,
mapTo,
filter,
pluck,
switchMap,
takeWhile,
withLatestFrom
} from "https://ga.jspm.io/npm:[email protected]/dist/esm5/operators/index.js";
const durationInput = document.querySelector("#timer-input");
const startButton = document.querySelector("#start-button");
const resetButton = document.querySelector("#reset-button");
const timeTextElement = document.querySelector("#time-text");
// Capture input events
const durationEvent$ = fromEvent(durationInput, "input").pipe(
map((e) => Number(e.target.value))
);
const enterEvent$ = fromEvent(durationInput, "keyup").pipe(
filter((e) => e.key === "Enter")
);
const startEvent$ = fromEvent(startButton, "click");
const resetEvent$ = fromEvent(resetButton, "click").pipe(mapTo(-1));
// Combine confirmation inputs
const timerEvent$ = merge(enterEvent$, startEvent$);
// Start timer from confirmation event
const timerStateWithInput$ = timerEvent$.pipe(
withLatestFrom(durationEvent$),
pluck(1), // We're not interested in the timerEvent$ value, only the durationEvent$ one, pick second array value
filter((duration) => duration !== 0) // Ignore when 0 to keep innter Observable running
);
// Calculate remaining time
const timer$ = merge(timerStateWithInput$, resetEvent$).pipe(
switchMap((duration) =>
merge(
timer(0, 1000).pipe(
map((r) => duration - r),
takeWhile((r) => r > 0, true) // Stop inner Observable when time is over
)
)
)
);
timer$.subscribe({
next: renderToDom
});
function renderToDom(time) {
if (time === -1) {
console.log("Reset");
durationInput.value = "";
timeTextElement.innerHTML = "Reset, please start again!";
return;
}
const text = time ? `Remaining time ${time} second(s).` : "Time is up!";
console.log(text);
timeTextElement.innerHTML = text;
}