How to prevent data from being loaded in NgRx a second time?


Irina

I have a DOM block:

<div (click)="toggle(block)">
   <div *ngIf="block.opened">
     <div *ngFor="let item of block.items"></div>
   </div>
</div>

On click I dispatch action to load data from server and display this data in block:

  toggle(block: RegistryGroup) {
    this.store.dispatch(RegistryActions.ToggleRegistryBlockAction(block));
    this.store.dispatch(RegistryActions.LoadRegistryLayersAction(this.block));
  }

The first event ToggleRegistryBlockActionchanges the property blockto true/false. The second LoadRegistryLayersActionmakes a request to the server block.idand returns the data to the same state of blockthe propertyblock.items = Server Response;

Actually, I'm listening to the action LoadRegistryLayersAction:

loadRegistriesLayers$: Observable<Action> = createEffect(() =>
    this.actions$.pipe(
      ofType(RegistryActions.LoadRegistryLayersAction),
      filter(
        (registryGroup) =>
          registryGroup.items && !registryGroup.items.length
      ),
      switchMap((registryGroup: RegistryGroup) =>
        from(this.registry.getRegistryLayers(registryGroup.Id)).pipe(
          map((layers: { [key: string]: RegistryLayerItemGeneric[] }) => {
            return RegistryActions.SuccessLoadRegistryLayersAction({
              payload: {
                layers: this.registry.createLayersMapWithObjects(layers),
                registryGroupId: registryGroup.Id,
              },
            });
          }),
          catchError((error: Error) => {
            return of(RegistryActions.ErrorRegistryLayersAction(error));
          })
        )
      )
    )
  );

I filtertake action and check if the data already exists. For this, I check if the block has registryGroup.items && !registryGroup.items.length.

The problem is that sometimes after executing this effect server it may return empty []. So the next time the user requests it, it will repeat the request.

If the data is already loaded, how can I fix it and not touch the server?

time writer

There are multiple solutions:

  • Hold it can be a state property loading, loaded, empty, ...
  • keep a list of ids fetched in state and check this list before sending the request
  • don't check registryGroup.items.length, and initialize the array asnull

Related


How to prevent data from being loaded in NgRx a second time?

