router: change order of arguments

fixup! freeze state & rename app ns to location
This commit is contained in:
Yoshua Wuyts
2016-07-02 22:54:35 +02:00
parent c679e37d3a
commit 6407188ee9
15 changed files with 85 additions and 50 deletions
+53 -21
View File
@@ -16,9 +16,11 @@ module.exports = choo
function choo (opts) {
opts = opts || {}
const _store = barracks(xtend(opts, { onState: render }))
const _store = start._store = barracks(xtend(opts, { onState: render }))
var _router = start._router = null
var _defaultRoute = null
var _rootNode = null
var _router = null
var _routes = null
start.toString = toString
start.router = router
@@ -34,11 +36,17 @@ function choo (opts) {
assert.equal(typeof route, 'string', 'choo.app.toString: route must be a string')
assert.equal(typeof serverState, 'object', 'choo.app.toString: serverState must be an object')
_store.start({ noSubscriptions: true, noReducers: true, noEffects: true })
const state = _store.state({ state: serverState })
const tree = _router(route, state, function () {
assert.fail('choo: send() cannot be called from Node')
})
const router = createRouter(_defaultRoute, _routes, createSend)
const tree = router(route, state)
return tree.toString()
function createSend () {
return function send () {
assert.fail('choo: send() cannot be called from Node')
}
}
}
// start the application
@@ -52,18 +60,18 @@ function choo (opts) {
_store.model(appInit(startOpts))
const createSend = _store.start(startOpts)
const send = createSend('view', true)
_router = start._router = createRouter(_defaultRoute, _routes, createSend)
const state = _store.state()
if (!selector) {
const tree = _router(state.app.location, state, send)
const tree = _router(state.location.pathname, state)
_rootNode = tree
return tree
} else {
document.addEventListener('DOMContentLoaded', function (event) {
const oldTree = document.querySelector(selector)
assert.ok(oldTree, 'could not query selector: ' + selector)
const newTree = _router(state.app.location, state, send)
const newTree = _router(state.location.pathname, state)
_rootNode = yo.update(oldTree, newTree)
})
}
@@ -75,17 +83,15 @@ function choo (opts) {
if (opts.onState) opts.onState(action, state, prev, name, createSend)
if (state === prev) return
// note(yw): only here till sheet-router supports custom constructors
const send = createSend('view', true)
const newTree = _router(state.app.location, state, send, prev)
const newTree = _router(state.location.pathname, state, prev)
_rootNode = yo.update(_rootNode, newTree)
}
// register all routes on the router
// (str?, [fn|[fn]]) -> obj
function router (defaultRoute, cb) {
_router = sheetRouter(defaultRoute, cb)
return _router
function router (defaultRoute, routes) {
_defaultRoute = defaultRoute
_routes = routes
}
// create a new model
@@ -93,16 +99,42 @@ function choo (opts) {
function model (model) {
_store.model(model)
}
// create a new router with a custom `createRoute()` function
// (str?, obj, fn?) -> null
function createRouter (defaultRoute, routes, createSend) {
var prev = {}
return sheetRouter(defaultRoute, routes, createRoute)
function createRoute (routeFn) {
return function (route, inline, child) {
if (typeof inline === 'function') {
inline = wrap(inline, route)
}
return routeFn(route, inline, child)
}
function wrap (child, route) {
const send = createSend(route, true)
return function chooWrap (params, state) {
const nwPrev = prev
const nwState = prev = xtend(state, { params: params })
if (!opts.noFreeze) Object.freeze(nwState)
return child(nwState, nwPrev, send)
}
}
}
}
}
// initial application state model
// obj -> obj
function appInit (opts) {
const loc = document.location
const state = { location: (opts.hash) ? hashMatch(loc.hash) : loc.href }
const state = { pathname: (opts.hash) ? hashMatch(loc.hash) : loc.href }
const reducers = {
location: function setLocation (action, state) {
return { location: action.location.replace(/#.*/, '') }
setLocation: function setLocation (action, state) {
return { pathname: action.location.replace(/#.*/, '') }
}
}
// if hash routing explicitly enabled, subscribe to it
@@ -114,12 +146,12 @@ function appInit (opts) {
})
}, 'handleHash', subs)
} else {
if (opts.history !== false) pushLocationSub(history, 'setLocation', subs)
if (opts.history !== false) pushLocationSub(history, 'handleHistory', subs)
if (opts.href !== false) pushLocationSub(href, 'handleHref', subs)
}
return {
namespace: 'app',
namespace: 'location',
subscriptions: subs,
reducers: reducers,
state: state
@@ -130,8 +162,8 @@ function appInit (opts) {
// (fn, obj) -> null
function pushLocationSub (cb, key, model) {
model[key] = function (send, done) {
cb(function navigate (href) {
send('app:location', { location: href }, done)
cb(function navigate (pathname) {
send('location:setLocation', { location: pathname }, done)
})
}
}