Created
September 6, 2016 15:00
-
-
Save nordfjord/1ad06d61e760ab9de6473f7dc60f0ce3 to your computer and use it in GitHub Desktop.
Subtree retain flyd streams
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
import m from 'mithril'; | |
import f from 'flyd'; | |
import retain from './flyd-retain'; | |
function myComponent(){ | |
let username = f.stream(''); | |
let username_header = username.map(name => m('h1', name)); | |
let username_input = username.map(name => m('input', {value: name, oninput: m.withAttr('value', username)})); | |
return retain(f.combine(function(h, i){ | |
return m('.user', [h(), i()]); | |
}, [username_header, username_input])); | |
} | |
function component(c) { | |
return {controller: c, view: v => v()}; | |
} | |
m.mount(document.body, component(myComponent)); |
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
export default function retain(src) { | |
let _changed = true; | |
src.map(()=> _changed = true); | |
return function(){ | |
if (!_changed) { | |
return {subtree: 'retain'}; | |
}; | |
_changed = false; | |
return src(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment