Commit c81f494
LIBDRUM-1005. Refactored pipeline avoid possible memory link
Refactored the `this.bitstream$.pipe` pipeline based suggestions from
Perplexity AI that the existing code had:
1) A memory leak if the pipeline doesn't complete, in Perplexity's
explanation:
> When Angular destroys this component (e.g., the user navigates away),
> the outer .subscribe() is not automatically cancelled. If the
> observable chain hasn’t completed yet — for example, the `switchMap`
> is still waiting for `isAuthorized$` or `isLoggedIn$` to emit — the
> subscription lives on in memory, holding a reference to the destroyed
> component and all its injected services. This prevents the garbage
> collector from reclaiming that memory.
>
> In practice, the take(1) operator in the pipe mitigates this
> somewhat, because once a single value passes through, `take(1)`
> completes the stream and the subscription auto-unsubscribes. However,
> if the upstream observables (`isAuthorized$`, `isLoggedIn$`) never
> emit (due to an error or unexpected delay), the subscription hangs
> indefinitely. The `take(1)` only protects you in the happy path.
This seems like a reasonable refactoring, because it is possible that
the authorization/authentication calls to the backend could fail.
2) Nested subscriptions are a rxjs anti-pattern.
See https://medium.com/ngconf/why-you-shouldnt-nest-subscribes-eafbc3b00af2
When pushed, Perplexity conceded that the nested subscription is not
likely a problem (more of a "code smell") in this case, because the
calls to the TranslateService in the inner subscription complete
synchronously, and so shouldn't cause a problem.
Seemed like a reasonable idea, so implemented it.
Co-authored-by: Perplexity AI <noreply@perplexity.ai>
https://umd-dit.atlassian.net/browse/LIBDRUM-10051 parent 555fe32 commit c81f494
1 file changed
Lines changed: 22 additions & 13 deletions
Lines changed: 22 additions & 13 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
5 | 5 | | |
6 | 6 | | |
7 | 7 | | |
| 8 | + | |
| 9 | + | |
8 | 10 | | |
9 | 11 | | |
| 12 | + | |
10 | 13 | | |
11 | 14 | | |
12 | 15 | | |
| |||
18 | 21 | | |
19 | 22 | | |
20 | 23 | | |
| 24 | + | |
21 | 25 | | |
22 | 26 | | |
23 | 27 | | |
| |||
68 | 72 | | |
69 | 73 | | |
70 | 74 | | |
| 75 | + | |
| 76 | + | |
71 | 77 | | |
72 | 78 | | |
73 | 79 | | |
| |||
112 | 118 | | |
113 | 119 | | |
114 | 120 | | |
115 | | - | |
| 121 | + | |
116 | 122 | | |
117 | 123 | | |
118 | | - | |
119 | | - | |
120 | | - | |
121 | | - | |
122 | | - | |
123 | | - | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
124 | 132 | | |
125 | 133 | | |
126 | 134 | | |
| |||
130 | 138 | | |
131 | 139 | | |
132 | 140 | | |
133 | | - | |
| 141 | + | |
134 | 142 | | |
135 | 143 | | |
136 | 144 | | |
| |||
145 | 153 | | |
146 | 154 | | |
147 | 155 | | |
148 | | - | |
149 | | - | |
150 | | - | |
151 | | - | |
152 | | - | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
153 | 162 | | |
154 | 163 | | |
155 | 164 | | |
| |||
0 commit comments