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

Less complex done #18

Merged
merged 1 commit into from
Dec 4, 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
57 changes: 26 additions & 31 deletions runtime.js
Original file line number Diff line number Diff line change
@@ -1,40 +1,35 @@
module.exports = {
program: function (program) {
var update = program.update
var view = program.view
var done = program.done
var state
var isRunning = true
var timeout = setTimeout
exports.program = function (program) {
var update = program.update
var view = program.view
var done = program.done
var state
var isRunning = true
var timeout = setTimeout

function dispatch (message) {
if (isRunning) {
change(update(message, state))
}
function dispatch (message) {
if (isRunning) {
change(update(message, state))
}
}

function change (change) {
state = change[0]
var effect = change[1]
if (effect) {
timeout(function () { effect(dispatch) }, 0)
}
view(state, dispatch)
function change (change) {
state = change[0]
var effect = change[1]
if (effect) {
timeout(function () { effect(dispatch) }, 0)
}
view(state, dispatch)
}

change(program.init)
change(program.init)

return function kill () {
if (!isRunning) {
return
}
isRunning = false
if (done) {
var effect = done(state)
if (effect) {
timeout(function () { effect() }, 0)
}
}
return function kill () {
if (!isRunning) {
return
}
isRunning = false
if (done) {
done(state)
}
}
}
7 changes: 2 additions & 5 deletions test/runtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ test('program() should call view() after dispatch', t => {
})

test('program() should call done() when killed', t => {
t.plan(2)
t.plan(1)
return new Promise(resolve => {
const initialState = 'state'
const kill = program({
Expand All @@ -50,10 +50,7 @@ test('program() should call done() when killed', t => {
view () {},
done (state) {
t.is(state, initialState, 'the state is passed')
return () => {
t.pass('the effect is run')
resolve()
}
resolve()
}
})

Expand Down