architecture: use barracks

Changes
=======
- add hook handlers
- add effect composition
- add noFreeze option

Commits
=======
- fixup! fix toString()
- fixup! use assert instead of throw
- fixup! fix reducers
- fixup! work on done callbacks
- fixup! more callback work
- fixup! woooh fixed effects errs
- fixup! wrap up stuff
This commit is contained in:
Yoshua Wuyts
2016-07-02 16:00:26 +02:00
parent 8f8df602c5
commit 9e039ed561
10 changed files with 146 additions and 218 deletions
+28 -19
View File
@@ -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)
})
}
}
}