Last active
May 15, 2020 01:31
-
-
Save ishiduca/9bcdbf2bf291f1b668c9bbc01a6fda4a to your computer and use it in GitHub Desktop.
JSON-RPC2.0 ライブラリーのユーザーインターフェイスはどう設計すればいいのか?
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
module.exports = { upper } | |
function upper (params, cb) { | |
var paraph = params.reduce((a, b) => a.concat(b.split(' ')), []).filter(Boolean) | |
var result = paraph.map(a => a.slice(0, 1).toUpperCase() + a.slice(1)).join(' ') | |
cb(null, result) | |
} |
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
var document = require('global/document') | |
var { pipeline, through } = require('mississippi') | |
var bind = require('../') | |
var api = require('./api-callback') | |
var schemas = require('./schemas') | |
var { prefix } = require('./config') | |
var rpc = bind(prefix, { api, schemas }) | |
var xtend = require('xtend') | |
var yo = require('yo-yo') | |
var { start } = require('@ishiduca/snoopy') | |
var root = document.createElement('div') | |
var app = { | |
init () { | |
return { model: { org: '', result: '' } } | |
}, | |
update (model, action) { | |
if (action == null) return { model } | |
if (action.upper != null) { | |
return { | |
model: xtend(model, { org: action.upper }), | |
effect: { upper: [ action.upper ] } | |
} | |
} | |
if (action.result != null) { | |
return { | |
model: xtend(model, action) | |
} | |
} | |
return { model } | |
}, | |
run (effect, sources) { | |
if (effect == null) return | |
if (effect.upper != null) { | |
return pipeline.obj( | |
rpc.upper(effect.upper), | |
through.obj((result, _, done) => done(null, { result })) | |
) | |
} | |
}, | |
view (model, actionsUp) { | |
return yo` | |
<div> | |
<div> | |
<label for="org">original:</label> | |
<input | |
type="text" | |
id="org" | |
value=${model.org} | |
size=30 | |
oninput=${e => actionsUp({ upper: e.target.value })} | |
/> | |
</div> | |
<div><p>result: <b>${model.result}</b></p></div> | |
</div> | |
` | |
} | |
} | |
var { views, actions, models } = start(app) | |
actions().on('data', action => console.log({ action })) | |
models().on('data', model => console.log({ model })) | |
views().on('data', rt => yo.update(root, rt)) | |
document.body.appendChild(root) |
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
{ | |
"prefix": "/rpc/v1.0" | |
} |
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
{ | |
"upper": { | |
"type": "array", | |
"items": { | |
"type": "string" | |
} | |
} | |
} |
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
var http = require('http') | |
var path = require('path') | |
var ecstatic = require('ecstatic')(path.join(__dirname, 'static')) | |
var bind = require('@ishiduca/snoopy-rpc') | |
var api = require('./api-callback') | |
var schemas = require('./schemas') | |
var { prefix } = require('./config') | |
var middle = bind(prefix, { api, schemas }) | |
module.exports = http.createServer(middle(ecstatic)) |
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
#!/usr/bin/env node | |
var app = require('./server') | |
var port = process.env.PORT || 6969 | |
var msg = `server start to listen on port "${port}"` | |
app.listen(port, () => console.log(msg)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment