-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathTask.tsx
81 lines (67 loc) · 1.61 KB
/
Task.tsx
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
import * as O from 'fp-ts/lib/Option'
import * as T from 'fp-ts/lib/Task'
import { pipe } from 'fp-ts/lib/pipeable'
import * as React from 'react'
import { cmd } from '../src'
import { Html } from '../src/React'
import { perform } from '../src/Task'
import { now } from '../src/Time'
type Time = number
// --- Flags
export type Flags = void
export const flags: Flags = undefined
// --- Model
export type Model = O.Option<Time>
export function init(_: Flags): [Model, cmd.Cmd<Msg>] {
return [
O.none,
pipe(
now(),
perform(newTime)
)
]
}
// --- Messages
export type Msg = { type: 'Click' } | NewTime
export type NewTime = { type: 'NewTime'; time: Time }
// --- Update
function newTime(time: Time): NewTime {
return { type: 'NewTime', time }
}
function delay<A>(n: number, task: T.Task<A>): T.Task<A> {
return () =>
new Promise(resolve => {
setTimeout(() => task().then(resolve), n)
})
}
export function update(msg: Msg, _: Model): [Model, cmd.Cmd<Msg>] {
switch (msg.type) {
case 'Click':
return [
O.none,
pipe(
delay(1000, now()),
perform(newTime)
)
]
case 'NewTime':
return [O.some(msg.time), cmd.none]
}
}
// --- View
export function view(model: Model): Html<Msg> {
return dispatch => (
<div>
Time:{' '}
{pipe(
model,
O.fold(displayLoading, displayTime)
)}
<button onClick={() => dispatch({ type: 'Click' })}>New time</button>
</div>
)
}
function displayTime(time: Time): string {
return new Date(time).toISOString()
}
const displayLoading = () => 'loading...'