Angular unsubscribe, Angular async pipe, RxJS subscribe - Avoid Memory Leaks

แชร์
ฝัง
  • เผยแพร่เมื่อ 19 ก.ย. 2024

ความคิดเห็น • 92

  • @MonsterlessonsAcademy
    @MonsterlessonsAcademy  17 วันที่ผ่านมา

    WATCH NEXT: Angular Interview Questions and Answers - Dominate Your Next Interview - th-cam.com/video/5A_YKlVWMPo/w-d-xo.htmlsi=2DCn7yspEAAJ2H6l

  • @luczztem
    @luczztem ปีที่แล้ว +6

    broo the extended class approach is genius

    • @MonsterlessonsAcademy
      @MonsterlessonsAcademy  ปีที่แล้ว

      Thank you!

    • @programmistka_v_amerike
      @programmistka_v_amerike 2 หลายเดือนก่อน

      Until for some reason we need to extend another class and TS doesn't support multiple inheritance.
      All the other solutions are indeed most used to avoid memory leakage. Also, multiple subscriptions can be also pushed to an array and looped through to unsubscribe from in the ngOnDestoy hook.

  • @chinmayapandya8990
    @chinmayapandya8990 ปีที่แล้ว +2

    superb explanation of all possible ways of avoiding memory leaks for RxJS subscriptions. Thank you so much ! 🙂

  • @riketscience
    @riketscience ปีที่แล้ว +2

    The last part at 09:12 is very nice indeed. I really like this kind of work. A great solution for many applications. Once again, great content my friend. 👍

  • @izacaqsha3480
    @izacaqsha3480 ปีที่แล้ว +1

    You are awesome sir,
    keep making videos like this !!!
    You are teaching a lot of newbies and experienced programmers!!

  • @bizmarinainfo1808
    @bizmarinainfo1808 ปีที่แล้ว +1

    Excellent short and consolidated walkthrough. Thanks for posting.

  • @rizaanjappie
    @rizaanjappie 2 ปีที่แล้ว +1

    This channel is underrated.

  • @hamza201183
    @hamza201183 2 ปีที่แล้ว +2

    High quality information as always. Many thanks Oleksandr!

  • @kirole7381
    @kirole7381 ปีที่แล้ว

    Best and straightforward...but i just see in my navbar one of your videos..
    that async pipe is broken in Angular

    • @MonsterlessonsAcademy
      @MonsterlessonsAcademy  ปีที่แล้ว

      Yes, you are right
      th-cam.com/video/Z9U9-VRN_XU/w-d-xo.htmlsi=WUum9t-zTDwQ078W

  • @valikonen
    @valikonen 2 ปีที่แล้ว +3

    Amazing post. Maybe you'll make more with RxJS and also with complex Directives. Thank you!

  • @krzaqabc
    @krzaqabc 2 ปีที่แล้ว +4

    You provide very valuable content, thank you Olexander
    PS Olexander, consider creating new angular course but even more advanced than medium one - by saying more advanced i mean using bigger spectrum of RxJs operators, change detection strategy, NG rx, using facade pattern, and such tricks like this one with extending abstract class, strict mode, use of firebase? This is just my idea, but knowing your skills as a teacher and experienced developer it would be Very valuable and helpful for developers on any 'level', best regards and stay healthy!

    • @MonsterlessonsAcademy
      @MonsterlessonsAcademy  2 ปีที่แล้ว +2

      Thank you for the idea. I will add it to the list of future courses!

  • @lianayukhymchak5328
    @lianayukhymchak5328 ปีที่แล้ว

    Дякую! Дуже круте відео, все чітко і зрозуміло!) Thanks a lot!)

  • @transforgoku
    @transforgoku ปีที่แล้ว

    Excellent video, I think I'm gonna stick to async pipe and avoid suscriptions, Oninit and Ondestroy altogether in my code...

  • @mrlindaring
    @mrlindaring 3 หลายเดือนก่อน

    Brilliant! Thank you.

  • @its_vincesanity
    @its_vincesanity 2 ปีที่แล้ว

    This is pretty awesome! Love that's it's so easy to implement but so valuable. Thanks!

  • @msiyam
    @msiyam ปีที่แล้ว

    Many Many Thanks to you sir, You make my day

  • @leiayuri
    @leiayuri 8 หลายเดือนก่อน

    Hi, i find this approach very interesting also. DO you might wanna do a short video about that option too.
    takeUntilDestroyed with destroyRef which close "Automatically" the sub.

    • @MonsterlessonsAcademy
      @MonsterlessonsAcademy  8 หลายเดือนก่อน

      Hi, I already did that
      th-cam.com/video/ddyxKtfOMqU/w-d-xo.htmlsi=iAcYmh8rOUI-w6ZX

  • @mirodz21
    @mirodz21 ปีที่แล้ว

    amazing explanation. super helpful!

  • @BennyS6502
    @BennyS6502 6 หลายเดือนก่อน

    Nice video 👍
    I would just like to mention that since version 16, Angular has introduced the "takeUntilDestroyed()" operator, which solves this problem.
    One question, why did you decorate the parent class "Unsub" with "@Injectable()"? In your example, it is not used as a service provided via dependency injection.
    Thanks!

    • @MonsterlessonsAcademy
      @MonsterlessonsAcademy  6 หลายเดือนก่อน +1

      I covered takeUntilDestroyed in a separate video
      th-cam.com/video/ddyxKtfOMqU/w-d-xo.htmlsi=i1RHZVQud8YKES4A
      Yes Unsub doesn't need injectable. It's just a habbit.

  • @tarasshevchuk8477
    @tarasshevchuk8477 ปีที่แล้ว

    Thank you ! Very useful information !

  • @ndthinh91
    @ndthinh91 ปีที่แล้ว

    Thank for your awesome content

  • @danilo_teixeira
    @danilo_teixeira ปีที่แล้ว

    Very nice video man, congrats! Could you make a video talking about the new operator released in Angular 16, the "takeUntilDestroyed()"

  • @ali.shahnawaz
    @ali.shahnawaz ปีที่แล้ว

    Awesome content on unsubscribe

  • @ugochukwuumerie6378
    @ugochukwuumerie6378 2 ปีที่แล้ว

    Legend! Thanks for the awesome content especially the custom takeUntil(), pretty useful

  • @VasyaFF
    @VasyaFF ปีที่แล้ว

    Is it correct that we should use ReplaySubject, NOT Subject for unsubscription?
    After component destroyed it is possible that Subject is destroyed too, but subscription NOT. And subscription survive after destroy. But ReplaySubject can "shot" one more time after component destroy and subscription will be completed.

    • @MonsterlessonsAcademy
      @MonsterlessonsAcademy  ปีที่แล้ว

      You can but there is no memory leak is subscribes if you use takeUntil or unsubscribe. Doesn't matter if it is subject, behaivourSubject or replaySubject.

  • @fuji91
    @fuji91 ปีที่แล้ว

    Sorry for the dumb question, but what if I have:
    data$ = interval(1000);
    const x$ = this.data$.pipe(take(1));
    x$.subscribe((data) => console.log('data', data));
    Which of the observables does the take(1) operator specifically complete?

    • @MonsterlessonsAcademy
      @MonsterlessonsAcademy  ปีที่แล้ว

      It's not dumb. x$ is a new observable based on data$. As you subscribe to x$ it has a take function

  • @dhmilmile1
    @dhmilmile1 หลายเดือนก่อน

    What should we do if we need ngOnDestory in the child component or how we can override it?

  • @imriharlev5151
    @imriharlev5151 2 ปีที่แล้ว

    Thank you for the good content 🤩

  • @roman-tp6sd
    @roman-tp6sd 2 ปีที่แล้ว

    You re the best! Tnxx!

  • @rohit36910
    @rohit36910 7 หลายเดือนก่อน

    Premium content, useful information. Thank you

  • @sergiocebrian85
    @sergiocebrian85 ปีที่แล้ว

    I still remember several projects where the people forgot to close subscriptions...

  • @rohit36910
    @rohit36910 7 หลายเดือนก่อน

    Should i unsubscribe route change as well

    • @MonsterlessonsAcademy
      @MonsterlessonsAcademy  7 หลายเดือนก่อน

      Angular does it for us but I prefer to just unsub for every subscribe I see

  • @austinZen8800
    @austinZen8800 2 ปีที่แล้ว

    you'll need a super() call in the component's constructor using / extending this class, otherwise TS will complain?

    • @MonsterlessonsAcademy
      @MonsterlessonsAcademy  2 ปีที่แล้ว

      You are totally right. If you need to do anything in contructor you must call super.

  • @space-waves
    @space-waves 3 หลายเดือนก่อน

    Wait what? I was sure the component is destroy when not in the view anymore and by consequence all subscribe are unsubscribed…

    • @MonsterlessonsAcademy
      @MonsterlessonsAcademy  3 หลายเดือนก่อน

      Destroying doesn't have anything to do with the in view or not. Also you need to unsubscribe manually.

  • @chagamajaykumarreddy1897
    @chagamajaykumarreddy1897 2 ปีที่แล้ว

    amazing stuff, we need to call super right if we use this class as a common for all components ?

  • @serhiitachuk
    @serhiitachuk 2 ปีที่แล้ว

    Hello! I love your video so much. Where is the best place to support you? On your platform or udemy (as far as I know udemy takes some commission)?

    • @MonsterlessonsAcademy
      @MonsterlessonsAcademy  2 ปีที่แล้ว +1

      Comission is everywhere -_-. Buying my courses or subscription on the website has the smallest comission and gives you knowledge in return.
      monsterlessons-academy.com/courses
      If you don't need a course but just want to support me there is a membership on youtube channel.

  • @austinZen8800
    @austinZen8800 2 ปีที่แล้ว

    Thanks!

  • @LailahaillahChannel
    @LailahaillahChannel 2 ปีที่แล้ว

    how would you call abstract unsub class while your component already extends other class? in typescript you cant extend two class. do you have any thoughts about that?

    • @MonsterlessonsAcademy
      @MonsterlessonsAcademy  2 ปีที่แล้ว +1

      You can't do that. Extend approach is not that popular in Angular at all. Typically you just inject services and use them.

  • @ArthunMorgan747
    @ArthunMorgan747 10 หลายเดือนก่อน

    Hi, can you tell us which IDE you are using? :)

    • @MonsterlessonsAcademy
      @MonsterlessonsAcademy  10 หลายเดือนก่อน

      It's Vim

    • @ArthunMorgan747
      @ArthunMorgan747 10 หลายเดือนก่อน

      @@MonsterlessonsAcademy Okie I am little confused as I am learning angular,should I use vscode or vim? I know it is just IDE but still which has better support?

    • @MonsterlessonsAcademy
      @MonsterlessonsAcademy  10 หลายเดือนก่อน

      @@ArthunMorgan747 You should use vscode and focus on learning programming and not configuring the editor which with vim will take a lot of time.

    • @ArthunMorgan747
      @ArthunMorgan747 10 หลายเดือนก่อน

      @@MonsterlessonsAcademy thank you 😁

  • @manit77
    @manit77 ปีที่แล้ว

    why don't we just use a promise for queries? this sounds like a potential problem in your app to troubleshoot.

    • @MonsterlessonsAcademy
      @MonsterlessonsAcademy  ปีที่แล้ว

      It's architecture of Angular team. I didn't have any problems or need to use promises in Angular.

  • @letok2871
    @letok2871 2 ปีที่แล้ว

    Крутотень)

  • @lindermannla
    @lindermannla 2 ปีที่แล้ว

    Monster! 😂

  • @iukys3889
    @iukys3889 8 หลายเดือนก่อน

    I don’t feel like the video is useful at all, this can be found in angular doc and this is far from real life situation (interval really ? ..)

    • @MonsterlessonsAcademy
      @MonsterlessonsAcademy  8 หลายเดือนก่อน

      It's the example to see that stream was canceled.

  • @mkrzyzowski
    @mkrzyzowski ปีที่แล้ว

    Last one approach is nice but it does not allow to use "ngOnDestroy" in the "Postscomponent" as it is already declared in "Unsub".

    • @mkrzyzowski
      @mkrzyzowski ปีที่แล้ว

      but we can add "public unsubscribe(): void {" in "Unsub" and call it from "public override ngOnDestroy() {", "this.unsubscribe();" to solve it.

    • @MonsterlessonsAcademy
      @MonsterlessonsAcademy  ปีที่แล้ว

      exactly