forked from BioPhoton/rxjs-operating-heavily-dynamic-uis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path01_implement-interval.ts
50 lines (44 loc) · 1.63 KB
/
01_implement-interval.ts
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
import {Counter, CountDownState, ConterStateKeys} from './counter'
import { merge, timer, NEVER} from 'rxjs';
import { mapTo, switchMap} from 'rxjs/operators';
// EXERCISE DESCRIPTION ==============================
/**
* Use `ConterStateKeys` for property names.
* Explort the counterUI API by typing `counterUI.` somewhere. ;)
*
* Implement all features of the counter:
* 1. Start, pause the counter. Then restart the counter with 0 (+)
* 2. Start it again from paused number (++)
* 3. If Set to button is clicked set counter value to input value while counting (+++)
* 4. Reset to initial state if reset button is clicked (+)
* 5. If count up button is clicked count up, if count down button is clicked count down (+)
* 6. Change interval if input tickSpeed input changes (++)
* 7. Change count up if input countDiff changes (++)
* 8. Take care of rendering execution and other performance optimisations as well as refactoring (+)
*/
// ==================================================================
const initialConterState: CountDownState = {
isTicking: false,
count: 0,
countUp: true,
tickSpeed: 200,
countDiff:1
};
const counterUI = new Counter(
document.body,
{
initialSetTo: initialConterState.count + 10,
initialTickSpeed: initialConterState.tickSpeed,
initialCountDiff: initialConterState.countDiff,
}
);
merge(
counterUI.btnStart$.pipe(mapTo(true)),
counterUI.btnPause$.pipe(mapTo(false)),
)
.pipe(
switchMap(isTicking => (isTicking) ? timer(0, initialConterState.tickSpeed): NEVER)
)
.subscribe(
s => counterUI.renderCounterValue(s)
);