Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Align runtime and React view functions #7

Merged
merged 1 commit into from
Jul 22, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ npm install raj
- `effect.map(mapper, effect)`: transforms the dispatched values of `effect` using the function `mapper`
- `effect.batch(effects)`: group an array of effects into a single effect
- `raj/runtime`: create generic runtimes
- `program({init, update, renderer})`
- `program({init, update, view})`
- `init`: the initial state and optional effect
- `update(message, state)`: return the new state and optional effect
- `renderer(state, dispatch)`: use the state and dispatch messages
- `view(state, dispatch)`: use the state and dispatch messages

#### Integrations
- `raj/react`: React bindings
Expand Down
2 changes: 1 addition & 1 deletion react.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ function reactProgram (Component, {init, update, view}) {
program({
init: init(props),
update,
renderer: (dispatch, state) => {
view: (state, dispatch) => {
this._dispatch = dispatch
if (initial) {
this.state = {state}
Expand Down
4 changes: 2 additions & 2 deletions runtime.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
function program ({init, update, renderer}) {
function program ({init, update, view}) {
let state

function change ([newState, effect]) {
state = newState
if (effect) {
setTimeout(() => effect(dispatch), 0)
}
renderer(dispatch, state)
view(state, dispatch)
}

function dispatch (message) {
Expand Down
8 changes: 4 additions & 4 deletions test/runtime.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
import test from 'ava'
import {program} from '../runtime'

test('program() should call renderer() initially', t => {
test('program() should call view() initially', t => {
const initialState = 1
return new Promise(resolve => {
program({
init: [initialState],
renderer (dispatch, state) {
view (state) {
t.is(state, initialState)
resolve()
}
})
})
})

test('program() should call renderer() after dispatch', t => {
test('program() should call view() after dispatch', t => {
let count = 0
return new Promise(resolve => {
program({
init: ['init'],
update (msg) {
return [msg]
},
renderer (dispatch, state) {
view (state, dispatch) {
count++
if (state === 'init') {
return dispatch('next')
Expand Down