-
Couldn't load subscription status.
- Fork 9
[2025-10-03] Czy wiesz, że za pomocą operatora shareReplay możesz cac… #273
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| --- | ||
| layout: post | ||
| title: Czy wiesz, że za pomocą operatora shareReplay możesz cache'ować dane z observable? | ||
| description: "" | ||
| date: 2025-10-03T08:00:00+01:00 | ||
| published: true | ||
| didyouknow: false | ||
| lang: pl | ||
| author: ptatarski | ||
| image: /assets/img/posts/2025-10-03-czy-wiesz-ze-za-pomoca-operatora-sharereplay-mozesz-cache-owac-dane-z-observable/thumbnail.webp | ||
| tags: | ||
| - angular | ||
| - rxjs | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. rxjs dałbym jako pierwszy tag (on się wyświetli na liście) |
||
| - observable | ||
| --- | ||
|
|
||
| Często zdarza się, że nie chcemy za każdym razem ponownie wykonywać całej logiki z danego *Observable* — | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Observable to nazwa klasy dałbym w apostrofach żeby się sformatowało jak kod |
||
| zamiast tego wolimy przechować jego wynik w pamięci. W Angularze (i ogólnie w RxJS) możemy to zrobić za pomocą operatora **`shareReplay`**. | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. przy shareReplay zostawiłbym same apostrofy (nie przekombinowujmy z formatowaniem) |
||
|
|
||
| ## Czym jest shareReplay? | ||
| Operator `shareReplay` (tak jak operator `share`) pozwala na konwersję z *cold* do *hot* observable, | ||
| tak aby każda subskrypcja korzystała z tej samej emisji observable. Dodatkowo dane są zapisywane w wewnętrznym **ReplaySubject** | ||
| (w przeciwieństwie do operatora share), przez co każda nowa subskrypcja dostaje ostatnią wyemitowaną wartość (lub wartości jeżeli `bufferSize > 1`). | ||
|
|
||
| ## Przykład użycia | ||
| W przykładzie poniżej generujemy losową liczbę. Normalnie przy każdej subskrypcji otrzymalibyśmy nowy wynik, | ||
| ale dzięki `shareReplay` obie subskrypcje dostają dokładnie tę samą wartość. | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. tą samą wartość There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. tę wartość |
||
| ```typescript | ||
| const source$: Observable<number> = of(Math.round(Math.random() * 1000)) | ||
| .pipe(shareReplay()); | ||
| source$.subscribe((x: number) => console.log('s1: ', x)); | ||
| source$.subscribe((x: number) => console.log('s2: ', x)); | ||
| ``` | ||
| W efekcie w konsoli pojawi się ta sama liczba w obu subskrypcjach, np.: | ||
| ```shell | ||
| s1: 742 | ||
| s2: 742 | ||
| ``` | ||
| ## Opcje konfiguracji | ||
| Operator `shareReplay` posiada także dodatkowe opcje do skonfigurowania. | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. dwukropek zamiast kropki |
||
| - `bufferSize` - określa wielkość wewnętrznego ReplaySubject (domyślnie przechowuje ostatnią wartość), | ||
| - `refCount` - określa, czy w momencie, gdy liczba subskrybentów wyniesie 0 shareReplay ma odsubskrybować się od źródłowego observable, co spowoduje wyczyszczenie cache (domyślnie false), | ||
| - `windowTime` - ograniczenie czasu przechowywanych wartości (domyślnie nie ogranicza). | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ogranicza czas przechowywania wartości (czego domyślnie nie robi) |
||
|
|
||
|
|
||
| ### Przykład z dodatkowymi opcjami konfiguracji | ||
| Załóżmy, że chcemy cache’ować więcej niż jedną wartość i dodatkowo kontrolować czas ich przechowywania. | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Wydaje mi się, że "dodatkowo" to wtrącenie i tam powinien być jakiś przecinek, ale niech się wypowie nasz specjalista od języka polskiego ;) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. tutaj bez przecinka :D |
||
| ```typescript | ||
| shareReplay({ bufferSize: 3, refCount: true, windowTime: 500 }) | ||
| ``` | ||
| - zostaną umieszczone w buforze 3 ostatnie wartości, | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Brakuje mi jakiejś części zdania przed tym wypunktowaniem |
||
| - każda z osobna będzie miała czas życia ustawiony na pół sekundy, | ||
| - w momencie, gdy nie będzie żadnej aktywnej subskrypcji, bufor zostanie wyczyszczony. | ||
|
|
||
| ### Przydatne linki | ||
| [StackBlitz - Przykład interaktywny](https://stackblitz.com/edit/rxjs-ewsunmdz?file=index.ts) | ||
|
|
||
| [Rxjs - Oficjalna dokumentacja](https://rxjs.dev/api/index/function/shareReplay) | ||
|
|
||
| [Github - Kod źródłowy](https://github.com/ReactiveX/rxjs/blob/b25db9f369b07f26cf2fc11714ec1990b78a4536/src/internal/operators/shareReplay.ts#L26-L37) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. IMO logo angulara jest tu chybione - jeśli miałoby to być logo to raczej rxjs |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
wyrzuciłbym angulara z tagów