Scan with halt (#1957)
* HALT if scan reducer doesn't change value * Updated docs to reflect new scan behaviour with HALT
This commit is contained in:
parent
117fac91a7
commit
fb3c344055
3 changed files with 38 additions and 2 deletions
|
|
@ -117,7 +117,9 @@ function merge(streams) {
|
|||
|
||||
function scan(reducer, seed, stream) {
|
||||
var newStream = combine(function (s) {
|
||||
return seed = reducer(seed, s._state.value)
|
||||
var next = reducer(seed, s._state.value)
|
||||
if (next !== HALT) return seed = next
|
||||
return HALT
|
||||
}, [stream])
|
||||
|
||||
if (newStream._state.state === 0) newStream(seed)
|
||||
|
|
|
|||
|
|
@ -30,4 +30,36 @@ o.spec("scan", function() {
|
|||
o(result[2]).equals(undefined)
|
||||
o(result[3]).deepEquals({a: 1})
|
||||
})
|
||||
|
||||
o("reducer can return HALT to prevent child updates", function() {
|
||||
var count = 0
|
||||
var action = stream()
|
||||
var store = stream.scan(function (arr, value) {
|
||||
switch (typeof value) {
|
||||
case "number":
|
||||
return arr.concat(value)
|
||||
default:
|
||||
return stream.HALT
|
||||
}
|
||||
}, [], action)
|
||||
var child = store.map(function (p) {
|
||||
count++
|
||||
return p
|
||||
})
|
||||
var result
|
||||
|
||||
action(7)
|
||||
action("11")
|
||||
action(undefined)
|
||||
action({a: 1})
|
||||
|
||||
result = child()
|
||||
|
||||
// check we got the expect result
|
||||
o(result[0]).equals(7)
|
||||
|
||||
// check child received minimum # of updates
|
||||
o(count).equals(2)
|
||||
})
|
||||
|
||||
})
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue