Skip to content

Commit fd35b5f

Browse files
committed
fix(docs): replace deprecated multicast() with connectable()
https://rxjs.dev/deprecations/multicasting
1 parent c459a8c commit fd35b5f

File tree

1 file changed

+12
-8
lines changed

1 file changed

+12
-8
lines changed

apps/rxjs.dev/content/guide/subject.md

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -71,14 +71,16 @@ A "multicasted Observable" passes notifications through a Subject which may have
7171

7272
<span class="informal">A multicasted Observable uses a Subject under the hood to make multiple Observers see the same Observable execution.</span>
7373

74-
Under the hood, this is how the `multicast` operator works: Observers subscribe to an underlying Subject, and the Subject subscribes to the source Observable. The following example is similar to the previous example which used `observable.subscribe(subject)`:
74+
Under the hood, this is how the `connectable` operator works: Observers subscribe to an underlying Subject, and the Subject subscribes to the source Observable. The following example is similar to the previous example which used `observable.subscribe(subject)`:
7575

7676
```ts
77-
import { from, Subject, multicast } from 'rxjs';
77+
import { connectable, from, Subject } from 'rxjs';
7878

7979
const source = from([1, 2, 3]);
80-
const subject = new Subject<number>();
81-
const multicasted = source.pipe(multicast(subject));
80+
const subjectFactory = () => new Subject<number>();
81+
const multicasted = connectable(source, {
82+
connector: subjectFactory,
83+
});
8284

8385
// These are, under the hood, `subject.subscribe({...})`:
8486
multicasted.subscribe({
@@ -92,7 +94,7 @@ multicasted.subscribe({
9294
multicasted.connect();
9395
```
9496

95-
`multicast` returns an Observable that looks like a normal Observable, but works like a Subject when it comes to subscribing. `multicast` returns a `ConnectableObservable`, which is simply an Observable with the `connect()` method.
97+
`connectable` returns an Observable that looks like a normal Observable, but works like a Subject when it comes to subscribing. `connectable` returns a `ConnectableObservable`, which is simply an Observable with the `connect()` method.
9698

9799
The `connect()` method is important to determine exactly when the shared Observable execution will start. Because `connect()` does `source.subscribe(subject)` under the hood, `connect()` returns a Subscription, which you can unsubscribe from in order to cancel the shared Observable execution.
98100

@@ -116,11 +118,13 @@ Consider the following example where subscriptions occur as outlined by this lis
116118
To achieve that with explicit calls to `connect()`, we write the following code:
117119

118120
```ts
119-
import { interval, Subject, Subscription, multicast } from 'rxjs';
121+
import { connectable, interval, Subject, Subscription } from 'rxjs';
120122

121123
const source = interval(500);
122-
const subject = new Subject<number>();
123-
const multicasted = source.pipe(multicast(subject));
124+
const subjectFactory = () => new Subject<number>();
125+
const multicasted = connectable(source, {
126+
connector: subjectFactory,
127+
});
124128
let subscription1, subscription2: Subscription, subscriptionConnect;
125129

126130
subscription1 = multicasted.subscribe({

0 commit comments

Comments
 (0)