How to use RxJs BehaviorSubject in an Angular Interceptor to receive incoming data

Being a newcomer to rxjs, I grasp most operators except for the specific use case involving BehaviorSubject, filter, and take.

I am working on renewing an oauth access and refresh token pair within an Angular interceptor. While reviewing various codes from different sources, it appears that there is a common approach to tackling this task. However, there is one aspect that perplexes me.

For instance, in this example, requests are delayed until fresh access and refresh tokens are obtained.

private refreshSubject = new BehaviorSubject<any>(null);

// ...etc.

if (!this.refreshInProgress) {
  // request new access and refresh tokens from server
  // populate refreshSubject once tokens received
}
else {
  return this.refreshSubject.pipe(
    filter(result => result !== null),                      // <---- ???
    take(1),                                                // <---- ???
    switchMap(() => next.handle(this.addToken(request)))
  );
}

In all such instances, it is explained that requests are held up until refreshSubject receives the updated tokens, with emphasis on the role of filter.

This is the aspect that baffles me. The official rxjs documentation does not explicitly mention how or where the waiting and queuing process takes place when using filter.

Answer №1

Initially, the value of the BehaviorSubject is set to "NULL". When making a request or socket connection to retrieve a new token, we must filter out values that are not NULL using the "filter" operator.

filter(result => result !== null), 

Only values that are not NULL will be emitted to the next step in the pipeline of the BehaviorSubject.

We then need to only select one of the emitted values from the BehaviorSubject (necessary for obtaining the new token). For this, we utilize the "take" operator.

take(1),

This ensures that only ONE value is emitted, even if multiple values were emitted previously, only the first one will be passed along to the subsequent step.

Following this, you can handle the values with "switchMap". Keep in mind that the BehaviorSubject emits the current value (the last emitted value) each time a new subscription is made.

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

The "data path" should not include any extra elements or properties, such as allowed CommonJS dependencies

I'm currently running Angular v10 and facing an issue when trying to start my .net core / Angular application. Despite searching for a solution and updating everything to the latest versions, the problem persists. Although there are no errors report ...

Angular 11 is causing confusion by incorrectly applying the Http interceptor to sibling modules

Here is the structure of my modules: https://i.sstatic.net/zO9dE.png The HTTP interceptor I provided in core.module.ts is affecting the HTTP client in the translation.module.ts. This is how my core module is set up: @NgModule({ declarations: [DefaultLa ...

Angular ngrx store not updating with the most recent value

When navigating to an "Account Detail" page and passing a parameter, it triggers a call to the Web Api for fetching account details. The issue arises when loading the "Account Detail" page for the first time - nothing is displayed. Subsequently, switching ...

Tips for utilizing generics to determine the data type of a property by its name

I am seeking a method to determine the type of any property within a class or a type. For instance, if I had the classes PersonClass and PersonType, and I wanted to retrieve the type of the nationalId property, I could achieve this using the following cod ...

The Vercel/NextJS deployment does not delay the completion of the serverless function until the email is sent via Azure

Upon a user's registration, I am attempting to send a registration/account activation email. While the email sends successfully via Azure's email services when running on localhost, deployments on Vercel do not trigger the email (although the use ...

Tips for integrating JavaScript libraries with TypeScript

I'm looking to add the 'react-keydown' module to my project, but I'm having trouble finding typings for it. Can someone guide me on how to integrate this module into my TypeScript project? ...

What is the process for type checking a Date in TypeScript?

The control flow based type analysis in TypeScript 3.4.5 does not seem to be satisfied by instanceof Date === true. Even after confirming that the value is a Date, TypeScript complains that the returned value is not a Date. async function testFunction(): ...

Is there a way I can utilize the $timeout and $interval functionalities that were implemented in angular.js, but for the Angular framework?

When working with Angular.js, I made use of the $timeout and $interval functions (which are similar to setInterval and setTimeout in JavaScript). $timeout(function(){}) $interval(function(){},5000) To stop the interval, I utilized $interval.cancel($scop ...

In Typescript, it is not possible to assign the type 'any' to a string, but I am attempting to assign a value that is

I'm new to TypeScript and currently learning about how types function in this language. Additionally, I'm utilizing MaterialUI for this particular project. The issue I'm encountering involves attempting to assign an any value to a variable ...

The parameter type '(value: any) => any[]' does not match the expected parameter type 'string[]'

I'm encountering an issue where I can't push a value to a state array due to a TS error. Let me highlight the relevant components where the error occurs. The error specifically lies in the child component, within the handleValue(value => [...v ...

Exposing the method to the outside world by making it public in

I have a situation where I have a base class with a protected method called foo, and a child class that needs to make this method publicly accessible. Since this method will be called frequently, I am looking for a more efficient solution to avoid unnecess ...

The formatting in vscode does not apply to .tsx files

For a while now, I've relied on the Prettier extension in Visual Studio Code for formatting my code. However, since switching to writing React with Typescript, I now need to configure Prettier to format .tsx files accordingly. ...

What is preventing the dependency injection of AuthHttp (angular2-jwt) into a component?

UPDATE: Success! Problem Solved After much trial and error, I finally discovered the solution to my issue. It turned out that the problem lied in a simple configuration mistake. To rectify this, I made changes to both my package.json (dependencies section ...

What is the significance of TypeScript's dual generic typing feature?

defineListenerShape< EventName extends string, EventData extends { [key in EventName]: unknown; } > = <E extends EventName>(data: EventData[E]) => void; enum EventName { Click = 'click', Hover = 'hover' ...

Unlocking the secrets of integrating Vuex store with JavaScript/TypeScript modules: A comprehensive guide

I am working on a vue application and I have a query. How can I access the store from javascript/typescript modules files using import/export? For example, if I create an auth-module that exports state, actions, mutations: export const auth = { namesp ...

Tips for choosing an HTML element using Playwright with TypeScript

My goal is to use playwright with typescript in order to select a specific html element. The element I am trying to target has the class "ivu-select-dropdown" and a specific style. <div class="ivu-select-dropdown" style="position: absolut ...

The array within the JSON object holds vital information [Typescript]

I have some data stored in an Excel file that I want to import into my database. The first step was exporting the file as a CSV and then parsing it into a JSON object. fname,lname,phone Terry,Doe,[123456789] Jane,Doe,[123456788, 123456787] Upon convertin ...

Unraveling the mystery of decoding a jwt token

Every time I attempt to validate a user token, I keep encountering Error 500. function verifyToken(req, res, next) { if(!req.headers.authorization){ return res.status(401).send('Unauthorized request') } let token = req.headers.authorization. ...

Expanding a given type using Typescript

My goal is to construct a custom table using Angular, where I aim to define a TableItem type that enforces the presence of a label property for every item added to the table. For instance, consider this example: <app-my-table [items]="items&qu ...

The value of the filename property cannot be determined as it is undefined

Hey everyone, I'm currently working on a project using nestjs and reactjs. I encountered an error when trying to add a document that reads: "Cannot read properties of undefined (reading 'filename') in multer.config.ts" import { diskStorag ...