git ssb

7+

dinoworm ๐Ÿ› / patchcore



Tree: 9b421343eb76d9c775a0f125bdc96770f966c4ea

Files: 9b421343eb76d9c775a0f125bdc96770f966c4ea / router / sync / router.js

717 bytesRaw
1const nest = require('depnest')
2
3exports.gives = nest('router.sync.router')
4
5exports.needs = nest({
6 'router.sync.normalise': 'first',
7 'router.sync.routes': 'reduce'
8})
9
10exports.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
24function 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