Skip to content

Conversation

@sirtekyt
Copy link
Contributor

@sirtekyt sirtekyt commented Oct 3, 2025

…he'ować dane z observable?

- observable
---

Często zdarza się, że nie chcemy za każdym razem ponownie wykonywać całej logiki z danego *Observable* —
Copy link
Contributor

Choose a reason for hiding this comment

The 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

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
Copy link
Contributor

Choose a reason for hiding this comment

The 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)

---

Często zdarza się, że nie chcemy za każdym razem ponownie wykonywać całej logiki z danego *Observable* —
zamiast tego wolimy przechować jego wynik w pamięci. W Angularze (i ogólnie w RxJS) możemy to zrobić za pomocą operatora **`shareReplay`**.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

przy shareReplay zostawiłbym same apostrofy (nie przekombinowujmy z formatowaniem)


## 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ść.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tą samą wartość

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tę wartość

s2: 742
```
## Opcje konfiguracji
Operator `shareReplay` posiada także dodatkowe opcje do skonfigurowania.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dwukropek zamiast kropki



### Przykład z dodatkowymi opcjami konfiguracji
Załóżmy, że chcemy cache’ować więcej niż jedną wartość i dodatkowo kontrolować czas ich przechowywania.
Copy link
Contributor

Choose a reason for hiding this comment

The 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 ;)

Choose a reason for hiding this comment

The 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,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Brakuje mi jakiejś części zdania przed tym wypunktowaniem

Copy link
Contributor

Choose a reason for hiding this comment

The 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

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
Copy link
Contributor

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

Operator `shareReplay` posiada także dodatkowe opcje do skonfigurowania.
- `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).

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ogranicza czas przechowywania wartości (czego domyślnie nie robi)


## 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ść.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tę wartość



### Przykład z dodatkowymi opcjami konfiguracji
Załóżmy, że chcemy cache’ować więcej niż jedną wartość i dodatkowo kontrolować czas ich przechowywania.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tutaj bez przecinka :D

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants