Monitoring for adjustments in variable with RXJS

One of my classes has a boolean variable called isLoading with a default value of false. It gets set to true when a certain action (HTTP request) occurs and then back to false once the action is completed. I am interested in using RXjs to monitor this variable.

this._dataSource = new HttpDataSource(this.service, this.paginator, this.sort, columns);

//This currently returns false as it happens before the action takes place 

this.isListLoading = this._dataSource.isLoading;

I would like to achieve something similar using RXjs

this._dataSource = new MCHTTPDataSource(this.takeThisFormService, this.paginator, this.sort, columns);

const intervalId = setInterval(()=>{
    if( !this._dataSource.isLoading ){
        this.isListLoading = false;
        clearInterval( intervalId );
    }
}, 250 );

Any thoughts on this approach?

Answer №1

For those who are interested

The element -

/*
**-------------------------------------------------------------------------------------
** FUNCTION NAME - ngOnInit
**-------------------------------------------------------------------------------------
*/
ngOnInit() {
    this.paginator.userPageIndex = 5;
    this.dataSource = new MCHTTPDataSource(
        this.tableService,
        this.paginator,
        this.sort,
        this.columnDefinitions
    );
    this.sub = this.dataSource.isLoading().subscribe(( isLoading )=>{
        //notify when loading is complete or close loading image, etc.
        console.log( "still loading", isLoading );
    });
}
/*
**-------------------------------------------------------------------------------------
** FUNCTION NAME - ngOnDestroy
**-------------------------------------------------------------------------------------
*/
ngOnDestroy() {
    this.dataSource.disconnect();
    this.sub.unsubscribe(); 
}

The Endpoint


public dataLoadingChange = new BehaviorSubject(true);
/*
**-------------------------------------------------------------------------------------
** FUNCTION NAME - isLoading
**-------------------------------------------------------------------------------------
*/
isLoading():Observable<boolean>{
    return this.dataLoadingChange.asObservable();
}   
/*
**-------------------------------------------------------------------------------------
** FUNCTION NAME - setLoading
**-------------------------------------------------------------------------------------
*/
setLoading( loading:boolean ){
    this.dataLoadingChange.next( loading );
}

//To trigger 
this.setLoading( true | false );

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

Issue: The formGroup function requires a valid FormGroup instance to be passed in. Data retrieval unsuccessful

My goal is to retrieve user data from a user method service in order to enable users to update their personal information, but I'm encountering an error. Currently, I can only access the "prenom" field, even though all the data is available as seen in ...

Utilizing JavaScript files within Angular2 components: A guide

I need to insert a widget that runs on load. Typically, in a regular HTML page, I would include the script: <script src="rectangleDrawing.js"></script> Then, I would add a div as a placeholder: <div name="rectangle></div> The is ...

Running your Angular application on a Node server: Step-by-step guide

I am looking to deploy my application using express on a Node server. This is the content of my server.js file: var express = require('express'); var path = require('path'); var app = express(); app.get('/', (req, res) => ...

Angular - Conceal Protected Links on the Template

In my AuthGuard, I have implemented CanActivate which returns either true or false based on custom logic: import { Injectable } from '@angular/core'; import { Router, CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot } from '@angular ...

Leveraging SumoLogic for managing logs in Angular 2 and higher applications

Is it possible to integrate SumoLogic () into my Angular 2+ application? I've searched online but haven't come across anything indicating that it can be done. Can anyone provide some guidance on this? ...

Vertical and horizontal tabs not functioning properly in Mat tabs

I successfully created a vertical material tab with the code below, but now I am looking to incorporate a horizontal tab inside the vertical tab. Attempting to do so using the code provided results in both tabs being displayed vertically. Below is my code ...

How to leverage tsconfig paths in Angular libraries?

While developing an Angular library, I made configurations in the tsconfig.lib.json file by adding the following setup for paths: "compilerOptions": { "outDir": "../../out-tsc/lib", "target": "es2015", "declaration": true, "inlineSources ...

Getting rid of parameters in HttpParams in Angular 4.3

I am working with a HttpParams object that looks like this: private filter: HttpParams = new HttpParams(); [...] this.filter = this.filter.append('page','1'); this.filter = this.filter.append('pageSize','50'); this. ...

Avoid unnecessary re-rendering of React Native components with each update of the state

I need my component to display either A or B based on the user's proximity to a specific location. I developed a custom hook to determine if the user is nearby. However, I'm facing an issue where the hook constantly returns a new value of true, ...

Guide on moving elements to a different list with the help of the Angular DragDrop CDK Service

I encountered a unique situation while working on my project where I needed to implement a drag and drop feature for multiple lists using Angular CDK's DragDrop service. While I was able to successfully move items within a single list, transferring an ...

Error with Typescript types when using Styled Components

After successfully setting up styled-components in react-native, I encountered an issue while trying to use it in a simple example with react-native-web: import * as React from 'react'; import styled from 'styled-components'; export d ...

Using an Object as a parameter in a Typescript function

I am currently working on an Angular component that includes a function. Within this function, I need to pass an Object as a parameter and invoke the function with these parameters. It has been some time since I last worked with Angular, where "any" was ty ...

When I utilize the redux connect function, the class information in my IDE (PhpStorm/WebStorm) seems to disappear

When I use the connect function from redux, it seems to hinder my IDE (PhpStorm) from "Find Usages" on my classes. This is likely because connect returns any, causing the type information from the imported SomeClass file to be lost. export default connect ...

Issue with font-size changes using css variables in Angular not updating in browser for specific fields

Utilizing CSS variables, I have implemented a feature that allows users to adjust the font size to small, medium, or large. While this functionality works correctly for most fields, there are certain instances where the value is applied but not displayed. ...

Sequelize 5: Error encountered when using many-to-many functions

Currently, I am working with Sequelize 5.21.7 (in a next.js application) to establish a many-to-many relationship between the Image and Tag models. However, I encounter TypeErrors when attempting to utilize the imageInstance.addTag() and imageInstance.getT ...

Converting ASP .Net Core Dto's and Controllers into TypeScript classes and interfaces

My concept involves incorporating two key elements: Converting C# Dto's (Data-transfer-objects) into TypeScript interfaces to ensure synchronization between client-side models and server-side. Transforming ASP .Net Core controller endpoints into Typ ...

What is the best way to instruct Angular to exclude certain sections of a template from compilation?

My scenario is quite straightforward: I am looking to create documentation for my library by including code examples along with demo sections. My main query is this: How can I make one part of the code compilable and executable, while leaving the other pa ...

Keeping an Rxjs observable alive despite encountering errors by simply ignoring them

I am passing some values to an rxjs pipe and then subscribing to them. If there are any errors, I want to skip them and proceed with the remaining inputs. of('foo', 'bar', 'error', 'bazz', 'nar', 'erro ...

"Question: How can I make a checkbox display 'true' and '

After creating checkboxes in Angular, I noticed that when all checkboxes are selected, it displays true and false instead of the actual values. In my reactive form, I need to retrieve the checkbox values instead of true and false. <input type="chec ...

Is there a way to create a reusable type annotation for declaring functions in Typescript?

type Func = (param:string) => void // implementing a function expression const myFunctionExpression:Func = function(param) { console.log(param) } Within the TypeScript code snippet above, I have utilized a type alias to define the variable in a func ...