Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

243
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda