-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
49 lines (39 loc) · 1.29 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
let choo = require('choo')
let app = choo()
if (process.env.NODE_ENV !== 'production') {
app.use(require('choo-devtools')())
}
function store (state, emitter) {
state.fetch = { }
state.fetching = false
state.emoticounter = 0
emitter.on('DOMContentLoaded', function () {
setInterval(() => emitter.emit('emoticon:next'), 800)
emitter.on('emoticon:next', function () {
state.emoticounter += 1
emitter.emit(state.events.RENDER)
})
emitter.on('fetch', function(key) {
if (state.fetching || state.fetch[key]) { return }
state.fetching = true
fetch(`/assets/md/${key}.md?${Date.now()}`)
.then((res) => res.text())
.then((txt) => {
state.fetch[key] = txt
state.fetching = false
emitter.emit(state.events.RENDER)
}).catch(console.error)
})
})
}
app.use(store)
app.route('/', require('./lib/home.js'))
app.route('/blog/:path', require('./lib/entry.js'))
app.route('/privacy', require('./lib/privacy.js'))
app.route('/*', require('./lib/404.js'))
// redirect from old path to new.
app.route('/blog/ai-ching-rejected-by-apple', function (state, emit) {
emit(state.events.REPLACESTATE, '/blog/introducing-the-ai-ching')
return require('./lib/entry.js')(state, emit)
})
module.exports = app.mount('.app')