re-rxjs / react-rxjs

React bindings for RxJS
https://react-rxjs.org
MIT License
549 stars 19 forks source link

feat(utils/combineKeys): emit set with changed keys #199

Closed voliva closed 3 years ago

voliva commented 3 years ago

Recently I found a use case where I'd like to have an operator similar to combineKeys, but that instead of emitting a map of values it would just emit the latest individual change.

I couldn't find any way of composing the existing operators to do this, so I had to make a custom operator which had mostly combineKeys' code changing a few lines.

After discussing it we found out that this operator would be easy to implement if the existing combineKeys also emitted the list of keys that have changed. This is a posible implementation.

With this operator, the mergeKeys operator I had in mind would become something like:

const mergeKeys = <K, T>(keys$: Observable<K>, getInner$: (key: K) => Observable<T>): Observable<T> =>
  combineKeys(keys$, getInner$).pipe(
    mergeMap(values => {
      return Array.from(values.changes)
        .map(key => values.get(key))
        .filter(value => value !== undefined)
    })
  )

Doubts on this one:

codecov[bot] commented 3 years ago

Codecov Report

Merging #199 (791bac2) into main (13fa3af) will not change coverage. The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff            @@
##              main      #199   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           27        27           
  Lines          445       448    +3     
  Branches        60        60           
=========================================
+ Hits           445       448    +3     
Impacted Files Coverage Δ
packages/utils/src/combineKeys.ts 100.00% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more Δ = absolute <relative> (impact), ø = not affected, ? = missing data Powered by Codecov. Last update 13fa3af...791bac2. Read the comment docs.

peicodes commented 3 years ago

Can't wait to use this!