git ssb

0+

jake / example-choov5-app



Commit 9b57375c6af220c424e3828408e8864e2d508eae

initial commit with test

jekrb committed on 3/15/2017, 3:30:26 AM

Files changed

index.jsadded
package.jsonadded
server.jsadded
test.jsadded
index.jsView
@@ -1,0 +1,36 @@
1 +var choo = require('choo')
2 +var html = require('choo/html')
3 +
4 +if (module.parent) {
5 + exports.trackClicks = trackClicks
6 +} else {
7 + var app = choo()
8 + app.route('/', mainView)
9 + app.use(trackClicks())
10 + app.mount('body')
11 +}
12 +
13 +function mainView (state, emit) {
14 + return html`
15 + <body>
16 + <h1>${state.clicks}</h1>
17 + <button onclick=${onclick}>click me</button>
18 + </body>
19 + `
20 +
21 + function onclick () {
22 + emit('clicked')
23 + }
24 +}
25 +
26 +function trackClicks () {
27 + return function (state, emitter) {
28 + state.clicks = 0
29 +
30 + emitter.on('clicked', function () {
31 + state.clicks++
32 + emitter.emit('render')
33 + })
34 + }
35 +}
36 +
package.jsonView
@@ -1,0 +1,24 @@
1 +{
2 + "name": "example-choov5-app",
3 + "version": "1.0.0",
4 + "description": "",
5 + "main": "index.js",
6 + "scripts": {
7 + "build": "browserify index.js > bundle.js",
8 + "test": "tape test.js"
9 + },
10 + "keywords": [],
11 + "author": "",
12 + "license": "ISC",
13 + "dependencies": {
14 + "browserify": "^14.1.0",
15 + "choo": "^5.0.0-5",
16 + "split2": "^2.1.1",
17 + "spok": "^0.7.0",
18 + "through2": "^2.0.3",
19 + "websocket-stream": "^3.3.3"
20 + },
21 + "devDependencies": {
22 + "tape": "^4.6.3"
23 + }
24 +}
server.jsView
@@ -1,0 +1,46 @@
1 +var readStream = require('fs').createReadStream
2 +var createServer = require('http').createServer
3 +var ws = require('websocket-stream')
4 +var through = require('through2')
5 +var split = require('split2')
6 +
7 +var server = createServer(function (req, res) {
8 + if (req.url === '/bundle.js') readStream('bundle.js').pipe(res)
9 +
10 + else res.end(`
11 + <!doctype html>
12 + <meta charset='utf8'>
13 + <body>
14 + <script src='bundle.js'></script>
15 + </body>
16 + `)
17 +})
18 +
19 +server.listen(9090, function () {
20 + console.log('http://localhost:9090')
21 +})
22 +
23 +
24 +// do websocket stuff
25 +// var wsServer = ws({ server: server}, connection)
26 +//
27 +// function connection (stream) {
28 +// var sp = stream.pipe(split(JSON.parse))
29 +//
30 +// sp.on('error', function (err) {
31 +// console.error(err)
32 +// })
33 +//
34 +// sp.pipe(through.obj(write))
35 +//
36 +//
37 +// function write (row, enc, next) {
38 +// if (!row) next()
39 +//
40 +// if (row.count) {
41 +// // just echo the count back to the client
42 +// stream.write(row)
43 +// }
44 +// }
45 +// }
46 +
test.jsView
@@ -1,0 +1,30 @@
1 +var EventEmitter = require('events').EventEmitter
2 +var spok = require('spok')
3 +var test = require('tape')
4 +
5 +var app = require('./')
6 +var trackClicks = app.trackClicks()
7 +
8 +test('should init empty state', function (t) {
9 + var emitter = new EventEmitter()
10 + var state = {}
11 + trackClicks(state, emitter)
12 + spok(t, state, {})
13 +
14 + t.end()
15 +})
16 +
17 +test('should increase clicks count to 100', function (t) {
18 + var emitter = new EventEmitter()
19 + var state = {}
20 + trackClicks(state, emitter)
21 +
22 + for (var i = 0; i !== 100; i++) {
23 + emitter.emit('clicked')
24 + }
25 +
26 + spok(t, state.clicks, 100)
27 +
28 + t.end()
29 +})
30 +

Built with git-ssb-web