Last active
August 29, 2015 14:19
-
-
Save ishiduca/956ee21635bfc40537e7 to your computer and use it in GitHub Desktop.
event-streamでflux実装。エラー処理も
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict' | |
var es = require('event-stream') | |
var assign = require('object-assign') | |
// dispatcher | |
var dispatcher = es.map(function (payload, cb) { | |
cb(null, payload) | |
}) | |
// actions | |
var act = assign(es.map(function (payload, cb) { | |
cb(null, payload) | |
}), { | |
add: function () { this.write({actionType: 'calc', value: 1}) } | |
, take: function () { this.write({actionType: 'calc', value: -1}) } | |
}) | |
var actErrorCenter = assign(es.map(function (payload, cb) { | |
cb(null, payload) | |
}), { | |
error: function (err, _actionType) { | |
this.write({actionType: _actionType || err.name, value: err}) | |
} | |
}) | |
// stores | |
var value = 0 | |
var store = es.map(function (payload, cb) { | |
if ('calc' !== payload.actionType) return cb() | |
var _value = value + payload.value | |
if (_value < 0) { | |
var err = new Error('value must be over "0"') | |
err.name = 'CalcError' | |
actErrorCenter.error(err) | |
return cb() | |
} | |
cb(null, (value = _value)) | |
}) | |
var storeErrorCenter = es.map(function (payload, cb) { | |
if ('CalcError' !== payload.actionType) return cb() | |
cb(null, payload.value) | |
}) | |
// view(component) | |
var view = es.map(function (value, cb) { | |
view.view.innerHTML = String(value) | |
cb() | |
}) | |
var viewErrorCenter = es.map(function (err, cb) { | |
viewErrorCenter.view.innerHTML = String(err) | |
setTimeout(function () { | |
viewErrorCenter.view.innerHTML = '' | |
}, 2000) | |
cb() | |
}) | |
window.onload = function () { | |
var $add = document.querySelector('#add') | |
var $take = document.querySelector('#take') | |
view.view = document.querySelector('#view') | |
viewErrorCenter.view = document.querySelector('#errors') | |
$add.onclick = act.add.bind(act) | |
$take.onclick = act.take.bind(act) | |
es.merge( act, actErrorCenter ).pipe(dispatcher) | |
dispatcher.pipe(store).pipe(view) | |
dispatcher.pipe(storeErrorCenter).pipe(viewErrorCenter) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<body> | |
<button type="button" id="add" >+</button> || | |
<button type="button" id="take">-</button> | |
<div id="view">--</div> | |
<div id="errors"></div> | |
<script src="./bundle.js"></script> | |
</body> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "flux-on-stream", | |
"dependencies": { | |
"event-stream": "^3.3.0", | |
"object-assign": "^2.0.0" | |
}, | |
"scripts": { | |
"build": "browserify ./flux-on-stream.js -o ./bundle.js" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
雑な実装を少しだけ直した。具体的には act にメソッドを生やして、
.write
をラップした。将来的に他の計算式が加わった場合、actionType に
calc
以外のものが加わる可能性があるので。