2

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?

Oran Dennison
  • 3,237
  • 1
  • 29
  • 37

1 Answers1

2

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); });
Oran Dennison
  • 3,237
  • 1
  • 29
  • 37
  • 1
    You can use `groupBy(".path")` instead of your function, see [Function construction rules](https://github.com/baconjs/bacon.js#function-construction-rules) in bacon documentation. – OlliM Aug 27 '15 at 08:00