-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.js
54 lines (47 loc) · 1.24 KB
/
main.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
import React from 'react';
import ReactDOM from 'react-dom';
import { of, interval, concat } from 'rxjs';
import {
takeWhile,
takeUntil,
scan,
startWith,
repeatWhen,
share,
} from 'rxjs/operators';
import { useEpic, ofType } from 'use-epic';
import './styles.css';
function alarmClockEpic(action$) {
const snooze$ = action$.pipe(ofType('snooze'));
const dismiss$ = action$.pipe(ofType('dismiss'));
const alarm$ = concat(
interval(250).pipe(
startWith(5),
scan(time => time - 1),
takeWhile(time => time > 0)
),
of('Wake up! 🎉')
).pipe(share());
const snoozableAlarm$ = alarm$.pipe(
repeatWhen(() => snooze$.pipe(takeUntil(dismiss$)))
);
return concat(snoozableAlarm$, of('Have a wonderful day! 🤗'));
}
function App() {
const [display, dispatch] = useEpic(alarmClockEpic);
const snooze = () => dispatch('snooze');
const dismiss = () => dispatch('dismiss');
return (
<>
<div className='display'>{display}</div>
<button onClick={snooze} className='snooze'>
Snooze
</button>
<button onClick={dismiss} className='dismiss'>
Dismiss
</button>
</>
);
}
const rootElement = document.getElementById('root');
ReactDOM.render(<App />, rootElement);