Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

242
Views
Custom distinctUntilChanged implementation

In our Angular application we use distinctUntilChanged() quite often and we are at a point where we want to use it to compare form.valueChanges directly. As described in this question: https://stackoverflow.com/a/53825991/1003686 using distinctUntilChanged on the form.valueChanges is not going to work properly:

this.myFormControl.valueChanges
  .pipe(distinctUntilChanged())
  .subscribe(newValue => {
    console.log('fires endlessly');
  });

and instead I have to write it like this to get a very basic comparison working.

this.searchAndFilterForm.valueChanges
  .pipe(distinctUntilChanged((a, b) => JSON.stringify(a) === JSON.stringify(b))
  .subscribe(newValue => {
    console.log('fires only on real changes');
  });

I don't want to define a custom compare on each form valuechange and instead I want to have a new Pipe operator called formDistinctUntilChanged() that I can use and refactor if needed.

I saw that distinctUntilChanged() is a MonoTypeOperatorFunction. How can I implement a custom MonoTypeOperatorFunction so that I get a formDistinctUntilChanged() operator? Preferably just using the normal distinctUntilChanged() with a predefined compare.

Basically I am looking for something that is doing the same as the normal distinctUntil but with a predefined compare that I can reuse in multiple components.

this.myFormControl.valueChanges
  .pipe(formDistinctUntilChanged())
  .subscribe(newValue => {
    console.log('fires only on real changes');
  });

Creating a custom rxjs operator like it is described in https://tane.dev/2021/01/creating-custom-rxjs-operators/ does not really work in my angular environment because we have eslint(prefer-arrow/prefer-arrow-functions) defined and I do not fully understand how to use another MonoTypeOperatorFunction in a MonoTypeOperatorFunction.

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

A pipeable operator is just a function that accepts a observable and returns an observable. So you could use something like this:

const formDistinctUntilChanged =
  <T>() =>
  (obs$: Observable<T>): Observable<T> =>
    obs$.pipe(distinctUntilChanged(/*your comparator*/));
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!