This repository has been archived by the owner on Feb 16, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
routing.js
77 lines (66 loc) · 1.64 KB
/
routing.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
import React from 'react'
import { Rx } from 'tom'
import { useQueries } from 'history'
import createHistory from 'history/lib/createHashHistory'
import Router from 'tom/lib/Router'
const history = useQueries(createHistory)({ queryKey: false })
const router = new Router([
{ path: '/', handler: ({ history: h }) => h.replace('/user?a=1') },
{ path: '/user', handler: request => <A request={request} /> },
{ path: '/orders/:orderId', handler: request => <B request={request} /> }
], history)
export default {
init() {
return {
model: { location: history.createLocation(location.hash.substring(1)) },
effect: { type: 'MOUNT_ROUTER' }
}
},
update(model, event) {
switch (event.type) {
case 'ROUTING' :
return {
model: {
location: event.location
}
}
default :
return { model }
}
},
view(model) {
return router.match(model.location)
},
run(effect) {
switch (effect.type) {
case 'MOUNT_ROUTER' :
return Rx.Observable.create(observer => {
history.listen(location => {
observer.onNext({ type: 'ROUTING', location })
})
})
}
}
}
class A extends React.Component {
render() {
return (
<div>
<p>Current view: A</p>
<pre>Request: {JSON.stringify(this.props.request, null, 2)}</pre>
<a href="#/orders/123?b=2">goto B</a>
</div>
)
}
}
class B extends React.Component {
render() {
return (
<div>
<p>Current view: B</p>
<pre>Request: {JSON.stringify(this.props.request, null, 2)}</pre>
<a href="#/user?a=1">goto A</a>
</div>
)
}
}