Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a new RxJS utility function to create a repeat operator with an internal subject #380

Merged
merged 3 commits into from
Jul 15, 2024

Conversation

LcsGa
Copy link
Contributor

@LcsGa LcsGa commented May 21, 2024

I thougt of this tool while working on a side project as I repeated this kind of logic:

@Component({
  ...,
  template: `
    ...
    <!-- diplayed on error -->
    <button (click)="retry()">Retry</button>
  `
})
export class Comp {
  readonly #service = inject(MyService);

  readonly retry$ = new Subject<void>();

  readonly #register = rxEffect(this.#service.register$.pipe(
    // custom operator with a map (data), startwith (pending), catchError (error) + a short debounceTime to avoid flashings
    toState(),
    repeat({ delay: () => this.#repeat$ })
  ));

  // Whereas it is a repeat, the retry name comes from the logic itself
  retry() {
    this.retry$.next();
  }
}

=> I had to repeat the subject everytime I needed this repeat operator, and as I don't want to let subjects public, I also had to wrap the next logic within a method + to use that subject inside of a repeat operator and it felt like I could simplify that with a custom createRepeat function:

@Component({
  ...,
  template: `
    ...
    <!-- diplayed on error -->
    <button (click)="retry.emit()">Retry</button>
  `
})
export class Comp {
  readonly #service = inject(MyService);

  // the cool part here is that you can name your operator as you want
  readonly retry = createRepeat();

  readonly #register = rxEffect(this.#service.register$.pipe(toState(), this.retry()));
}

NB: I also plan to add a createRetry

@nartc nartc merged commit eb3615e into ngxtension:main Jul 15, 2024
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants