Files: 2fa8e6a002f4394a9059d775c6b7e5655de35e76 / router / sync / router.js
717 bytesRaw
1 | const nest = require('depnest') |
2 | |
3 | exports.gives = nest('router.sync.router') |
4 | |
5 | exports.needs = nest({ |
6 | 'router.sync.normalise': 'first', |
7 | 'router.sync.routes': 'reduce' |
8 | }) |
9 | |
10 | exports.create = (api) => { |
11 | var _router = null |
12 | const { routes, normalise } = api.router.sync |
13 | |
14 | return nest('router.sync.router', (location) => { |
15 | if (!_router) { |
16 | _router = buildRouter(routes()) |
17 | } |
18 | |
19 | const locationObject = normalise(location) |
20 | return _router(locationObject) |
21 | }) |
22 | } |
23 | |
24 | function buildRouter (routes) { |
25 | return (location) => { |
26 | const route = routes.find(([validator]) => validator(location)) |
27 | // signature of a route is [ routeValidator, routeFunction ] |
28 | |
29 | if (route) return route[1](location) |
30 | } |
31 | } |
32 | |
33 |
Built with git-ssb-web