Skip to content

Instantly share code, notes, and snippets.

@ntilwalli
Last active November 18, 2016 20:15
Show Gist options
  • Save ntilwalli/843327c8fb502ef4b4b9ee9a2c29fc36 to your computer and use it in GitHub Desktop.
Save ntilwalli/843327c8fb502ef4b4b9ee9a2c29fc36 to your computer and use it in GitHub Desktop.
esnextbin sketch
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>ESNextbin Sketch</title>
<!-- put additional styles and scripts here -->
</head>
<body>
<div id=app></div>
<!-- put markup and other contents here -->
</body>
</html>
// write ES2015 code and import modules from npm
// and then press "Execute" to run your program
import { run } from '@cycle/xstream-run';
import { makeDOMDriver, div, button } from '@cycle/dom';
import xs from 'xstream';
import isolate from '@cycle/isolate';
function Component(sources) {
// expected: one console.log per component
// actual: more and more for every click
return {
DOM : sources.shared$
.debug(x => console.log(x))
.map(() => div('Component'))
}
}
function main(sources) {
// just a state stream used to create two components
const shared$ = sources.DOM
.select('button')
.events('click')
.map(_ => [1])
.startWith([1]);
// mapping over xs.of([1,2]) fixes the problem but I need to map over shared$ and would like to know
// why I get this behaviour
const sinks$ = shared$
.map(array => array.map(key => Component({ shared$, ...sources })))
const childDOM$ = sinks$
.map(array => array.map(sink => sink.DOM))
.map(array => xs.combine(...array))
.flatten();
const vdom$ = sinks$.map(childDOM => div([
div(childDOM[0].DOM),
button('Change Shared Data'),
div('PROBLEM: Why do I get more and more console.logs for every click on the button?')
]))
return {
DOM : vdom$
}
}
run(main, {
DOM : makeDOMDriver('#app'),
});
{
"name": "esnextbin-sketch",
"version": "0.0.0",
"dependencies": {
"@cycle/xstream-run": "3.1.0",
"@cycle/dom": "14.1.0",
"xstream": "8.0.0",
"@cycle/isolate": "1.4.0",
"babel-runtime": "6.18.0"
}
}
'use strict';
var _toConsumableArray2 = require('babel-runtime/helpers/toConsumableArray');
var _toConsumableArray3 = _interopRequireDefault(_toConsumableArray2);
var _extends2 = require('babel-runtime/helpers/extends');
var _extends3 = _interopRequireDefault(_extends2);
var _xstreamRun = require('@cycle/xstream-run');
var _dom = require('@cycle/dom');
var _xstream = require('xstream');
var _xstream2 = _interopRequireDefault(_xstream);
var _isolate = require('@cycle/isolate');
var _isolate2 = _interopRequireDefault(_isolate);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
// write ES2015 code and import modules from npm
// and then press "Execute" to run your program
function Component(sources) {
// expected: one console.log per component
// actual: more and more for every click
return {
DOM: sources.shared$.debug(function (x) {
return console.log(x);
}).map(function () {
return (0, _dom.div)('Component');
})
};
}
function main(sources) {
// just a state stream used to create two components
var shared$ = sources.DOM.select('button').events('click').map(function (_) {
return [1];
}).startWith([1]);
// mapping over xs.of([1,2]) fixes the problem but I need to map over shared$ and would like to know
// why I get this behaviour
var sinks$ = shared$.map(function (array) {
return array.map(function (key) {
return Component((0, _extends3.default)({ shared$: shared$ }, sources));
});
});
var childDOM$ = sinks$.map(function (array) {
return array.map(function (sink) {
return sink.DOM;
});
}).map(function (array) {
return _xstream2.default.combine.apply(_xstream2.default, (0, _toConsumableArray3.default)(array));
}).flatten();
var vdom$ = sinks$.map(function (childDOM) {
return (0, _dom.div)([(0, _dom.div)(childDOM[0].DOM), (0, _dom.button)('Change Shared Data'), (0, _dom.div)('PROBLEM: Why do I get more and more console.logs for every click on the button?')]);
});
return {
DOM: vdom$
};
}
(0, _xstreamRun.run)(main, {
DOM: (0, _dom.makeDOMDriver)('#app')
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment