Questions tagged [subject-observer]

Use this tag for questions related to an Observer for a Subject. Observers can subscribe to the subject to receive the last (or initial) value and all subsequent notifications.

119 questions
39
votes
3 answers

How can PublishSubject and BehaviorSubject be unsubscribed from?

Under the subjects package you have classes like PublishSubject and BehaviorSubject which I suppose can be described as some usable sample Observables. How can these subjects be unsubscribed from? There is no unsubscribe method and calling…
arinte
  • 3,660
  • 10
  • 45
  • 65
37
votes
5 answers

RXJS Observable - How to call next from outside of the Observable's constructor

I am building a service which exposes an Observable. In this service I receive external function calls which should trigger a next call on the Observable so that various consumers get the subscribe event. During Observer constructor I can call next…
Joshua Ohana
  • 5,613
  • 12
  • 56
  • 112
14
votes
1 answer

Why does piping a BehaviorSubject create an AnonymousSubject in RxJS?

When creating an RxJS BehaviorSubject, it stays a BehaviorSubject until it's pipe'd. As soon a pipe'd version is returned, it becomes an AnonymousSubject. Examples: // Instance of `BehaviorSubject` const behaviorSubject$ = new BehaviorSubject({…
Kevin Ghadyani
  • 6,829
  • 6
  • 44
  • 62
13
votes
2 answers

Async pipe not working with Subject

I have the following BehaviorSubject in a service: isAuthenticated = new BehaviorSubject(false); And I am using it as follows in a component: authenticated: Observable; constructor(private accountService: AccountService) {…
AngularDebutant
  • 1,436
  • 5
  • 19
  • 41
7
votes
2 answers

BehaviorSubject with boolean value is not working as intended

I have implemented a simple BehaviorSubject, import {BehaviorSubject} from "rxjs"; class MyWeirdoClass { constructor() {} private st: Subject = new BehaviorSubject(null); changeSt(val:boolean){ this.st.next(val); …
All Іѕ Vаиітy
  • 24,861
  • 16
  • 87
  • 111
6
votes
1 answer

how to add an object to an observable of array

I have an angular project with a service called BookService. private books: Subject; getBookList(skip:number = 0,limit:number = 0): Subject { return this.books; } addBookToList(book:Book) { } this service help me to monitor the…
Yedidya kfir
  • 1,419
  • 3
  • 17
  • 32
5
votes
1 answer

Detect when a Subject has no more subscriptions

I'm implementing an angular service that lets consumers observe various values based on their id: The essence of it looks like this: private subjects = new Map>(); public subscribe(id: number, observer: any): Subscription { …
Robert Hegner
  • 9,014
  • 7
  • 62
  • 98
5
votes
5 answers

Wrap an Observable. Do something before and after each emitted value

I'd like to build a wrapper class, that does something before and after each emitted value of an Observable. Here's what I came up with: class Wrapper { wrapped$: Observable; _dataSubject = new Subject(); data$ =…
Benjamin M
  • 23,599
  • 32
  • 121
  • 201
5
votes
1 answer

Angular 4 RxJs Observable Subjects array not updating with new object

So recently i've learnt about Subjects, and i'm trying to use them in a personal project. I have a service that fetches data from a json file and casts it to an "Article" type. Article is a custom class that holds information on a blog article. My…
simon_www
  • 489
  • 2
  • 5
  • 13
5
votes
2 answers

Angular Service with Subject Observable calling next() is not seen by Component subscriptions

I want to retrieve an updated object value (borrower) from an Angular Service. I've created an RxJS Subject in the Service so that multiple components can subscribe to it and get the updated value. When I subscribe to the Subject through the…
MattTreichel
  • 1,418
  • 3
  • 18
  • 35
4
votes
1 answer

RxJS & Angular 6 - observables, subjects and basic get and add operations on arrays

I'm struggling to wrap my head around observables/observers/subjects and angular. I've looked through a number of tutorials covering observable basics and the basic subscription scenario makes sense. These tutorials do not seem to cover adding to…
Web Dev
  • 2,677
  • 2
  • 30
  • 41
4
votes
1 answer

How can I use the same observable with multiple mappings?

My code has a Subject which, when new values are added triggers a HTTP request which returns an Observable. I want to process this data in two different ways (using the same data) and use the resulting Observables stored in group and times as values…
Kian Cross
  • 1,818
  • 2
  • 21
  • 41
4
votes
1 answer

Angular Unit Test Observable/Subject with Karma

I am trying to test a Subject change in my component, but the coverage never enters into the subscribe function. titlebar-search.component.ts export class TitlebarSearch implements OnInit { @ViewChild('titleSearchInput') titleSearchInputEl:…
4
votes
1 answer

Angular 2 Subject.next not working once the Observable its listens throws an error

I'm struck at a scenario, where I have Angular service class (WeatherService) which performs a REST call to an external api and fetches data in searchWeather() function. I have a component WeatherSearchComponent, which has input field search in its…
Ganesh chaitanya
  • 638
  • 6
  • 18
3
votes
2 answers

Difference between using a variable and using a subject in angular service to create the global variable which can be used throughout the application?

Angular 8 provides us rxjs library , we can use subject from that library to set data that can be used gloabally in our application by declaring it in service file and this same we can do is by declaring just a variable in service file and using it…
prince
  • 31
  • 4
1
2 3 4 5 6 7 8