How can the action value be passed in effects for switchMap using both another switchMap and filter?

There is a certain code snippet

  @Effect()
  myEffect$ = this.actions$.pipe(
    ofType(MyActions.doSomething),
    tap(() => this.store.dispatch(MyActions.doSomething2())),
    switchMap(action => {
      

that is functioning as expected. In order to inject a filter just before the tap, I am looking for a solution that allows switchMap to retain the action value.

My attempt looks like this

  @Effect()
  myEffect$ = this.actions$.pipe(
    ofType(MyActions.doSomething),
    switchMap(() => this.store.pipe(select(mySelectors.getAmount), take(1))),
    filter(amount => amount <= 0),
    tap(() => this.store.dispatch(MyActions.doSomething2())),
    switchMap(action => {
    ....

However, it throws an error saying

property action.xxxx doesn't exist on type number
which is understandable. How can I avoid losing the action value returned by MyActions.doSomething?

Answer №1

To enhance the organization of the code and improve encapsulation, I suggest relocating the filter within the operator chain of the amount store. This adjustment ensures that the filter is only applied to specific values, eliminating the need to export the amounts outside of the switchMap. Instead, you can easily remap them to actions within the chain:

@Effect()
myEffect$ = this.actions$.pipe(
  ofType(MyActions.doSomething),
  switchMap(action => this.store.pipe(
    select(mySelectors.getAmount),
    take(1),
    filter(amount => amount <= 0), // apply filter here
    map(_ => action) // remap to actions
  )),
  tap(() => this.store.dispatch(MyActions.doSomething2())),
  switchMap(action => {
    // additional operations
  })

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

implementing a default reducer using ngrx schematics

I'm having trouble creating a reducer using the ngrx schematics command ng generate @ngrx/schematics:reducer ZipCodes --group When I generate the reducer file, it does not include a default implementation of the reducer export const reducer = createR ...

Guidelines for Managing Test Cases for a Promise-Returning Function with Resolve and Reject in Angular 6

I need to write a test case for a function that returns a Promise with resolve and reject. Here's the function definition: isAuthSuccess(): Promise<any> { const promise = new Promise((resolve, reject) => { if (this.userInfo) { ...

Can TypeScript provide a method for verifying infinite levels of nested arrays within a type?

Check out this example The concept behind this is having a type that can either be a single object or an array of objects. type SingleOrArray<T> = T | T[]; The structure in question looks like this: const area: ItemArea = [ { name: 'test1& ...

Deliver a securely authenticated single-page application on the root route through the use of a BFF, featuring a personalized login interface prior to employing Azure authentication

The scenario I'm dealing with is a bit complex, so bear with me as I try to explain it. In my ASP.NET Core 5 application, I've set up a BFF (backend for frontend) for my Angular app. The catch is that users must log in using Azure AD before acces ...

Extract the date and time information from the API response

I have received a response array from an API that I need to work with. Take a look at the response below: [{createdBy:"user1", updatedDttm: "2022-01-20T07:31:35.544Z"}, {createdBy:"user2", updatedDttm: "2022-02-20T09:31:3 ...

Angular2 and Typescript paired with Visual Studio 2013

Currently, I am utilizing the angular2 QUICKSTART and encountering an issue where Visual Studio fails to recognize Angular2 with typescript import Modules. However, everything else seems to be functioning correctly: https://i.stack.imgur.com/0s46Y.jpg Th ...

The server is taking too long to respond, resulting in a 504 Timeout error

I currently have an Angular frontend paired with a .NET CORE backend. The issue I am experiencing is related to a specific request that is resource-intensive and takes a significant amount of time to complete. Whenever I check the browser console, I receiv ...

Binding objects and properties from Angular2/Typescript to a template

Disclaimer: Seeking insight on the correct approach or any additional information _____________________________________________________________________ ANGULAR1 When working with angular1, we had the option to define our objects in the following ma ...

Implementing generics in TypeScript for objects made easy with this guide!

My question is regarding a function that utilizes generics and selects data from an object based on a key. Can we use generics inside the type of this object, or do we have to create a separate function for options? enum Types { book = 'book', ...

Identifying Scroll Behavior in Ionic 4

I'm trying to figure out how to detect upwards scrolling using the following code: <ion-infinite-scroll (ionInfinite)="doInfinite($event)" scrollY="true"> <ion-infinite-scroll-content></ion-infinite-scroll-content> </ion-infin ...

Aligning two identical components within the same container when triggered by a single click

Currently, I am exploring Angular 2 and Typescript and have been developing a pager component for a table. The pager functions correctly, but I have encountered an issue with having two pagers - one above the table and one below it. When the next button on ...

Launching Angular 4 front-end and .Net Web Api back-end simultaneously on the same IIS website results in a 404 error when refreshing the page

My web application consists of an Angular 4 front-end client-side code that communicates with a back-end services part written in ASP.NET WebAPI. I have deployed the application on IIS v10, within the same website. However, whenever I try to refresh the pa ...

The specified type 'undefined' cannot be assigned to the type '"default" | "red" | "green" | "blue"

I am currently developing an app using React and TypeScript. Can someone help me troubleshoot and resolve the error message below? import { styled } from "linaria/react"; type Color = { color: "default" | "red" | "gree ...

Ensure all promises are resolved inside of for loops before moving on to the next

Within my angular 11 component, I have a process that iterates through elements on a page and converts them from HTML to canvas to images, which are then appended to a form. The problem I am encountering is that the promise always resolves after the ' ...

Retrieve the runtime configuration object or file using Jest

Is there a way to access the Jest runtime config object or file path? I wanted to utilize runtime config properties in my custom matchers. // ./jest.config.js const path = require("path"); module.exports = { prop1: "foo", prop2: "bar" }; // my-custo ...

What is the best way to ensure that two promises are both resolved before triggering a function from within a promise?

In my code, I have a forEach loop on a matches fetch that looks like this: matches => { matches.forEach(match => { Promise.all([this.teamService.getTeam(match._links.homeTeam.href)]) .then(team => { match. ...

How can you load an HTML page in Puppeteer without any CSS, JS, fonts, or images?

My current task involves using Puppeteer to scrape data from multiple pages in a short amount of time. However, upon closer inspection, I realized that the process is not as efficient as I would like it to be. This is because I am only interested in spec ...

"Exploring the world of Typescript's return statements and the

I'm currently grappling with a design dilemma in typescript. Within my controller, I perform a validation process that can either return a 422 response, which ends the thread, or a validated data object that needs to be utilized further. Here's a ...

What is the process for inserting a new element into an Array-type Observable?

I've been working on the Angular Tour of Heroes example and I have a feature where users can add new heroes to the existing list. Here's my add hero method in hero.service.ts: addNewHero(hero : Hero) : Observable<Hero> { console.log(her ...

Building React Typescript Components with Froala Editor Plugins

Attempting to integrate a custom plugin into a Froala Editor within my React application using the package react-froala-wysiwyg. Following a tutorial on incorporating a custom popup/plugin found here. Encountering an issue due to TypeScript incompatibility ...