Last active
April 3, 2016 18:02
-
-
Save vojtatranta/281eb9d3face7e074b4d6c7b101a3290 to your computer and use it in GitHub Desktop.
This file contains 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
const passProps = (props, component) => { | |
return { | |
type: 'LOCAL_STATE', | |
props, | |
component | |
} | |
} | |
const getElementWithLocalState = ({ clicked = false }) => { | |
return passProps(props, (update, props) => { | |
const onClick = () => { | |
update({ clicked: true }) | |
} | |
return document.createTextNode(props.clicked ? 'clicked!' : 'not clicked') | |
}) | |
} | |
//API: | |
// When this function is called first time, "renderer" should see that it does not return DOM and than it should take returned function | |
// and call it with update argument, which is a function that gets new props as argument | |
//renderer.js | |
const replaceElement = (oldEl, newEl) => { | |
oldEl.parentNode.replaceChild(newEl, oldEl) | |
} | |
const createElementUpdate = (rootEl, path, componentFn) => { | |
return (state) => { | |
let element = getElementByPath(path, rootEl) | |
let updateFn = createElementUpdate(rootEl, path, componentFn) | |
replaceElement(element, componentFn(updateFn, state)) | |
} | |
} | |
let maybeDom = getElementWithLocalState({}) | |
if (maybeDom['type'] == 'LOCAL_STATE') { | |
let updateFn = createElementUpdate(document.body, [ 0 ], maybeDom.component) | |
dom = maybeDom.component(updateFn, maybeDom['props']) | |
} else { | |
let dom = maybeDom | |
} | |
document.body.appendChild(dom) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment