Skip to content

Instantly share code, notes, and snippets.

@abradley2
Created January 5, 2019 18:14
Show Gist options
  • Save abradley2/042e00747cf65d3ccee58cdd72c25113 to your computer and use it in GitHub Desktop.
Save abradley2/042e00747cf65d3ccee58cdd72c25113 to your computer and use it in GitHub Desktop.
Model-View-Intent
import xs from 'xstream'
import { run } from '@cycle/run'
import * as H from '@cycle/dom'
import create from 'immutability-helper'
const INPUT = 'INPUT'
function intent (DOM) {
const inputEvents = DOM.select('.field').events('input')
.map((e) => ({
type: INPUT,
value: e.target.value
}))
return xs.merge(
inputEvents
)
}
const initialModel = {
message: 'hello there'
}
function model (state, action) {
switch (action.type) {
case INPUT:
return create(state, {
message: {$set: action.value}
})
}
return state
}
function view (state) {
return H.div([
H.div({}, state.message),
H.input({
attrs: {
class: 'field',
value: state.message
}
})
])
}
function app (sources) {
return {
DOM: intent(sources.DOM)
.startWith({ type: 'INIT' })
.fold(model, initialModel)
.map(view)
}
}
run(app, { DOM: H.makeDOMDriver('#app') })
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment