Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

rxjs Subject.create deprecated - what should I use instead?

Tags:

angular

rxjs

I have this code in a service in my Angular app:

const subby = Subject.create(observer, observable)

But my IDE correctly marks Subject.create as deprecated. What should I use instead thanks? I've tried new Subject(observer, observable) but no joy. TIA.

RxJS version: 6.4.0

Angular version: 7.2

like image 843
danday74 Avatar asked Apr 06 '19 17:04

danday74


People also ask

Why use subject instead of Observable?

In comparison to a regular Observable, a Subject allows values to be multicasted to many Observers. A Subject and its subscribers have a one-to-many relationship. A Subject can be an Observable as well as an Observer. They hold a registry of many listeners to multiple Observables.

What is the difference between Observable and a subject in RxJS?

An RxJS Subject is a special type of Observable that allows values to be multicasted to many Observers. While plain Observables are unicast (each subscribed Observer owns an independent execution of the Observable), Subjects are multicast. A Subject is like an Observable, but can multicast to many Observers.

What is next in Subject next angular?

next() The subject next method is used to send messages to an observable which are then sent to all angular components that are subscribers (a.k.a. observers) of that observable.

What is Subject and BehaviorSubject in RxJS?

BehaviorSubject is a variant of a Subject which has a notion of the current value that it stores and emits to all new subscriptions. This current value is either the item most recently emitted by the source observable or a seed/default value if none has yet been emitted.


1 Answers

Looking at the source code, the static function Subject.create(destination, source) is just a wrapper around new AnonymousSubject<T>(destination, source).

If you're just looking to deal with the warning, you can change your code to

import { AnonymousSubject } from 'rxjs/internal/Subject';

const subby = new AnonymousSubject<T>(observer, observable);

RxJs has documented their motivation for this change here. The important quote:

Subject.create doesn't actually create a Subject, but rather an AnonymousSubject, which I would REALLY like to rename as FrankenSubject because that describes what it is, you basically glue an Observer to an Observable and call it a "Subject".

In short, using Subject.create (or the AnonymousSubject object) is a confusing way of accomplishing your goals.

You can look at the source code for this class here, but the gist is that it's a pointless class that obfuscates what's going on. In the code, you can see that destination and source (the Observer and Observable arguments) have NO interaction.

So, the "right way" to fix the code is to murder this object entirely and be more explicit about how the relevant events are routed.

For example:

// This code...
const subby = Subject.create(observer, observable);
subby.subscribe((value: value) => console.log(value));
subby.next("Test");

// Should be replace by this code
// Nothing
observable.subscribe((value: value) => console.log(value));
observer.next("Test");

I also found the following warnings about using Subject.create that may be relevant for future readers:

  • Subjects created with Subject.create can't unsubscribe
  • Subject Subscribe method not working as expected
like image 189
Vlad274 Avatar answered Nov 13 '22 06:11

Vlad274