ReactiveX / ReactiveX/rxjs

exhaustMapWithTrailing/debounceMap implementation

Open
#5,004 13 comments 7 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
TypeScript
Stars
31.7k
Forks
3k
PR merge metrics
No merged PRs in 30d

Description

I'm just opening (and closing) this to add an option to #1777, which I found and spent some time trying to get the examples working but ultimately found them lacking. I'm sorry if this isn't welcome (perhaps adding a stackoverflow Q&A would be better?)

This appears to work for me, hopefully it will be helpful to others:

// Sometimes we want the throttling behavior of exhaustMap without the losing
// the last emission. This is helpful in situations where we want to only issue
// one request at a time, but if we are told to issue another while issuing the
// first, we respect that. Think of it as a nicer switchMap.
//
// input   -1--2--3--4-------->
// project -1xxxxxxx3xxxxxxx4-> (x is async/waiting)
//
// Originally started with this, but did not work:
// https://github.com/ReactiveX/rxjs/issues/1777#issuecomment-466267803
export function exhaustMapWithTrailing<T, R>(
  project: (value: T, index: number) => ObservableInput<R>
): OperatorFunction<T, R> {
  return (source): Observable<R> => {
    const release = new Subject()

    return source.pipe(
      throttle(() => release, {
        leading: true,
        trailing: true,
      }),
      // TODO: Upgrade to TypeScript 3.6.3 when available and remove the cast
      // https://github.com/microsoft/TypeScript/issues/33131
      exhaustMap((value, index) =>
        from(project(value, index)).pipe(
          finalize(() => {
            release.next()
          })
        )
      ) as OperatorFunction<T, R>
    )
  }
}

/cc @edusperoni @Dorus

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reading issue #1777 and the TypeScript exhaustMapWithTrailing implementation and examples included here. Verify the proposed behavior for trailing emissions and determine whether the request has a settled scope; done requires an agreed implementation or documentation outcome.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
tooling
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.