Skip to content

Commit

Permalink
Scan with halt (MithrilJS#1957)
Browse files Browse the repository at this point in the history
* HALT if scan reducer doesn't change value

* Updated docs to reflect new scan behaviour with HALT
  • Loading branch information
Adam Gamble authored and dead-claudia committed Sep 24, 2018
1 parent 089c082 commit eb34798
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 2 deletions.
4 changes: 3 additions & 1 deletion docs/stream.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,11 +120,13 @@ Argument | Type | Required | Description

Creates a new stream with the results of calling the function on every value in the stream with an accumulator and the incoming value.

Note that you can prevent dependent streams from being updated by returning the special value `stream.HALT` inside the accumulator function.

`stream = Stream.scan(fn, accumulator, stream)`

Argument | Type | Required | Description
------------- | -------------------------------- | -------- | ---
`fn` | `(accumulator, value) -> result` | Yes | A function that takes an accumulator and value parameter and returns a new accumulator value
`fn` | `(accumulator, value) -> result \| HALT` | Yes | A function that takes an accumulator and value parameter and returns a new accumulator value
`accumulator` | `any` | Yes | The starting value for the accumulator
`stream` | `Stream` | Yes | Stream containing the values
**returns** | `Stream` | | Returns a new stream containing the result
Expand Down
4 changes: 3 additions & 1 deletion stream/stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
32 changes: 32 additions & 0 deletions stream/tests/test-scan.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})

})

0 comments on commit eb34798

Please sign in to comment.