git ssb

7+

dinoworm ๐Ÿ› / patchcore



Tree: 781e1c19862d0d3327dd9a68e0c497d9f876cc0f

Files: 781e1c19862d0d3327dd9a68e0c497d9f876cc0f / docs / router.md

1302 bytesRaw

Router

Adding routes

Patchcore collects routes from router.sync.routes as a reduce. It expects the final routes collection to be an array of arrays of the form:

  [ routeValidator, routeFunction ]

Where routeValidator is a function that returns true / false when given a location object.

Here's a simple example of extending the routes

exports.create = (api) => {
  return { router: { sync: { routes } } }

  function routes (sofar = []) {
    const moreRoutes = [
      [ (location) => location.page === 'home',  api.app.page.home ],
      [ (location) => location.type === 'group', api.app.page.group ],
      [ ()         => true,                      api.app.page.notFound ]
    ]

    return [...moreRoutes, ...sofar]
    // Note order matters here
  }
}

Using the router

The router is accessible at app.sync.router, and can be used like :

const location = { page: 'inbox', theme: 'dark' }
const newView = api.app.sync.router(location)

The router finds the first route which matches the location it is passed, then automatically calls the associated routeFunction with the location object as an argument.

In our example the route is generating a view, which we might insert / append to the DOM, but this doesn't have to be the case.

Built with git-ssb-web