NgRx effects - new streams cancelling out previous streams before they finish executing

I could really use some guidance to help me move forward. My issue involves a store that is an array containing charts, with each item in the array having its own state. The problem arises when a new effect is triggered and makes an API call - it ends up canceling out the previous API call along with the previous stream entirely. Any assistance would be greatly appreciated.

This is what the effect currently looks like:

@Effect() addCharts$ = this.actions$
    .ofType(LOAD_CHART)
    .switchMap((action) => {
        return this.chartApi.getChart(action.payload.url).map((res) => {
          res.id = action.payload.id;
          res.url = action.payload.url;
          return res;
        });
    })
    .map((result: any) => {
        debugger;
        return {
          type: LOAD_CHART_SUCCESS,
          payload: result
        }
    })
    .catch((error: any) => {
        debugger;
        return Observable.of({
            type: LOAD_CHART_FAILURE,
            payload: error.detail
        });
    });

As a result, my store ends up looking like this:

[
  {
    id: 1,
    data: null,
    loading: true,
    loaded: false
  },
  {
    id: 2,
    data: {
      ...
    }
    loading: false,
    loaded: true
  }
]

Answer №1

To maintain the order, it is recommended to use mergeMap over concatMap.

As per the information provided in the official documentation, using switchMap:

maps each source value to an Observable, which is then combined into the output Observable. It only emits values from the latest projected Observable.

Therefore, when a LOAD_CHART action is received by the effect, it unsubscribes from any existing chartApi.getChart operation.

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

Why is ASP.NET Core returning an empty object in the response?

Upon debugging the code in VS, I noticed that the cities list I am returning contains 3 objects with properties. However, when I call the endpoint, I receive a response of 3 empty list items. How can I resolve this issue? Model Class: public class City ...

A guide to customizing cell text color in autoTable using jspdf with Angular2+ based on specific conditions

I am looking to change the text color to red and make it bold if the 'Target Data Type' and 'Data Type Verified' values in the title columns are different when using autoTable in jspdf. I have attempted to write a code for this in Angul ...

Signing in with Angular2 and gapi

I am trying to implement a Google authentication button on my website. Here is the meta tag in the index.html file: <meta name="google-signin-scope" content="profile email"> <meta name="google-signin-client_id" content="xxxxx.apps.googleusercont ...

Steps to create an instance method that only accepts the name of another instance method

I am looking to enhance an object by adding a method that specifically accepts the name of another method within the object. How can I achieve this in a way that dynamically narrows down the accepted names of methods, without hardcoding them? Let's t ...

Combining Closure Compiler with Typescript

My objective is to leverage Typescript along with Closure Compile (advanced compilation) to target ES5 and then minify the resulting output. Is it mandatory for me to replace tsc with tsickle? I find that tsickle does not provide support for all options a ...

Utilizing movingMarker from leaflet-moving-marker in Angular: A Step-by-Step Guide

I am currently working on incorporating the leaflet-moving-marker plugin but encountering some errors in the process. import {movingMarker} from 'leaflet-moving-marker' var myMovingMarker = L.movingMarker([[48.8567, 2.3508],[50.45, 30.523 ...

How to set up Angular 5 with Express

What is the best way to add Angular 5 to an existing Express project? Below are my files: https://i.stack.imgur.com/DPgMs.png Package.json: https://i.stack.imgur.com/iVVxA.png I am looking to integrate Angular 5 into this express project and develop t ...

Issue with Angular 6 and Chrome: Event listener ($event) occasionally throws the error "unable to access property 'value' of null"

It appears that the buttons are being iterated correctly using ngFor, and upon inspection they have the correct attributes. However, when clicked, the function in the controller sometimes claims the parameter is 'undefined', happening randomly ab ...

Can a single file in NextJS 13 contain both client and server components?

I have a component in one of my page.tsx files in my NextJS 13 app that can be almost fully rendered on the server. The only client interactivity required is a button that calls useRouter.pop() when clicked. It seems like I have to create a new file with ...

`Is there a way to implement a collapsing toolbar in Ionic 3?`

I am looking to design a page similar to the one shown above. The layout includes an image positioned at the top of two divisions, or sandwiched between a toolbar and content as seen in the image above. I have been unable to find any resources or documen ...

Is it possible to navigate to a different section of a webpage while also jumping to a specific id within that section seamlessly?

I'm trying to create a navbar link that will take me directly to a specific section of a page, but I'm having trouble getting it to work. Here's what I've tried: <a href="home#id" class="nav-link text on-hover"></a> Where ...

Encountered a challenge in Angular 2.4 when attempting to import a feature module from another project into the main project

I currently have two distinct projects, one serving as the main project and the other as a shared project. Within the shared project, I have developed a feature module known as NavBar. The intention behind separating these projects is to facilitate the re ...

Understanding File Reading in Angular/Typescript

I'm currently developing an app similar to Wordle and I'm facing an issue with reading words from a file. Here's what I tried: import * as fs from 'fs'; const words = fs.readFileSync('./words.txt', 'utf-8'); con ...

Name cannot be located within the specified namespace

I am facing an issue with separating interfaces and implementations in TypeScript by utilizing the `module` feature. Despite using `<reference path=.../>`, I keep getting a "Cannot find name" error in my code. Below is an example: IUserService.ts n ...

Having trouble loading my Angular 2 application on Plunker correctly

Link to Plunker 1 encounters an issue with the following error message: zone.js:323 Error: Error: XHR error (404 Not Found) loading http://run.plnkr.co/XaKTrrlcPIlCBySj/node_modules/rxjs/index.js(…) Link to Plunker 2 is facing a different error: angul ...

Using observables rather than promises with async/await

I have a function that returns a promise and utilizes the async/await feature within a loop. async getFilteredGuaranteesByPermissions(): Promise<GuaranteesMetaData[]> { const result = []; for (const guarantees of this.guaranteesMetaData) { ...

Adding ngModel in the template causes the Angular component to load multiple times

I have been working on creating an Angular form and have successfully referenced the form in the Angular module. However, I am facing a problem where adding ngModel to an input textbox causes the Angular component to load multiple times on the page. Belo ...

Generating a Radio Button Label on-the-fly using Angular 8 with Typescript, HTML, and SCSS

Struggling with generating a radio button name dynamically? Looking to learn how to dynamically generate a radio button name in your HTML code? Check out the snippet below: <table> <td> <input type="radio" #radio [id]="inputId" ...

An error is being thrown in the Angular build due to an issue with parsing JSON after stripping out

Currently, I am working with angular nx alongside nestjs. Upon cloning the project and executing the yarn command, it successfully builds. However, whenever I try to install any package and compile the project, an error is thrown: **D:\projectNAme&bso ...

Tips for soothing the TypeScript compiler

It seems like TypeScript is heavily influenced by Microsoft's interpretation of the DOM and JavaScript. But what if I'm not concerned with Internet Explorer and Edge? Unfortunately, I'm encountering issues with TypeScript... For instance, w ...