Is my zipLatest statement already existing?

quick question about the operator that I wrote for myself.

Please excuse my gloomy poor man charts:

zip aa--bb--cc--dd--ee--ff--------gg --11----22--33--------44--55---- ================================ --a1----b2--c3--------d4--e5---- combineLatest aa--bb--cc--dd--ee--ff--------gg --11----22--33--------44--55---- ================================ --a1b1--c2--d3--e3--f3f4--f5--g5 zipLatest aa--bb--cc--dd--ee--ff--------gg --11----22--33--------44--55---- ================================ --a1----c2--d3--------f4------g5 

zipLatest (the one I wrote) works almost at the same time as zip , but without queue, zip turns on.

I already implemented it, I'm just wondering if this already exists. I know that in the past I wrote a similar method to accidentally discover the case that I wrote a sample statement without knowing it.

So, does this already exist within the framework, or does it exist as a trivial composition of elements that I did not think about?

Note. I do not want to rely on the equality of my deduplication inputs (a la distinctUntilChanged ). It should work with a signal that outputs "a" to the interval.

+8
source share
2 answers

To give an update on the problem: there is still no operator for this requirement included in RxJS 6, and none of them are planned to be planned for future releases. In addition, there are no open call requests offered by this operator.

As suggested here , a combination of combineLatest , first and repeat will result in the expected behavior:

 combineLatest(obs1, obs2).pipe(first()).pipe(repeat()); 

combineLatest will expect the release of both observers - the release of all emissions except the last ones. first will complete the observation after the issue and repeat subscribe to combineLatest , forcing him to wait again for the latest values ​​of both observables.

The repeated behavior of the repeat subscription is not fully documented, but can be found in the GitHub source :

source.subscribe (this._unsubscribeAndRecycle ());

+2
source

Although you specifically mention that you are not using DistinctUntilChanged, you can use it with a counter for various new values:

 public static IObservable<(T, TSecond)> ZipLatest<T, TSecond>(this IObservable<T> source, IObservable<TSecond> second) { return source.Select((value, id) => (value, id)) .CombineLatest(second.Select((value, id) => (value, id)), ValueTuple.Create) .DistinctUntilChanged(x => (x.Item1.id, x.Item2.id), new AnyEqualityComparer<int, int>()) .Select(x => (x.Item1.value, x.Item2.value)); } public class AnyEqualityComparer<T1, T2> : IEqualityComparer<(T1 a, T2 b)> { public bool Equals((T1 a, T2 b) x, (T1 a, T2 b) y) => Equals(xa, ya) || Equals(xb, yb); public int GetHashCode((T1 a, T2 b) obj) => throw new NotSupportedException(); } 

Note that I used Int32 here - because it gives me Select (), but may be small for some use cases. Int64 or Guid may be the best choice.

0
source

All Articles