Bacon example does not run as suggested
cefn opened this issue · comments
I copied the code from an example describing how groupBy works, as I was suspicious that the operation on the stream in limitF actually did nothing.
In the limitF call, is a call to takeUntil, but the return value is never used, and no return value is passed back from the function. If I understand Bacon correctly, then the original stream is unchanged by the filter calls, and if the result is not used, so I'd expect there to be no side-effects. I was trying to run the example to investigate what was wrong with my mental model.
However, when I run the example against the current git distribution, it actually fails with a different error as follows.
require("./groupby.js");
{}
>
/home/user/Documents/code/imagination/git/bacon.js/dist/Bacon.js:1246
throw e;
^
TypeError: Cannot call method 'withHandler' of undefined
at /home/cefn/Documents/code/imagination/git/bacon.js/dist/Bacon.js:2862:33
at Next.valueF (/home/cefn/Documents/code/imagination/git/bacon.js/dist/Bacon.js:882:18)
at Next.value (/home/cefn/Documents/code/imagination/git/bacon.js/dist/Bacon.js:871:35)
at spawn (/home/cefn/Documents/code/imagination/git/bacon.js/dist/Bacon.js:2223:40)
at Object.sink (/home/cefn/Documents/code/imagination/git/bacon.js/dist/Bacon.js:2274:22)
at Dispatcher.pushToSubscriptions (/home/cefn/Documents/code/imagination/git/bacon.js/dist/Bacon.js:1236:23)
at Dispatcher.pushIt (/home/cefn/Documents/code/imagination/git/bacon.js/dist/Bacon.js:1259:14)
at Object.Bacon.UpdateBarrier.inTransaction (/home/cefn/Documents/code/imagination/git/bacon.js/dist/Bacon.js:350:18)
at Dispatcher.push (/home/cefn/Documents/code/imagination/git/bacon.js/dist/Bacon.js:1227:28)
at Dispatcher._handleEvent (/home/cefn/Documents/code/imagination/git/bacon.js/dist/Bacon.js:2824:23)
The original code (with the modification of the require call) is as follows...
var Bacon = require("baconjs");
var events = [
{id: 1, type: "add", val: 3 },
{id: 2, type: "add", val: -1 },
{id: 1, type: "add", val: 2 },
{id: 2, type: "cancel"},
{id: 3, type: "add", val: 2 },
{id: 3, type: "cancel"},
{id: 1, type: "add", val: 1 },
{id: 1, type: "add", val: 2 },
{id: 1, type: "cancel"}
]
function keyF(event) {
return event.id
}
function limitF(groupedStream, groupStartingEvent) {
var cancel = groupedStream.filter(function(x) { return x.type === "cancel"}).take(1)
var adds = groupedStream.filter(function(x) { return x.type === "add" })
adds.takeUntil(cancel).map(".val")
}
Bacon.sequentially(2, events)
.groupBy(keyF, limitF)
.flatMap(function(groupedStream) {
return groupedStream.fold(0, function(acc, x) { return acc + x })
})
.onValue(function(sum) {
console.log(sum)
// returns [-1, 2, 8] in an order
})
...and it can be found under the heading "Calculator for grouped consecutive values until group is cancelled" at https://github.com/baconjs/bacon.js/
Is there something I'm doing wrong which means this example doesn't work.
There's a return
missing from the limitF
function above.
I fixed the example in the readme.