-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclockView.js
83 lines (67 loc) · 1.43 KB
/
clockView.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
const choo = require('choo')
const html = require('choo/html')
const css = require('sheetify')
const prefix = css`
:host {
flex: none;
height: 58px;
display: block;
margin: 0;
padding: 12px 20px 5px;
font-weight: 200;
font-size: 48px;
line-height: 1;
background: rgba(255, 255, 255, 0.1);
color: rgba(255, 255, 255, 0.9);
border-radius: 8px;
}
`
const id = 'the-clock'
const app = choo()
if (process.env.NODE_ENV !== 'production') {
app.use(require('choo-devtools')())
}
app.use(store)
app.route('*', view)
const tree = app.start()
function view (state, emit) {
return html`
<p class=${prefix}>
${state.time}
</p>
`
}
function currentTimeString () {
const now = new Date()
let hours = now.getHours()
if (hours < 10) {
hours = `0${hours}`
}
let minutes = now.getMinutes()
if (minutes < 10) {
minutes = `0${minutes}`
}
return `${hours}:${minutes}`
}
function store (state, bus) {
state.time = currentTimeString()
function emit (...args) { bus.emit(...args) }
function render () { emit('render') }
bus.on('clock:update', time => {
if (state.time !== time) {
state.time = time
render()
}
})
setInterval(() => {
const time = currentTimeString()
if (state.time !== time) {
emit('clock:update', time)
}
}, 5000)
}
module.exports = () => {
const el = html`<div id=${id}></div>`
el.appendChild(tree)
return el
}