Skip to content

Commit e928777

Browse files
thekibaKwintenP
authored andcommitted
content(rxjs): prevent memory leaks with refCount
1 parent 9c87635 commit e928777

File tree

1 file changed

+4
-3
lines changed

1 file changed

+4
-3
lines changed

content/rxjs/use-ngifas.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ We can also make our Observable hot so that the Observable will no longer trigge
7272

7373
This fixes our problem because it means it doesn't matter anymore if we have multiple subscriptions.
7474

75-
To do this, we can use for example the `publishReplay` and `refCount` operators.
75+
To do this, we can use for example the `shareReplay` operator.
7676

7777
```ts
7878
@Component({
@@ -81,13 +81,14 @@ To do this, we can use for example the `publishReplay` and `refCount` operators.
8181
})
8282
export class SomeComponent implements OnInit, OnDestroy {
8383
sharedData$ = data$.pipe(
84-
publishReplay(1),
85-
refCount()
84+
shareReplay({ bufferSize: 1, refCount: true })
8685
);
8786
...
8887
}
8988
```
9089

90+
> Note: we should specify `refCount: true` to prevent possible memory leaks.
91+
9192
# Resources
9293

9394
- [Multicasting operators in RxJS](https://blog.strongbrew.io/multicasting-operators-in-rxjs/) by Kwinten Pisman

0 commit comments

Comments
 (0)