Skip to content

Instantly share code, notes, and snippets.

@busypeoples
Last active September 22, 2016 21:21
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save busypeoples/8741fd4cdc705a58879d8285cd18dbe6 to your computer and use it in GitHub Desktop.
Save busypeoples/8741fd4cdc705a58879d8285cd18dbe6 to your computer and use it in GitHub Desktop.
// a basic example demonstrating the power of channels and generators
import React from 'react'
import { render } from 'react-dom'
import { chan, go, take, put, putAsync } from 'js-csp'
import { curry } from 'ramda'
import Counter from './Counter'
// helper
const createRender = curry((node, app) => render(app, node))
// create one channel for now
const AppChannel = chan()
const doRender = createRender(document.getElementById('mountNode'))
// let start
const AppStart = ({ init, update, view }) => {
let model = 0
const signal = action => () => {
model = update(action, model)
putAsync(AppChannel, model)
}
// initial render...
putAsync(AppChannel, init(model))
go(function* () {
while(true) {
doRender(view(signal, yield take(AppChannel)))
}
})
}
// start
AppStart(Counter)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment