replayShare alters the sequence

After creating an Observable from the array [1, 2, 3, 4, 5] and logging each iteration, the output is as expected: 1,2,3,4,5.

However, when adding shareReplay(2), only the last two iterations - 4,5 are displayed. This result is confusing as I was anticipating to see 1,2 as the output.

numbers$: Observable<number> = from([1, 2, 3, 4, 5, 6, 7]);

ngOnInit() {
this.numbers$.pipe(
  shareReplay(2),
  refCount()
).subscribe(data => console.log(data));

}

The issue can be seen on stackBlitz: https://stackblitz.com/edit/hello-angular-6-yb387t?file=src/app/app.component.ts

Answer №1

ShareReplay allows for replaying the last two emitted values from the observable. If you require the first two values instead, consider using take(2). For scenarios where both functionalities are necessary, shareReplay can still be utilized:

this.numbers$.pipe(
  take(2),
  shareReplay()
).subscribe(data => console.log(data));

It's important to note that when employing shareReplay, there is no need for refCount since it is already integrated within shareReplay. A comprehensive explanation on this topic can be found here.

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

Is there a way to restrict props.children.some to only accept image types?

Currently troubleshooting the following issue: An error is occurring: 'Property 'type' does not exist on type 'true | ReactChild | ReactFragment | ReactPortal'. Property 'type' does not exist on type 'string'. ...

Navigating the onSubmit with Formik in React: Tips and Tricks

I have a query regarding my usage of Formik in my React application. Within the onSubmit function, I am making an API call to a service. If this call fails, I want to immediately stop the rest of the submission process without executing any further action ...

Create a variable within a JavaScript function that retains its value each time the function is called

Is there a way to declare a variable inside a JavaScript function so that it initializes once with the following behavior? The initialization should occur only once. function counter() { let_special count = 0; count++; if (count == 3) { count = ...

Enhancing User Interfaces with TypeScript Accordions

Looking for a way to expand the sub-menu when the SETTINGS menu is clicked using typescript. Here is the list structure: <li> <a href="#"> <i class="fa fa-cogs fa-fw"></i> <span>SETTINGS</span> </a> ...

Error with tsc rootDir due to Docker's installation of yarn in root directory

I am encountering a problem with my node project which serves a simple "hello world" server. Everything runs smoothly when I run it locally, but when I try to run it inside Docker, a /opt/yarn-v1.21.1 folder is created which leads to a rootDir error: erro ...

Uncovering TypeScript's Type Inference Power Through the keyof Keyword

Recently, I encountered a situation where I needed to utilize an abstract class. export abstract class ABaseModel { public static isKeyOf<T>(propName: (keyof T)): string { return propName; } } Following that, I also created another class wh ...

I am encountering difficulties using useContext alongside Typescript in my React project

I am currently learning TypeScript and attempting to develop a Todo application using the 'useContext' hook in TypeScript-React. I am encountering an issue with passing TodoContextProviderProps as a prop below, to my Provider function. import Re ...

Is it possible to receive upload progress events in HttpInterceptor but not through HttpClient calls?

Utilizing HttpInterceptor in my project, I have an Http service that calls http methods with HttpClient. I am currently working on retrieving the upload progress and encountering two issues: Firstly, the progress event is only being captured by the HttpIn ...

Error: Failed to load chunk 552 due to chunk loading issue

Currently in the process of migrating Angular 12 to version 13. The migration itself was successful, however, upon running the project in the browser post a successful build, the application fails to display. On checking the console, I encountered the foll ...

.net 6 Attribute routes resulting in a 404 error response

Last week I had a similar question, but now I'm facing an issue with a different controller. I'm baffled as to why I keep getting a 404 error. API Controller... [Route("api/[controller]")] [ApiController] public class FilesController { ...

Is there a way to prevent Ngrx Store from resetting when the browser is refreshed? Learn how to maintain the application's

One component triggers an action this.store.dispatch({type : STORE_TEAMCREST , payload : team.crestURI}); and another component retrieves the value from the store using this.store.select(state => state.table.teamCrest).subscribe(data => this.team ...

Implementing Angular's Advanced Filtering Across Multiple Data Fields

I am looking to create a custom filter for a list. Here is an example of the Array of Objects: myList: [ { "id": 1, "title":"title", "city":"city name", "types":[ { ...

Displaying data from an Angular subscription in a user interface form

I am attempting to transfer these item details to a form, but I keep encountering undefined values for this.itemDetails.item1Qty, etc. My goal is to display them in the Form UI. this.wareHouseGroup = this.formBuilder.group({ id: this.formBuilder.contr ...

Thread of Worker, different document from oneself, unable to locate module

Is there a way to separate my worker thread into an external file from the main runtime file? Currently, my folder structure looks like this: src/ > service.ts // my 'main' > thread/ >> test.js In my service.ts file, I have the follo ...

How can the encapsulation parameter in Angular 2's component section be utilized effectively?

https://i.sstatic.net/CmVG2.pngI need some clarification on the encapsulation parameter in Angular 2. I encountered an error message when trying to utilize this parameter. How can I resolve this issue? @Component({ selector: 'app-add', ...

The mkdir function has encountered an invalid argument error with code EINVAL

I am encountering an issue with my Angular app build command. I have npm version 6.14 and node version 14.15.4 installed on my Windows 7 32-bit system. The error message I receive when running 'npm run build' is: 95% emitting index-html-webpack-p ...

Tips for displaying dynamic HTML content in Angular

Is there a way to dynamically display HTML content within {{ name }}? let firstName = "Faisal"; let data1 = "<h1>{{firstName}}</h1>"; let lastName = "Khan"; let data2 = "<h1>{{lastName}}</h1>"; How can I achieve the desired outpu ...

Retrieving the Windows user's username via Typescript or Javascript is an essential

How can I retrieve the current Windows username in my Angular 4 application using TypeScript or JavaScript? I am specifically working with the Chrome browser. ...

I am facing difficulty in retrieving data from Firestore using Angular

I've been utilizing the AngularFireList provided by @angular/fire/database to retrieve data from firestore. However, despite having data in the firestore, I am unable to fetch any information from it. import { Injectable } from '@angular/core&apo ...

Nesting *ngFor in Angular allows for a powerful way

I have a table that contains two nested iterations. The first three columns iterate through an array of objects (items), while the fourth column should iterate through an array of numbers (total=[30,70,100]). <table class="table"> <thead cla ...