These are chat archives for pozadi/kefir

1st
Oct 2015
Thomas Collardeau
@collardeau
Oct 01 2015 07:13

Hi good peeps. Question: I'm combining two streams with Kefir.combine() and want to return a filtered stream of the first stream based on the second stream.

(for context: the first stream is weather forecast records, the second stream is a minimum temperature... I only want to return the weather forecasts that pass the minimum temp).

So my Kefir.combine() is returning forecasts.filter(... ) . When I onValue() the results I get an AnonymousObservable. Anyone know how I can get the actual stream of filtered forecasts. Hope I made sense, thanks for any guidance!

Roman Pominov
@rpominov
Oct 01 2015 08:21
Hey, you can do something like this:
result = Kefir.combine([forecasts, tempeture])
  .filter(data => data[1] < minTemp)
  .map(data => data[0])

result.onValue(forecastData => ...)
@collardeau ^
result = forecasts.filterBy(tempeture.map(t => t < minTemp))
Ulric Wilfred
@shamansir
Oct 01 2015 08:49
btw, from the upgrade to v3.0 I’ve noticed that indices for values of merged streams are quite non-safe to use. It very easy to forget to change them when you change your code (of course, tests are required, but still).
could that be an option to do smth like this:
Kefir.combine({ forecast: forecasts, temp: temperature })
     .filter(combined => combined.temp < minTemp)
     .map(combined => combined.forecast)
Denis Stoyanov
@xgrommx
Oct 01 2015 09:03
@shamansir did you mean smth like https://github.com/staltz/combineLatestObj ?
Ulric Wilfred
@shamansir
Oct 01 2015 09:05
@xgrommx yes, looks like that! (as an option for every combinating method taking streams in arrays).
Denis Stoyanov
@xgrommx
Oct 01 2015 09:09
@shamansir Probably this sample should work:
function combineObj(obj) {
  var sources = [];
  var keys = [];
  for (var key in obj) {
    if (obj.hasOwnProperty(key)) {
      keys.push(key);
      sources.push(obj[key]);
    }
  }
  return Kefir.combine(sources).map(function (data) {
    var dataLength = data.length;
    var combination = {};
    for (var i = dataLength - 1; i >= 0; i--) {
      combination[keys[i]] = data[i];
    }
    return combination;
  })
}
Kefir.combineObj = combineObj;
Ulric Wilfred
@shamansir
Oct 01 2015 09:12
@xgrommx yeah, but I just thought about it as a part of official Kefir API. @rpominov, on the other hand, could say that it’s excessive, and could be right :worried: . or may be someone already suggested that.
Thomas Collardeau
@collardeau
Oct 01 2015 09:13

Thanks, I will check out filterBy (in a couple hours). Appreciated.

I was thinking maybe I should not think of my forecasts as a stream? It's a set of data that I'm already holding onto, and combine() only takes the last value in each stream, so it does not feel like the ideal solution to filter the entire stream...

I was thinking to have my stream of temperatures (user input) unwrap my forecast stream and only return the one that pass the condition. So there aonValue() inside a map(). Thinking out loud here...

Denis Stoyanov
@xgrommx
Oct 01 2015 09:25
@shamansir @collardeau This method already exists in core of Bacon http://baconjs.github.io/api.html#bacon-combinetemplate Also exist some implementations for Kefir https://github.com/ahomu/kefir.combinetemplate
Roman Pominov
@rpominov
Oct 01 2015 09:35
rpominov/kefir#143
Ulric Wilfred
@shamansir
Oct 01 2015 10:10
@rpominov cool, it’s great you thought about it before! :)