ahdinosaur / inu

:dog2: composable unidirectional user interfaces using pull streams
207 stars 12 forks source link

add inu.router using sheet-router #12

Closed ahdinosaur closed 8 years ago

ahdinosaur commented 8 years ago

api in mind, heavily stolen from choo:

const inu = require('inu')

const routes = [
  ['/', (params) => inu.html`<div>Welcome to router land!</div>`],
  ['/:username', (params) => inu.html`<div>${params.username}</div>`, [
    ['/orgs', (params) => inu.html`<div>${params.username}'s orgs!</div>'`]
  ]
]
const routerApp = inu.router(routes, {
  href: true, // default
  history: true // default
})

where routerApp is something like:

const routerApp = {
  namespace: 'router',
  init: () => ({
    model: { location: document.location.href },
    effect: 'ROUTER_INIT'
  }),
  update: (model, action) => {
    switch (action.type) {
      case 'SET_LOCATION':
        return { location: action.payload.replace(/#.*/, '') }
      default:
        return state
    }
  },
  run: (effect, actions) => {
    if (effect !== 'ROUTER_INIT') { return }
    var effectActions = pushable(function onClose (error) {
      // cleanup href and/or history
    })
    // enable catching <href a=""></href> links
    if (opts.href !== false) href(push)
    // enable HTML5 history API
    if (opts.history !== false) history(push)
    return effectActions

    function push (href) {
      effectActions.push({
        type: 'SET_LOCATION',
        payload: href
      })
    }
  }
}

references: sheet-router, choo, tom

ahdinosaur commented 8 years ago

need to support https://github.com/yoshuawuyts/choo/issues/1#issuecomment-222120071, probably as a check to within routerApp.view to see if it's a new route and if so dispatch a 'load' action for that route. or something.

ahdinosaur commented 8 years ago

initial release done in inux, we'll see how the experiment goes. :rocket: