From dc840059a46e483184a43df809bb3c67b995726d Mon Sep 17 00:00:00 2001 From: Yoshua Wuyts Date: Mon, 23 May 2016 14:40:52 +0900 Subject: [PATCH] models: fix state namespace --- README.md | 8 +++++++- examples/http/models/api.js | 3 +-- examples/http/models/error.js | 2 +- examples/http/server.js | 2 +- examples/mailbox/server.js | 2 +- examples/server/server.js | 34 +++++++++++++++++++++++----------- index.js | 11 +++++------ site/server.js | 2 +- 8 files changed, 40 insertions(+), 24 deletions(-) diff --git a/README.md b/README.md index fa0a6e9..647c279 100644 --- a/README.md +++ b/README.md @@ -88,7 +88,6 @@ document.body.appendChild(tree) ``` ## Concepts -`choo` is a complete framework. It has an answer to pretty most points - __user:__ 🙆 - __DOM:__ the [Document Object Model][dom] is what is currently displayed in your browser @@ -417,6 +416,13 @@ Also imagine telling some business people you chose to rewrite something critical to the company using the `choo` framework. :steam_locomotive::train::train::train: +### Why is it a framework, and not a library? +I love small libraries that do one thing well, but when working in a team, +having an undocumented combination of packages often isn't great. `choo()` is a +small set of packages that work well together, wrapped in an an architectural +pattern. This means you get all the benefits of small packages, but get to be +productive right from the start. + ### How does choo compare to X? Ah, so this is where I get to rant. `choo` (_chugga-chugga-chugga-choo-choo!_) was built because other options didn't quite cut it for me, so instead of diff --git a/examples/http/models/api.js b/examples/http/models/api.js index 73daf8a..65be0db 100644 --- a/examples/http/models/api.js +++ b/examples/http/models/api.js @@ -14,7 +14,7 @@ module.exports = { } } -function request (uri, send) { +function request (uri, send, state) { http(uri, { json: true }, function (err, res, body) { if (err) return send('app:error', { payload: 'HTTP error' }) if (res.statusCode !== 200) { @@ -24,7 +24,6 @@ function request (uri, send) { return send('app:error', { payload: message }) } if (!body) { - console.log('req made!') return send('app:error', { payload: 'fatal: no body received' }) } send('api:set', { payload: body.message || body.title }) diff --git a/examples/http/models/error.js b/examples/http/models/error.js index 7d05ce5..632d0e5 100644 --- a/examples/http/models/error.js +++ b/examples/http/models/error.js @@ -34,7 +34,7 @@ module.exports = { }, effects: { error: function (action, state, send) { - const timeout = state.app.errorTimeDone - Date.now() + const timeout = state.errorTimeDone - Date.now() setTimeout(function () { send('app:error:delete') }, timeout) diff --git a/examples/http/server.js b/examples/http/server.js index c98ca55..b0da21e 100644 --- a/examples/http/server.js +++ b/examples/http/server.js @@ -6,7 +6,7 @@ const http = require('http') const PORT = 8080 const server = http.createServer(createRouter()) -server.listen(PORT, () => console.log(`listening on port ${PORT}`)) +server.listen(PORT, () => process.stdout.write(`listening on port ${PORT}\n`)) var index = 0 const errors = [ diff --git a/examples/mailbox/server.js b/examples/mailbox/server.js index 85d6517..ebf3755 100644 --- a/examples/mailbox/server.js +++ b/examples/mailbox/server.js @@ -6,7 +6,7 @@ const http = require('http') const PORT = 8080 const server = http.createServer(createRouter()) -server.listen(PORT, () => console.log(`listening on port ${PORT}`)) +server.listen(PORT, () => process.stdout.write(`listening on port ${PORT}\n`)) function createRouter () { const router = serverRouter('/404') diff --git a/examples/server/server.js b/examples/server/server.js index 051d06c..93e138f 100644 --- a/examples/server/server.js +++ b/examples/server/server.js @@ -2,21 +2,33 @@ const serverRouter = require('server-router') const http = require('http') const PORT = 8080 - const client = require('./client') -const server = http.createServer(createRouter()) -server.listen(PORT, () => console.log(`listening on port ${PORT}`)) +// If an incoming request accepts "text/html", render the +// appropriate HTML. Else use the API server +const apiRouter = createRouter() +const server = http.createServer(function (req, res) { + if (/text\/html/.test(req.headers.accept)) handleHtml(req, res) + else apiRouter(req, res) +}) +server.listen(PORT, () => process.stdout.write(`listening on port ${PORT}\n`)) +// create a new router +// null -> fn function createRouter () { - const router = serverRouter('/404') - - router.on('/404', (req, res) => res.end('not found')) - router.on('/', function (req, res, params) { - const html = client.toString('/', { message: 'hello server!' }) - res.setHeader('Content-Type', 'text/html; charset=utf-8') - res.end(html) + const apiRouter = serverRouter('/404') + apiRouter.on('/404', (req, res) => res.end('not found')) + apiRouter.on('/', (req, res) => { + res.end(JSON.stringify({ routes: [ '/', '/404' ] })) }) + return apiRouter +} - return router +// render the client to string +// based on the requested url +// (obj, obj) -> null +function handleHtml (req, res) { + const html = client.toString(req.url, { message: 'hello server!' }) + res.setHeader('Content-Type', 'text/html; charset=utf-8') + res.end(html) } diff --git a/index.js b/index.js index 3e012dd..b054b7e 100644 --- a/index.js +++ b/index.js @@ -105,17 +105,16 @@ function choo () { const _reducers = ns ? reducers[ns] : reducers if (_reducers && _reducers[action.type]) { if (ns) { - const newState = reducers[ns][action.type](action, state[ns]) + const newState = _reducers[action.type](action, state[ns]) state[ns] = xtend(state[ns], newState) - } else { - state = xtend(state, reducers[action.type](action, state)) - } + } else state = xtend(state, reducers[action.type](action, state)) reducersCalled = true } const _effects = ns ? effects[ns] : effects if (_effects && _effects[action.type]) { - _effects[action.type](action, state, send) + if (ns) _effects[action.type](action, state[ns], send) + else _effects[action.type](action, state, send) effectsCalled = true } @@ -175,7 +174,7 @@ function appInit (opts) { return model // create a new subscription that modifies - // 'app:location' and push it to the subs + // 'app:location' and push it to be loaded // fn -> null function pushLocationSub (cb) { model.subscriptions.push(function (send) { diff --git a/site/server.js b/site/server.js index 85d6517..ebf3755 100644 --- a/site/server.js +++ b/site/server.js @@ -6,7 +6,7 @@ const http = require('http') const PORT = 8080 const server = http.createServer(createRouter()) -server.listen(PORT, () => console.log(`listening on port ${PORT}`)) +server.listen(PORT, () => process.stdout.write(`listening on port ${PORT}\n`)) function createRouter () { const router = serverRouter('/404')