Skip to content

Instantly share code, notes, and snippets.

@staltz
Created December 10, 2015 19:46
Show Gist options
  • Save staltz/f460cc137fdef0181df3 to your computer and use it in GitHub Desktop.
Save staltz/f460cc137fdef0181df3 to your computer and use it in GitHub Desktop.
Tiny Cycle.js 2
function main() {
return {
DOM: Rx.Observable.timer(0, 1000)
.map(i => {
return {
tagName: 'h1',
children: [`Seconds elapsed ${i}`]
}
})
}
}
const drivers = {
DOM: function DOMDriver(sink) {
function createEl(obj) {
const element = document.createElement(obj.tagName);
obj.children
.filter(c => typeof c === 'object')
.map(createEl)
.forEach(c => element.appendChild(c));
obj.children
.filter(c => typeof c === 'string')
.forEach(c => element.innerHTML += c);
return element;
}
sink.subscribe(obj => {
const container = document.querySelector('#app');
container.innerHTML = '';
if (typeof obj === 'string') {
container.innerHTML += obj;
} else {
container.appendChild(createEl(obj));
}
});
}
};
function run(main, drivers) {
const sinks = main();
drivers.DOM(sinks.DOM);
}
run(main, drivers);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment