Feat: Add tapOnce and tapOnceOnFirstTruthy Operators - #490
Conversation
|
Hello @endlacer |
|
hi @eneajaho , 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 |
|
Any update on this? :) |
|
View your CI Pipeline Execution ↗ for commit fdb16b3
💡 Verify your cache is correct by running tasks in a sandbox. Read docs ↗ ☁️ Nx Cloud last updated this comment at |
7288b73 to
f12e1f1
Compare
|
/gemini |
…ique state per subscription
f12e1f1 to
80378f7
Compare
|
/gemini |
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.