Created
December 10, 2015 19:46
-
-
Save staltz/f460cc137fdef0181df3 to your computer and use it in GitHub Desktop.
Tiny Cycle.js 2
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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