Files: dd6a279d35c61c3c1a36d453b6e27af326a3fc67 / router / async / router.js
801 bytesRaw
1 | const nest = require('depnest') |
2 | |
3 | exports.gives = nest('router.async.router') |
4 | |
5 | exports.needs = nest({ |
6 | 'router.async.normalise': 'first', |
7 | 'router.sync.routes': 'reduce' |
8 | }) |
9 | |
10 | exports.create = (api) => { |
11 | var router = null |
12 | |
13 | return nest('router.async.router', (location, cb) => { |
14 | if (!router) { |
15 | router = Router(api.router.sync.routes()) |
16 | } |
17 | |
18 | api.router.async.normalise(location, (err, normLocation) => { |
19 | if (err) return cb(err) |
20 | |
21 | router(normLocation, cb) |
22 | }) |
23 | |
24 | // stop depject 'first' after this method |
25 | return true |
26 | }) |
27 | } |
28 | |
29 | function Router (routes) { |
30 | return (location, cb) => { |
31 | const route = routes.find(([validator]) => validator(location)) |
32 | // signature of a route is [ routeValidator, routeFunction ] |
33 | |
34 | if (route) cb(null, route[1](location)) |
35 | } |
36 | } |
37 |
Built with git-ssb-web