Skip to content

Instantly share code, notes, and snippets.

@nmorse
Last active May 15, 2017 01:14
Show Gist options
  • Save nmorse/b773a605f274f6dae7ef3683def2e3a7 to your computer and use it in GitHub Desktop.
Save nmorse/b773a605f274f6dae7ef3683def2e3a7 to your computer and use it in GitHub Desktop.
tried cycle.js for the first time (on the http://widdersh.in/tricycle/ site) starting with the basic demo. I updated the single increment example to add a subtract button, fun to be functional in js and use streams easily.
import {run} from '@cycle/xstream-run';
import {makeDOMDriver, div, button} from '@cycle/dom';
import _ from 'lodash';
import xs from 'xstream';
function main ({DOM}) {
const add$ = DOM
.select('.add')
.events('click')
.map(ev => 1);
const sub$ = DOM
.select('.sub')
.events('click')
.map(ev => -1);
const action$ = xs.merge(add$, sub$);
// const count$ = action$.fold((x, y) => x + y, 0);
const count$ = action$
.fold((total, change) => total + change, 0);
return {
DOM: count$.map(count =>
div('.counter', [
'Counts: ' + count,
button('.add', 'Add'),
button('.sub', 'Sub')
])
)
};
}
const sources = {
DOM: makeDOMDriver('.app')
}
// Normally you need to call Cycle.run, but Tricycle handles that for you!
// If you want to try this out locally, just uncomment this code.
//
// Cycle.run(main, sources);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment