Unique Pipe Application Using RxJS Filters

I am facing an issue with subscribing to an observable using the async pipe in Angular. The data in the pipe may come from different sources and have varying structures based on the source.

The current implementation is not working as expected because I am overriding the initial value of this.data$, resulting in source A not being subscribed to.

Is there a way to 'split' the pipe based on a filter?

problem.component.ts:

// Data source A and logic A 
this.data$ = this.service.context.pipe(
  filter(context => context.flag === true),
  switchMap(context => this.dataSourceA(context.id)),
  map(data => this.processDataA(data))
);

// Data source B and logic B 
this.data$ = this.service.context.pipe(
  filter(context => context.flag === false),
  switchMap(context => this.dataSourceB(context.id)),
  map(data => this.processDataB(data))
);

problem.component.html

<pro-table [data]="data$ | async"></pro-table>

Answer №1

Transfer the logic to your switchMap()

this.data$ = this.service.context.pipe(
   switchMap(context => context.flag
      ? this.dataSourceA(context.id).pipe(map(d => this.processDataA(d)))
      : this.dataSourceB(context.id).pipe(map(d => this.processDataB(d)))
   )
);

Answer №2

Using a "higher order observable" is the key to achieving the desired result in a more effective way. Similar to a "higher order function" that can generate another function based on its arguments, a higher order observable can generate another Observable based on specific parametrization.

this.service.context.pipe(
  switchMap(context => context.flag ? this.dataSourceA(context.id).pipe(...) : this.dataSourceB(context.id).pipe(...))
);

Although it might seem complex at first, understanding higher order observables is crucial for optimizing your streams.


P.S. Remember: higher order observables allow you to parameterize your streams by working with streams of streams.

Answer №3

If you want to consolidate multiple data sources and streamline your subscription process, consider implementing a Subject with the following approach:

    this.data$ = new Subject();
    observer = {
     next(data) { this.data$.next(data) },
     error(msg) { /*handle error*/ }
   };
    // Incorporating Data source A and its corresponding logic 
   this.service.context.pipe(
     filter(context => context.flag === true),
     switchMap(context => this.dataSourceA(context.id))
     map(data => this.processDataA(data))
   ).subscribe(observer);

   this.service.context.pipe(
     filter(context => context.flag === false),
     switchMap(context => this.dataSourceB(context.id))
     map(data => this.processDataB(data))
   ).subscribe(observer);

To optimize efficiency by utilizing the flag condition to maintain cohesion within one block of code:

this.service.context.pipe(
     switchMap(context => context.flag ?
         this.dataSourceA(context.id) : this.dataSourceB(context.id)
    )
     map(data => this.processData(data)) // effectively handle both scenarios
   ).subscribe(observer);

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Experimenting with a loader component in AngularJS