Irina I have a DOM block: <div (click)="toggle(block)"> <div *ngIf="block.opened"> <div *ngFor="let item of block.items"></div> </div> </div> On click I dispatch action to load data from server and display this data in block: toggle(block: Regist

How to prevent data from being loaded in NgRx a second time?

Irina I have a DOM block: <div (click)="toggle(block)"> <div *ngIf="block.opened"> <div *ngFor="let item of block.items"></div> </div> </div> On click I dispatch action to load data from server and display this data in block: toggle(block: Regist

How to prevent data from being loaded in NgRx a second time?

Irina I have a DOM block: <div (click)="toggle(block)"> <div *ngIf="block.opened"> <div *ngFor="let item of block.items"></div> </div> </div> On click I dispatch action to load data from server and display this data in block: toggle(block: Regist

How to prevent RSpec helpers from being loaded

Coding Rabbit Currently, I'm using Capybara to write integration tests for our Rails 4 application. To make it as lightweight as possible, I wrote a capybara_helper.rbfile inside the specfolder I want to use with RSpec . However, this file is loaded every time

How to prevent RSpec helpers from being loaded

Coding Rabbit Currently, I'm using Capybara to write integration tests for our Rails 4 application. To make it as lightweight as possible, I wrote a capybara_helper.rbfile that lives in the specfolder I want to use with RSpec . However, this file is loaded eve

How to prevent RSpec helpers from being loaded

Coding Rabbit Currently, I'm using Capybara to write integration tests for our Rails 4 application. To make it as lightweight as possible, I wrote a capybara_helper.rbfile that lives in the specfolder I want to use with RSpec . However, this file is loaded eve

How to prevent RSpec helpers from being loaded

Coding Rabbit Currently, I'm using Capybara to write integration tests for our Rails 4 application. To make it as lightweight as possible, I wrote a capybara_helper.rbfile that is inside the folder I want to specuse with RSpec . However, this file is loaded ev

How to prevent RSpec helpers from being loaded

Coding Rabbit Currently, I'm using Capybara to write integration tests for our Rails 4 application. To make it as lightweight as possible, I wrote a capybara_helper.rbfile that is inside the folder I want to specuse with RSpec . However, this file is loaded ev

How to prevent RSpec helpers from being loaded

Coding Rabbit Currently, I'm using Capybara to write integration tests for our Rails 4 application. To make it as lightweight as possible, I wrote a capybara_helper.rbfile that is inside the folder I want to specuse with RSpec . However, this file is loaded ev

How to prevent multiple copies of React from being loaded?

Sylar: In my previous Meteor app, using browserify and React, everything worked until I switched to meteor webpack . I'm using react-select in a Meteor app and it works great, but using browserify prevents multiple copies of react from being loaded, thus avoid

How to prevent multiple copies of React from being loaded?

Sylar: In my previous Meteor app, using browserify and React, everything worked until I switched to meteor webpack . I'm using react-select in a Meteor app and it works great, but using browserify prevents multiple copies of react from being loaded, thus avoid

How to prevent a specific dynamic library from being loaded

Herman I want to use an ancient software (Unreal Tournament "Classic" from 1999, also known as UT99). The dynamic library libtxc_dxtn.sois loaded implicitly in search of optional S3 Texture Compression (S3TC) support. Unfortunately, when loading the library, t

How to prevent a specific dynamic library from being loaded

Herman I want to use an ancient software (Unreal Tournament "Classic" from 1999, also known as UT99). The dynamic library libtxc_dxtn.sois loaded implicitly in search of optional S3 Texture Compression (S3TC) support. Unfortunately, when loading the library, t

How to prevent multiple copies of React from being loaded?

Syrah In my previous Meteor app, using browserify and React, everything worked fine until I switched to meteor webpack . I'm using react-select in a Meteor app and it works great, but using browserify prevents multiple copies of react from being loaded, thus a

How to prevent multiple copies of React from being loaded?

Sylar: In my previous Meteor app, using browserify and React, everything worked until I switched to meteor webpack . I'm using react-select in a Meteor app and it works great, but using browserify prevents multiple copies of react from being loaded, thus avoid

How to prevent a specific dynamic library from being loaded

Herman I want to use an ancient software (Unreal Tournament "Classic" from 1999, also known as UT99). The dynamic library libtxc_dxtn.sois loaded implicitly in search of optional S3 Texture Compression (S3TC) support. Unfortunately, when loading the library, t

How to prevent a specific dynamic library from being loaded

Herman I want to use an ancient software (Unreal Tournament "Classic" from 1999, also known as UT99). The dynamic library libtxc_dxtn.sois loaded implicitly in search of optional S3 Texture Compression (S3TC) support. Unfortunately, when loading the library, t

How to prevent data from being overwritten? Matlab

username Currently I am using that code to upload multiple files and import numeric data. But the code will overwrite the previously selected file and only give the data of the final selected file. Here is the code: [FileName,PathName,FilterIndex] = uigetfile(

How to prevent data from being overwritten? Matlab

username Currently I am using that code to upload multiple files and import numeric data. But the code will overwrite the previously selected file and only give the data of the final selected file. Here is the code: [FileName,PathName,FilterIndex] = uigetfile(

How to prevent else statement from being fired every time

Starks I'm very new to coding, sorry for wasting your time on beginner questions. I'm trying to learn JS with an ebook and my exercise atm is to write a program to save the entered name (first and last name) and the entered gender. The program should check the

How to prevent else statement from being fired every time

Starks I'm very new to coding, sorry for wasting your time on beginner questions. I'm trying to learn JS with an ebook and my exercise atm is to write a program to save the entered name (first and last name) and the entered gender. The program should check the