Skip to content

Commit

Permalink
Merge pull request #104 from yoshuawuyts/use-barracks
Browse files Browse the repository at this point in the history
architecture: use barracks
  • Loading branch information
yoshuawuyts authored Jul 2, 2016
2 parents 8f8df60 + 9e039ed commit c679e37
Show file tree
Hide file tree
Showing 10 changed files with 145 additions and 217 deletions.
21 changes: 20 additions & 1 deletion examples/http/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,26 @@ const choo = require('../../')

const mainView = require('./views/main')

const app = choo()
const app = choo({
onError: function (err, state, createSend) {
console.groupCollapsed(`Error: ${err.message}`)
console.error(err)
console.groupEnd()
const send = createSend('onError: ')
send('app:error', err)
},
onAction: function (action, state, name, caller, createSend) {
console.groupCollapsed(`Action: ${caller} -> ${name}`)
console.log(action)
console.groupEnd()
},
onState: function (action, state, prev, createSend) {
console.groupCollapsed('State')
console.log(prev)
console.log(state)
console.groupEnd()
}
})

app.model(require('./models/error'))
app.model(require('./models/api'))
Expand Down
20 changes: 10 additions & 10 deletions examples/http/models/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,26 @@ module.exports = {
title: 'Button pushing machine 3000'
},
reducers: {
set: (action, state) => ({ 'title': action.payload })
set: (action, state) => ({ 'title': action.data })
},
effects: {
good: (action, state, send) => request('/good', send),
bad: (action, state, send) => request('/bad', send)
good: function (action, state, send, done) {
request('/good', send, done)
},
bad: (action, state, send, done) => request('/bad', send, done)
}
}

function request (uri, send, state) {
function request (uri, send, done) {
http(uri, { json: true }, function (err, res, body) {
if (err) return send('app:error', { payload: 'HTTP error' })
if (err) return done(new Error('HTTP error'))
if (res.statusCode !== 200) {
const message = (body && body.message)
? body.message
: 'unknown server error'
return send('app:error', { payload: message })
return done(new Error(message))
}
if (!body) {
return send('app:error', { payload: 'fatal: no body received' })
}
send('api:set', { payload: body.message || body.title })
if (!body) return done(new Error('fatal: no body received'))
send('api:set', { data: body.message || body.title }, done)
})
}
47 changes: 28 additions & 19 deletions examples/http/models/error.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,34 +10,43 @@ const ERROR_TIMEOUT = 1000
module.exports = {
namespace: 'app',
state: {
error: [],
errorTimeDone: null,
errors: [],
errorTimeDone: 0,
triggerTime: null
},
reducers: {
error: function (action, state) {
const now = Date.now()
const timeDone = state.errorTimeDone
const newTimestamp = (timeDone && timeDone >= now)
? timeDone + ERROR_TIMEOUT
: now + ERROR_TIMEOUT

setError: function (action, state) {
return {
error: state.error.concat(action.payload),
errorTimeDone: newTimestamp
errors: state.errors.concat(action.message),
errorTimeDone: action.errorTimeDone
}
},
'error:delete': function (action, state) {
state.error.shift()
return { error: state.error }
'delError': function (action, state) {
state.errors.shift()
return { errors: state.errors }
}
},
effects: {
error: function (action, state, send) {
const timeout = state.errorTimeDone - Date.now()
setTimeout(function () {
send('app:error:delete')
}, timeout)
error: function (err, state, send, done) {
const timeDone = state.errorTimeDone
const now = Date.now()

const timeStamp = (timeDone && timeDone >= now)
? timeDone + ERROR_TIMEOUT
: now + ERROR_TIMEOUT

const timeout = timeStamp - now

const errAction = {
message: err.message,
errorTimeDone: timeStamp
}
send('app:setError', errAction, function (err) {
if (err) return done(err)
setTimeout(function () {
send('app:delError', done)
}, timeout)
})
}
}
}
2 changes: 1 addition & 1 deletion examples/http/views/main.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const html = require('../../../html')

module.exports = function (params, state, send) {
const error = state.app.error[0]
const error = state.app.errors[0]
const title = state.api.title
return html`
<section>
Expand Down
1 change: 0 additions & 1 deletion examples/mailbox/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ const choo = require('../../')
const sf = require('sheetify')

sf('css-wipe/dest/bundle')
sf('tachyons')

const app = choo()

Expand Down
12 changes: 7 additions & 5 deletions examples/mailbox/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,18 @@
"scripts": {
"start": "NODE_ENV=development node server.js"
},
"browserify": {
"transform": [
"sheetify/transform"
]
},
"author": "Yoshua Wuyts <i@yoshuawuyts.com>",
"license": "ISC",
"dependencies": {
"css-wipe": "^4.2.1",
"dateformat": "^1.0.12",
"tachyons": "^4.0.0-beta.33"
},
"devDependencies": {
"bankai": "^2.0.5",
"browserify": "^13.0.1",
"insert-css": "^0.2.0",
"server-router": "^2.1.0",
"sheetify": "^5.0.3"
}
}
15 changes: 15 additions & 0 deletions examples/title/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"name": "title",
"version": "1.0.0",
"description": "",
"main": "client.js",
"scripts": {
"start": "budo client.js -p 8080"
},
"keywords": [],
"author": "Yoshua Wuyts <i@yoshuawuyts.com>",
"license": "ISC",
"dependencies": {
"budo": "^8.3.0"
}
}
Loading

0 comments on commit c679e37

Please sign in to comment.