This is a test for a $resource with a loader describe('Service: MultiCalculationsLoader', function(){ beforeEach(module('clientApp')); var MultiCalculationLoader, mockBackend, calculation; beforeEach(inject(function (_ ...

What is the best method for caching inline SVG in web browsers?

Over the years, SVGs have remained popular due to their scalability. One key advantage of inline SVG is the ability to manipulate it with CSS and JS. Additionally, using the <use> tag allows for referencing the original element when repeating the sam ...

How can I achieve a stylish scrolling header similar to a Google Blog for my website?

Google's blog features a unique header design - as you scroll down, the gray bar in the header moves up until it locks at the top of the screen. While CSS's position:fixed can achieve a similar effect, Google seems to have used a combination of p ...

When the user clicks on the element, swap out the current image tag

Is it possible to replace the <img src> code in a textarea with image URLs when a button is clicked? <textarea> <img src="https://example.com/image1.gif" alt="image1" /></a> <img src="https://example.com/image2.gif" alt="ima ...

Failure to load a picture does not trigger onError in the onLoad function

Is there a way to ensure that an image always loads, even if the initial load fails? I have tried using the following code snippet: <img class="small" src="VTVFile4.jpg" onload="this.onload=null; this.src='VTVFile4.jpg?' + new Date();" alt=" ...

Executing functions during the page loading process

I'm currently in the process of integrating a new payment method into a checkout page that does not have built-in support for it. The button on the page that redirects to an external payment portal requires specific parameters to be passed. I've ...

The challenge of customizing Ng-select styles

https://i.sstatic.net/NtU2H.pngOnce ng-select has been successfully installed using the command: npm install --save @ng-select/ng-select I have included the material style in styles.scss using the following import statement: @import "~@ng-select/ng-sel ...

unable to assign the disable property to a button element

In the code snippet below, I am working on a child-component. I have created a button and I want to include a disable property. However, the current implementation of the code results in an error message with red underlining indicating: Type '"is ...

Discover email addresses within the content of your emails using Mailparser

I'm a beginner when it comes to this topic and I'm encountering some problems with my mail parser. While I can successfully search and find emails in the email header (mail.from), I'm facing issues with locating them in the email body. Is th ...

What is the best way to assign a unique ID to every element in this situation?

Here is the given code: var words = ['ac', 'bd', 'bf', 'uy', 'ca']; document.getElementById("wordTable").innerHTML = arr2tbl(2); function arr2tbl(ncols) { return words.reduce((a, c, i) => { ...

Unable to store all of the location values in my array

When I request Twitter for all user locations, I encounter an issue where all my array objects end up with the same location - specifically the last one from the $.each loop. It seems like I am unable to loop through all array values properly due to the e ...

JavaScript method to prevent users from entering numbers as the first two characters, with all subsequent characters being numbers only

I need a specific requirement for my form. The textbox field in my form is labeled "DEA License number", and I want it to only allow the user to enter alphabetic characters for the first two characters, followed by numbers afterward. I am looking to impl ...

I am perplexed by JQuery. It is returning true when comparing an input value from a number type input, and I cannot figure out the reason behind it

Seeking guidance on a perplexing issue. Here is a basic code snippet to depict my dilemma: html: <form> <input type="number" id="someNumber"> <input type="button" id="submitBtn"> </form> jquery: $("#submitBtn"). ...

Error encountered: Unable to start Angular 5 project - '$' not recognized

I recently downloaded a project and ran npm install. It was running smoothly on my work PC, but now I am facing an issue when trying to start it. The error that keeps popping up is: error TS2304: Cannot find name '$'. This problem is occurring ...

Automatically expand all PrimeNG Accordion panels for easy printing purposes

I've implemented the PrimeNG library's accordion component in my angular project. You can find more information here. In my template, I have some custom css styling for printing the page that looks like this: @media print { .profile-progress ...

Currently, I am working with Angular 11 and encountered some errors while trying to embark on a new project

Command: ng serve Error: Cannot find module '@angular-devkit/build-angular/package.json' View details in "/tmp/ng-lQbnUK/angular-errors.log". These errors occurred when I tried to create the project: Installing packages (npm)... npm WARN depreca ...

Since switching to PHP 5.5 from version 3.x, I have noticed that it is attempting to interpret my JavaScript comment within a script tag in a PHP include file

After a long break from working with PHP, I recently encountered an issue with an older website I built using PHP and the include function. The site was functioning perfectly until the web host updated PHP to version 5.5, causing a strange bug where it see ...

Angular being called before being established

After spending a considerable amount of time working on AngularJS, I encountered an error stating that Angular is being used before it was defined. Despite watching several instructional videos and thoroughly reviewing the documentation, even with just the ...

"Triggering the scrollTop animation will trigger the scroll event to

I am facing an issue with my code that tracks the scrolling of a page. It successfully performs the necessary action when the "page" changes, but I am having trouble preventing the scroll event from being triggered when animating the scrollTop to the neare ...

Can you please provide me with the format of the image data that was returned and explain how I can encode it into base64?

https://i.sstatic.net/g5Pv6.png Could someone please explain the format of the image data shown in the link above? And how can I convert this data to base 64? Thank you! I am looking for a way to showcase this retrieved data on a web browser using an ima ...