From 5f68a8bf3a2f7e121db44df7658aef60895cd539 Mon Sep 17 00:00:00 2001 From: Yoshua Wuyts Date: Wed, 11 May 2016 14:56:18 +0700 Subject: [PATCH] choo: fix updates --- README.md | 6 +++--- examples/title.js | 6 +++--- index.js | 36 +++++++++++++++++++++++++----------- 3 files changed, 31 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index 2b06205..272cd86 100644 --- a/README.md +++ b/README.md @@ -27,7 +27,7 @@ app.model('title', { 'update': (action, state) => ({ title: action.payload }) }, effects: { - 'update': (action, state, send) => (document.title = action.title) + 'update': (action, state, send) => (document.title = action.payload) } }) @@ -46,8 +46,8 @@ app.router((route) => [ route('/', mainView) ]) -const node = app.start() -document.body.appendChild(node) +const tree = app.start() +document.body.appendChild(tree) ``` ## Concepts diff --git a/examples/title.js b/examples/title.js index e415232..d1d2274 100644 --- a/examples/title.js +++ b/examples/title.js @@ -7,7 +7,7 @@ app.model('title', { 'update': (action, state) => ({ title: action.payload }) }, effects: { - 'update': (action, state, send) => (document.title = action.title) + 'update': (action, state, send) => (document.title = action.payload) } }) @@ -26,5 +26,5 @@ app.router((route) => [ route('/', mainView) ]) -const node = app.start() -document.body.appendChild(node) +const tree = app.start() +document.body.appendChild(tree) diff --git a/index.js b/index.js index 3b328e3..add9550 100644 --- a/index.js +++ b/index.js @@ -23,8 +23,8 @@ function choo () { return start // start the application - // str|DOMNode -> DOMNode - function start (query) { + // null -> DOMNode + function start () { const events = bootstrap(_models) const send = sendAction({ onaction: events.middleware, @@ -32,14 +32,16 @@ function choo () { state: events.state }) - const node = _router(send.state().location, send.state(), send) - console.log(node) - document.body.appendChild(node) + const tree = _router(send.state().location, send.state(), send) + tree.setAttribute('id', 'choo-root') + return tree // update on every change function onchange (action, state) { - const el = document.querySelector('#main') - yo.update(el, router(state.location, send, state)) + const oldTree = document.querySelector('#choo-root') + const newTree = _router(state.location, state, send) + newTree.setAttribute('id', 'choo-root') + yo.update(oldTree, newTree) } } @@ -109,13 +111,25 @@ function bootstrap (events) { } function modifyState (action, state, send) { + var _reducers = false + var _effects = false + var newState = null + if (reducers[action.type]) { - return reducers[action.type](action, state, send) - } else if (effects[action.type]) { + newState = xtend(state, reducers[action.type](action, state)) + _reducers = true + } + + if (effects[action.type]) { effects[action.type](action, state, send) - return state - } else { + newState = newState || state + _effects = true + } + + if (!_reducers && !_effects) { throw new Error('Could not find action ' + action.type) } + + return newState } }