RXjs: Reverting a value to its original state after a specified duration

Within this service/state:

export class SpinnerService {
  public throttleTime: number = 10;
  public isLoading$: BehaviorSubject<boolean> = new BehaviorSubject<boolean>(false);

  constructor() {}

  public showLoader(): void {
    this.isLoading$.next(true);
  }

  public hideLoader(): void {
    this.isLoading$.next(false);
  }

  public get isLoadingAPIVal$(): Observable<boolean> {
    return this.isLoading$.pipe(throttleTime(this.throttleTime), shareReplay());
  }
}

This is where the logic for displaying the app loading animation is stored. The value can be set and retrieved in multiple components simultaneously or with delays. For example, setting isLoading$ to true in one component, and then after 0.004 seconds in another. It works smoothly except for one scenario.

Sometimes, I need to reset isLoading$ to false if its last true value was set over 20 seconds ago.

How can I ensure it is set to false after being true for more than 20 seconds?

I attempted the following approach:

  constructor() {
    this.isLoading$
      .pipe(
        filter((val) => !!val),
        timeout(20000),
      )
      .subscribe(() => {
        this.isLoading$.next(false);
      });
  }

However, it seems like this method is not working as expected and takes the first true value instead.

Answer №1

Here is a suggestion for what you may want to implement:

public get isLoadingAPIVal$(): Observable<boolean> {
  return merge(
    this.isLoading$,
    this.isLoading$.pipe(
      debounceTime(20000),
      map(() => false),
    ),
  ).pipe(
    throttleTime(this.throttleTime),
    shareReplay(),
  );
}

This function accomplishes the following:

  1. It delays signals from isLoading$ by 20 seconds before emitting a new signal:
    this.isLoading$.pipe(
      debounceTime(20000),
    
  2. It emits false values after the delay:
    map(() => false),
    
  3. It merges the original stream of signals with the delayed "false" value after 20 seconds:
    merge(
      this.isLoading$,
      this.isLoading$.pipe(
        debounceTime(20000),
        map(() => 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

Create an instance using the window object in JavaScript

Having an issue when trying to instantiate a class using the window object. I have a namespace called UTIL and within it, there is a class defined as follows: var UTIL = { Classes : {}}; UTIL.Classes.ObservationVal = function(state, id, type, context, pe ...

Unable to access property 'hasAttribute' of null value

I'm experiencing an issue with Material Design Bootstrap. I installed it using NPM, but when I visit my website, I encounter the following error: Uncaught TypeError: Cannot read property 'hasAttribute' of null at r (app.js:19116) at ...

Issues with fundamental JavaScript client-side code

As a newcomer to the world of javascript and jQuery, I am diving into my first experiment with javascript. My initial focus has been on changing questions by clicking next or previous buttons. The goal is to create a dynamic quiz webpage that updates quest ...

Using HTML to execute a JavaScript function that transforms images

The script cutImageUp has been previously discussed on SE in a thread about Shattering image using canvas. However, I have encountered a different issue while attempting to use it. The HTML code I implemented seems to be ineffective, and despite my efforts ...

Utilizing Angular's capabilities to smoothly transfer objects between controllers

Currently, I am in the midst of developing an AngularJS application integrated with a C# Web API. I have two controllers: A and B. In controller A, there is a list of objects. When I click "Add" (in between two list items), I am redirected to controller ...

My Node.js code has encountered an unexpected end of input error

const express = require("express"); const bodyParser = require("body-parser"); const ejs = require("ejs"); const mongoose = require('mongoose'); const app = express(); app.set('view engine', 'ejs'); app.use(bodyParser.url ...

Having trouble looping through an array in Angular 2?

I am currently using a FirebaseObjectObservable to retrieve the value of a property from my firebase database. The property can have multiple values, so I stored them in a local array variable. However, I ran into an issue while trying to iterate through ...

The socket.io client in my JavaScript code is failing to receive the necessary event

Currently, I am in the process of configuring a socket.io chat feature with an expressjs backend and sveltejs frontend. I have established a custom namespace named 'chat' where a new room is generated upon a 'join' request. My appro ...

Troubleshooting Nested Handlebars Problem

After creating a customized handlebar that checks for equality in this manner: Handlebars.registerHelper('ifEquals', (arg1, arg2, options) => { if (arg1 == arg2) { return options?.fn(this); } return options?.inverse(t ...

What methods can be used to customize the font and background color of a website for different user groups?

Trying to incorporate a template into my project. My client has requested the following: The regular user area should feature a blue background. The professional user area should have an orange background. Is there a way to set up a condition to change ...

Exploring the vertices of a single face of a cube using three.js

My current project involves manipulating the x position of all coordinates on a single face of a cube. Here is my current method: var wDepth = 200; var hDepth = 200; var geo = new THREE.CubeGeometry( 20, 40, 40, 20, wDepth, hDepth); for ( var i = ...

Tips for invoking a function from one React component to another component

Currently, I am working on two components: one is Game and the other is PickWinner. The Game component serves as the parent component, from which I need to call the pickWinner function in the PickWinner component. Specifically, I want to trigger the startP ...

Using the excel.js module in conjunction with node.js to create distinct columns within a header row

I am facing an issue with Excel.js while trying to add a header row to a CSV file. It seems that all the columns in the row are getting merged into one cell instead of staying separate. Does anyone know how to properly separate the columns? https://i.sst ...

Unable to display grid items in React material-ui grid list

export interface FlatsGridProps { flats: IClusterFlats[]; } export const FlatsGrid: React.StatelessComponent<FlatsGridProps> = (props: FlatsGridProps) => { if (props.flats.length === 0) { return (<div> empty </di ...

Angular firebase Error: The parameter 'result' is missing a specified type and is implicitly assigned the 'any' type

I have encountered an issue with the code I am working on and both the result and error are throwing errors: ERROR in src/app/login/phone/phone.component.ts(48,75): error TS7006: Parameter 'result' implicitly has an 'any' type. s ...

Guide to saving a JSON array to a file using a writeStream in Node.js

I created a Node.js script to scrape data from a website, with the process involving iterating through pages to collect structured data. Every page yields an array of objects as the data I extract. My initial plan was to utilize the fs.createWriteStream( ...

Managing the creation of a fresh VueJs object with checkboxes enabled for CRUD operations

Hello fellow developers! I am currently working on a shop card project and I'm looking to add a new product to the collection of sale elements. Let's consider the JSON structure retrieved from the backend: "products": [ { "product_prov ...

Angular reactive forms allow you to create dynamic forms with fields that change

Consider the following data structure: formAviso: FormGroup; deapartamentos: [ {nombre: 'Amazonas', codigo: 41}, {nombre: 'Ancash', codigo: 43}, {nombre: 'Apurimac', codigo: 83}, ... ] constructor() { this.formAvi ...

Is there a way to implement hover behavior for a Material-UI Button within a ButtonGroup component?

When using MUI v5, I am encountering an issue where the first button in the code provided is only half working. The button is initially colored red (both the border and text), however, upon hovering over it, the color of the border changes to blue. This is ...

Getting the WebElement object by manually clicking an element while in an active WebDriver Session

I am currently developing a Java Swing application for managing object repositories in Selenium scripts. This application will launch a WebDriver instance and allow users to manually navigate to the desired element for inspection. My goal is to capture th ...