Files
buuh/examples/http/models/error.js
T
Yoshua Wuyts 9e039ed561 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
2016-07-02 16:00:26 +02:00

53 lines
1.3 KiB
JavaScript

// Create a new error. Errors are pushed into an array so they can be
// queued. We're doing a bit of timestamp trickery here so each error
// is shown for exactly one second before being dropped. Probably
// useful for real world scenarios; especially when coupled to
// fancy CSS animation thingies
// Each error is displayed 1 second
const ERROR_TIMEOUT = 1000
module.exports = {
namespace: 'app',
state: {
errors: [],
errorTimeDone: 0,
triggerTime: null
},
reducers: {
setError: function (action, state) {
return {
errors: state.errors.concat(action.message),
errorTimeDone: action.errorTimeDone
}
},
'delError': function (action, state) {
state.errors.shift()
return { errors: state.errors }
}
},
effects: {
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)
})
}
}
}