Created
June 24, 2015 14:55
-
-
Save queckezz/bb787b1dc95bfefb3ed2 to your computer and use it in GitHub Desktop.
chat
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
const Type = require('union-type') | |
const h = require('snabbdom/h') | |
const flyd = require('flyd') | |
const R = require('ramda') | |
const patch = require('snabbdom').init([ | |
require('snabbdom/modules/class'), | |
require('snabbdom/modules/style'), | |
require('snabbdom/modules/props'), | |
require('snabbdom/modules/eventlisteners') | |
]) | |
// Model | |
const init = () => ({ | |
user: null, | |
route: '/' | |
}) | |
// Actions | |
const Action = Type({ | |
ChangeRoute: [String], | |
Login: [String] | |
}) | |
// Action -> Model -> Model | |
const update = Action.caseOn({ | |
Login: R.assoc('user'), | |
ChangeRoute: R.assoc('route') | |
}) | |
const view = R.curry((action$, model) => { | |
return h('div.container', [ | |
h('button', { | |
on: { click: () => action$(Action.ChangeRoute('/chat'))} | |
}, 'Goto Chat'), | |
h('p', model.route) | |
]) | |
}) | |
const fUpdate = R.flip(update) | |
const action$ = flyd.stream() | |
const model$ = flyd.scan(fUpdate, init(), action$) | |
const vnode$ = flyd.map(view(action$), model$) | |
const container = document.getElementById('js-app') | |
// Render | |
flyd.scan(patch, container, vnode$) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment