-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
88 lines (67 loc) · 1.98 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
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
84
85
86
87
const DOMTITLECHANGE = 'DOMTitleChange'
const PUSHSTATE = 'pushState'
const POPSTATE = 'popState'
const REPLACESTATE = 'replaceState'
const RENDER = 'render'
const DOMCONTENTLOADED = 'DOMContentLoaded'
const NAVIGATE = 'navigate'
const types = {
DOMCONTENTLOADED, NAVIGATE, RENDER, DOMTITLECHANGE, PUSHSTATE, POPSTATE, REPLACESTATE
}
module.exports.types = types
/**
* Action creators
*/
module.exports.changeDOMTitle = changeDOMTitle
function changeDOMTitle(title) {
return {type: DOMTITLECHANGE, payload: title}
}
module.exports.render = render
function render() {
return {type: RENDER}
}
module.exports.pushState = pushState
function pushState(route) {
return {type: PUSHSTATE, payload: route}
}
module.exports.replaceState = replaceState
function replaceState(route) {
return {type: REPLACESTATE, payload: route}
}
module.exports.popState = popState
function popState(route) {
return {type: POPSTATE, payload: route}
}
module.exports.chooMiddleware = chooMiddleware
/**
* A choo middleware for redux that propagates redux events through nanobus (the choo event emitter)
*/
function chooMiddleware (app) {
const events = Object.keys(app._events).map((e) => app._events[e])
return function chooReduxDispatcher ({ getState }) {
return next => action => {
if (~events.indexOf(action.type)) {
app.emitter.emit(action.type, action.payload)
}
if (!action.render) {
return next(action)
}
const result = next(action)
app.emitter.emit(RENDER)
return result
}
}
}
module.exports.patchRouter = patchRouter
/**
* Path the router view and use choo state combined with redux state
* The `emit` argument in the view is replaced by redux dispatch function
*/
function patchRouter (app, store) {
const route = app.route
app.route = function reduxRoute(path, view) {
return route.call(app, path, function reduxView(state, emit) {
return view(Object.assign({}, state, store.getState()), store.dispatch)
})
}
}