-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e9552f2
commit 31a7767
Showing
2 changed files
with
92 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
const test = require('tape') | ||
const defer = require('pull-defer') | ||
const extend = require('extend') | ||
const inu = require('../') | ||
const pull = inu.pull | ||
|
||
test('infinite loop if sync dispatch, model always new, and effect needs to run async', function (t) { | ||
var i = 0 | ||
var ended = false | ||
const app = { | ||
init: function () { | ||
return { model: {}, effect: 'INIT' } | ||
}, | ||
update: function (model, action) { | ||
switch (action) { | ||
case 'TICK': | ||
return { model: {}, effect: 'NEXT' } | ||
case 'DONE': | ||
return { model: false } | ||
} | ||
return { model: model } | ||
}, | ||
view: function (model, dispatch) { | ||
if (ended) return | ||
if (i++ > 1000) { | ||
ended = true | ||
t.ok(true) | ||
return t.end() | ||
} | ||
if (model) return dispatch('TICK') | ||
t.ok(false) | ||
}, | ||
run: function (effect) { | ||
switch (effect) { | ||
case 'NEXT': | ||
const deferred = defer.source() | ||
process.nextTick(function () { | ||
deferred.resolve(pull.values(['DONE'])) | ||
}) | ||
return deferred | ||
} | ||
} | ||
} | ||
const sources = inu.start(app) | ||
}) | ||
|
||
test('no infinite loop if async dispatch, model always new, and effect needs to run async', function (t) { | ||
var i = 0 | ||
var ended = false | ||
const app = { | ||
init: function () { | ||
return { model: {}, effect: 'INIT' } | ||
}, | ||
update: function (model, action) { | ||
switch (action) { | ||
case 'TICK': | ||
return { model: {}, effect: 'NEXT' } | ||
case 'DONE': | ||
return { model: false } | ||
} | ||
return { model: model } | ||
}, | ||
view: function (model, dispatch) { | ||
if (ended) return | ||
if (i++ > 1000) { | ||
ended = true | ||
t.ok(false) | ||
return t.end() | ||
} | ||
if (model) { | ||
process.nextTick(() => { | ||
return dispatch('TICK') | ||
}) | ||
return | ||
} | ||
t.ok(true) | ||
t.end() | ||
}, | ||
run: function (effect) { | ||
switch (effect) { | ||
case 'NEXT': | ||
const deferred = defer.source() | ||
process.nextTick(function () { | ||
deferred.resolve(pull.values(['DONE'])) | ||
}) | ||
return deferred | ||
} | ||
} | ||
} | ||
const sources = inu.start(app) | ||
}) |