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

Feat: Add tapOnce and tapOnceOnFirstTruthy Operators #490

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

endlacer
Copy link

This pull request adds two new standalone RxJS operators, tapOnce and tapOnceOnFirstTruthy, to the utility library. These operators allow for conditional execution of functions based on emitted values.

tapOnce: Executes the provided function only once when the value at the specified index is emitted.
tapOnceOnFirstTruthy: Executes the provided function only once when the first truthy value is emitted.

What are your thoughts on these operators? They seem particularly useful for scenarios where you need to initialize something, such as a modal element, as soon as the first value from a source is emitted.

This is my first PR on GitHub, so I appreciate your patience :)! Any contributions, feedback, or change requests are more than welcome.

@endlacer endlacer marked this pull request as ready for review September 2, 2024 08:08
@eneajaho
Copy link
Collaborator

eneajaho commented Oct 1, 2024

Hello @endlacer
Thanks for creating this PR,
I would like to know more about the usecases for this operator, can you give some examples which this solves.
Currently it's hard to understand where this would be useful and if other projects can benefit from this.

@endlacer
Copy link
Author

endlacer commented Oct 1, 2024

hi @eneajaho ,
I used the tapOnceOnFirstTruthy operator for initialization of a modal dialog. For example, I have an open$ Subject of type ModalData. The ModalComponent subscribes to an Observable derived from this Subject. When it receives its first truthy value, it performs some initialization before opening the modal in the subscribe block.

Here's a simplified example:

import { Subject } from 'rxjs';
import { tapOnceOnFirstTruthy } from './operators';

class ModalComponent {
  private open$ = new Subject<ModalData | null>();

  constructor() {
    this.open$
      .pipe(
        tapOnceOnFirstTruthy((data) => {
          // Perform one-time initialization
          this.initializeModal(data);
        })
      )
      .subscribe((data) => {
        if (data) {
          this.openModal(data);
        } else {
          this.closeModal();
        }
      });
  }

  private initializeModal(data: ModalData) {
    // One-time setup logic
  }

  private openModal(data: ModalData) {
    // Logic to open the modal
  }

  private closeModal() {
    // Logic to close the modal
  }
}

This approach ensures that the initialization logic runs only once, even if the modal is opened and closed multiple times.

Another example could be a user filling out a multipage form and when reaching the midpoint you want to log that to some analyze-backend. But you only want to do it once and not on every back/forth navigation. This can be done with a sperate subscription with filter + take(1), but this operator is cleaner.


Concerning the API: It would be cleaner and more consistent to also allow the user to provide a tapIndex to the tapOnceOnFirstTruthy (and then rename if oc) method, similar to how it's implemented in the tapOnce method. This would give users more control over when the truthy check is performed.

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