Skip to content

Commit

Permalink
Add a summing pair of counters example
Browse files Browse the repository at this point in the history
  • Loading branch information
uniphil committed May 31, 2016
1 parent 7557c47 commit d8ece52
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 1 deletion.
47 changes: 47 additions & 0 deletions examples/sum-counters/Counter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import React from 'react';
import { Record } from 'immutable';
import { Union } from 'results';
import { component, Update } from '../../spindle';


const Model = Record({
value: 0,
});


const Msg = Union({
Increment: null,
Decrement: null,
});


const update = (msg, model) => Msg.match(msg, {
Increment: () => {
const newModel = model.update('value', v => v + 1);
return Update({
model: newModel,
emit: newModel.get('value'),
});
},

Decrement: () => {
const newModel = model.update('value', v => v - 1);
return Update({
model: newModel,
emit: newModel.get('value'),
});
},
});


const view = (model, BoundMsg) => (
<p>
<button onClick={BoundMsg.Decrement}>-</button>
{model.get('value')}
<button onClick={BoundMsg.Increment}>+</button>
</p>
);


export default component('Counter',
{ Model, Msg, update, view });
40 changes: 40 additions & 0 deletions examples/sum-counters/SummedPair.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import React from 'react';
import { Record } from 'immutable';
import { Union } from 'results';
import { component, Update } from '../../spindle';
import Counter from './Counter';


const Model = Record({
topValue: null,
bottomValue: null,
});


const Msg = Union({
TopValue: null,
BottomValue: null,
});


const update = (msg, model) => Msg.match(msg, {
TopValue: v =>
Update({ model: model.set('topValue', v) }),

BottomValue: v =>
Update({ model: model.set('bottomValue', v) }),
});


const view = (model, BoundMsg) => (
<div>
<p>here are some counters:</p>
<Counter onEmit={BoundMsg.TopValue} />
<Counter onEmit={BoundMsg.BottomValue} />
<p>their sum is: {model.get('topValue') + model.get('bottomValue')}</p>
</div>
);


export default component('SummedPair',
{ Model, Msg, update, view });
6 changes: 6 additions & 0 deletions examples/sum-counters/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import React from 'react';
import ReactDOM from 'react-dom';
import SummedPair from './SummedPair';


ReactDOM.render(<SummedPair />, document.getElementById('app'));
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
"build": "browserify app.js -t babelify --outfile bundle.js",
"watch-counter": "watchify examples/counter/app.js -d -t babelify --outfile bundle.js",
"watch-pair": "watchify examples/pair-of-counters/app.js -d -t babelify --outfile bundle.js",
"watch-n": "watchify examples/n-counters/app.js -d -t babelify --outfile bundle.js"
"watch-n": "watchify examples/n-counters/app.js -d -t babelify --outfile bundle.js",
"watch-sum": "watchify examples/sum-counters/app.js -d -t babelify --outfile bundle.js"
},
"author": "uniphil",
"license": "GPL",
Expand Down

0 comments on commit d8ece52

Please sign in to comment.