Created
February 6, 2016 00:14
-
-
Save jefersondaniel/456b98924a7da4c16421 to your computer and use it in GitHub Desktop.
dumb-cyclejs-router
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 {Observable} from 'rx'; | |
function Intent (sources) { | |
return { | |
sources: sources, | |
changeLocation$: sources.History, | |
props$: sources.props$ | |
}; | |
} | |
function resolveComponent (model, location) { | |
model.component = model.routes[location]; | |
} | |
function Model (intent) { | |
let changeLocation$ = intent | |
.changeLocation$ | |
.map(location => model => { | |
resolveComponent(model, location); | |
return model; | |
}); | |
let props$ = intent.props$ | |
.map(props => model => { | |
console.log('Changing props', props); | |
model.routes = props.routes; | |
resolveComponent(model, props.initialLocation); | |
return model; | |
}); | |
return Observable.just(function (model) { | |
model.sources = intent.sources; | |
return model; | |
}) | |
.merge(props$, changeLocation$) | |
.scan((model, modelFn) => { | |
return modelFn(model); | |
}) | |
.shareReplay(1); | |
} | |
function View (model$) { | |
var sinks$ = model$.map( | |
function (model) { | |
return model.component(model.sources); | |
} | |
); | |
return { | |
DOM: sinks$.flatMap((sinks) => { | |
return sinks.DOM; | |
}) | |
}; | |
} | |
export default function Component (sources) { | |
return View(Model(Intent(sources))); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment