Oran Dennison
Oran Dennison

Reputation: 3297

Debouncing unique values with Bacon.js

I have a file system watcher producing a Bacon.js event stream of changed file paths. I'd like to filter and debounce this stream so that each unique file path only appears in the output stream after 5 seconds of no activity for that unique value. I essentially want to write the following pseudocode:

var outputStream = inputStream.groupBy('.path',
    function (groupedStream) { return groupedStream.debounce(5000); }
).merge();

I have a convoluted solution that involves creating a separate Bacon.Bus for each filtered stream, and creating a new Bus each time I encounter a new unique value. These are each debounced and plugged into an output Bus. Is there a better way? Would I be better off switching to RxJS and using its groupBy function?

Upvotes: 2

Views: 309

Answers (1)

Oran Dennison
Oran Dennison

Reputation: 3297

It turns out Bacon.js recently added a groupBy function! I had been misled by searches that indicated it didn't exist. So this works for me:

var outputStream = inputStream.groupBy(function (item) { return item.path; })
    .flatMap(function (groupedStream) { return groupedStream.debounce(5000); });

Edit: here's a simplified version based on OlliM's comment (kiitos!):

var outputStream = inputStream.groupBy('.path')
    .flatMap(function (groupedStream) { return groupedStream.debounce(5000); });

Upvotes: 2

Related Questions