Тёмный

Is complex RxJS still useful with Angular signals? 

Joshua Morony
Подписаться 73 тыс.
Просмотров 20 тыс.
50% 1

My modern Angular course: angularstart.com/
In this video, I revisit one of my more complex RxJS streams in an Angular application and refactor it to use the new state management approach I've been using with Angular signals.
State management explanation: • My NEW default for sta...
Source code: github.com/joshuamorony/angul...
Get weekly content and tips exclusive to my newsletter: mobirony.ck.page/4a331b9076
Learn Angular with Ionic: ionicstart.com
0:00 Introduction
1:38 Recap
2:30 Fetching data
3:22 Handling errors
4:37 Recursive fetching
5:30 Pagination
6:52 Thoughts
#angular #rxjs #signals
- More tutorials: eliteionic.com
- Follow me on Twitter: / joshuamorony

Опубликовано:

 

31 май 2024

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 62   
@JoshuaMorony
@JoshuaMorony 7 месяцев назад
Psst, my Angular course is launching soon - don't miss the launch discount: mobirony.ck.page/4a331b9076
@alissonts6574
@alissonts6574 7 месяцев назад
will the course cover how to test?
@JoshuaMorony
@JoshuaMorony 7 месяцев назад
@@alissonts6574 no, testing isn't covered in this course (the source code for the first two example apps do have tests if you want to check them out though: github.com/joshuamorony/angularstart-quicklists github.com/joshuamorony/angularstart-giflist
@sakesun
@sakesun 7 месяцев назад
The sample code, graphics, annotation, and editing. This channel is the finest coding channel produced by the author who really care.
@LarsRyeJeppesen
@LarsRyeJeppesen 7 месяцев назад
NGRX has the sweet spot for us - it allows to expose selectors as signals. Wonderful
@unhandledexception1948
@unhandledexception1948 7 месяцев назад
can't wait for the course
@philip-rodrigues
@philip-rodrigues 7 месяцев назад
Your channel inspires me, I'm really curious to know how you make these slides, it looks like it was done by hand, thanks for the video
@JoshuaMorony
@JoshuaMorony 7 месяцев назад
Thanks! And most of my diagrams are done in excalidraw (which gives it that hand drawn look) - some stuff (arrows and such) are also sometimes added when editing the video
@pedrofernandes2005
@pedrofernandes2005 7 месяцев назад
This is very nice. I was interested in seeing how you would integrate the pagination subject (when to reset,etc) but i forgot reddit api works differently
@holycrimpsauce
@holycrimpsauce 7 месяцев назад
I’ve just had a little brain lightbulb: signal() is to computed() as Subject is to Observable. If your application is declarative, computed signals and observables only react. Whereas (writable) signals and subjects can be changed more directly. To be more declarative, lean towards computed signals and observables whenever possible.
@e-jarod4110
@e-jarod4110 7 месяцев назад
It's somewhat true
@JoshuaMorony
@JoshuaMorony 7 месяцев назад
Yes this is generally a good way to think of it - signals (specifically WritableSignals) and BehaviorSubjects fill a similar role, and whenever you set a signal or next a subject you are performing an imperative action - a WritableSignal or a BehaviorSubject are inherently not declarative because they can be imperatively changed after their declaration. On the other hand, derived computeds/observables are inherently declarative because their value can only be derived never imperative set, and you can see that computation up front in the declaration for deriving that value.
@machiinate
@machiinate 6 месяцев назад
I still watch your videos even though I´m no longer writing Angular code all that often, since starting a new job been using react for the first time. I´m interested to keep following along, recently build an infinite scroll feature similar to this so far so good, React feels more like writing vanilla JS but im keen to stay in touch with the Angular ecosystem things are moving fast for as much pain RXJS can be to learn it´s very powerful.
@vredurs
@vredurs 7 месяцев назад
I have a question, when you setup a subscription in the constructor, and in the subscribe, update the signal, is the subscription cancelled or unsubscribed automatically then? Or have you just omitted that to minimize the code in the example? Thanks for all the great content
@vredurs
@vredurs 7 месяцев назад
I saw it now, small mobile screen so I missed the destroy method
@jakehockey10
@jakehockey10 7 месяцев назад
What're the pros and cons in regards to those subscriptions acting as reducers compared with using a tap operator in the original observable declarations?
@JoshuaMorony
@JoshuaMorony 7 месяцев назад
There aren't really any pros/cons - a subscribe, or a tap + subscribe can achieve effectively the same thing. But imo if what you are doing is pulling a value out of a stream then just a subscribe is the more direct/obvious way to do that.
@TayambaMwanza
@TayambaMwanza 7 месяцев назад
Signals for State, Rxjs for events ✅️
@LarsRyeJeppesen
@LarsRyeJeppesen 7 месяцев назад
Ngrx: signals for selectors, observables for effects
@LarsRyeJeppesen
@LarsRyeJeppesen 7 месяцев назад
Absolutely
@kingpinkent
@kingpinkent 7 месяцев назад
Can you not skip the imperative bridge by using the toSignal method from rxjs-interop and wrap the observable?
@JoshuaMorony
@JoshuaMorony 7 месяцев назад
This would be a more declarative approach - all the state would be derived from the source streams. But the "imperative bridge" does not solve the sole purpose of getting values from RxJS to Signals, it also serves as an area where all state can be accessed/set imperatively which simplifies things a great deal (e.g. some source emits and then I react to that by accessing whatever other state I need, rather than having to have that value incorporated into the stream in some way)
@kingpinkent
@kingpinkent 7 месяцев назад
@@JoshuaMorony Yeah, that makes sense. It probably still could be done declaratively though, but maybe for simplicity and readability this is nicer. Would be interesting to see this example in an all declarative way using signals.
@bric305
@bric305 7 месяцев назад
Great video as usual. I do wish though that you could explain why this solution (or part of it) using only signals would not work. The same way you explained how signals solved the diamond problem of rxjs in your other video "why angular team didn't go with rxjs?"
@JoshuaMorony
@JoshuaMorony 7 месяцев назад
The big thing that RxJS provides is the ability to deal with asynchronous reactivity, as in if some thing B reacts to A changing, but some time needs to pass before B can be set (e.g. because the value of A needs to be used in an HTTP request to set B). But aside from that, suppose we had to async stuff to worry about, signals are also just kind of awkward to represent events. Let's say you have some kind of source that is a signal instead of a subject, the signal will not notify anything unless it has actually changed, so you can only really sue it to represent "actions" if the data being set into the signal is different every time (which is especially awkward if there is no payload related to the event)
@kaischonberger97
@kaischonberger97 7 месяцев назад
Beautiful code, well done. Two questions: - Did you consider to use the materialize operator to materialize errors instead of adding and extra Observable for the error? - Did you consider using a BehaviorSubject to get rid of the startsWith operator for the pagination?
@JoshuaMorony
@JoshuaMorony 7 месяцев назад
I've barely used materialize so haven't given it much thought, maybe I should give it a proper try but I feel like it would be more awkward to handle (though it would have the benefit of keeping all of the imperative stuff just in the subscribe/reducer set rather than nexting the error stream imperatively from the catchError). As for BehaviorSubject, I feel like Subject fits the concept better as I am trying to represent events and want to trigger an initial event, rather than storing state for the sake of essentially simulating a first event.
@kaischonberger97
@kaischonberger97 7 месяцев назад
@@JoshuaMorony Thanks for answering. I agree and mostly do it the same way. Was just curious if you considered those alternate approaches.
@armynyus9123
@armynyus9123 5 месяцев назад
wow. Subbed. Totally.
@shivisuper91
@shivisuper91 7 месяцев назад
Is there a specific reason to use Subject sources and not BehaviourSubject sources? Because almost always you're using startWith operator to give it an initial value anyways...?
@JoshuaMorony
@JoshuaMorony 7 месяцев назад
The reason not to use BehaviorSubjects is because I am not using them to store state, that is the role of the signals - startWith in cases where I need it is more just a way to trigger an initial event/action automatically
7 месяцев назад
Why are you using signal.update() instead of signal.mutate()? Is there specific reason for that?
@JoshuaMorony
@JoshuaMorony 7 месяцев назад
signal.mutate() will cease to exist soon, but API wise I do prefer working with immutable data so I probably would do it anyway even if mutate was technically more efficient
@LarsRyeJeppesen
@LarsRyeJeppesen 7 месяцев назад
Signal.mutate() has been removed in V17-RC0
@TheXeropax
@TheXeropax 7 месяцев назад
Will Angular keep RxJS stufff in Angular 17? I'm confused a little.
@JoshuaMorony
@JoshuaMorony 7 месяцев назад
Most of the RxJS stuff will be unchanged in 17, but likely over time we will see observable based APIs in Angular being replaced with signal ones and RxJS not being so much of a dependency for Angular. It doesn't mean RxJS can't/won't be used in Angular, just that it probably won't be a required part of the experience.
@aravindmuthu5748
@aravindmuthu5748 7 месяцев назад
@@JoshuaMorony That's the best thing that could happen to Angular, for most beginner devs especially the ones with no Java or OOP experience, the unintuitiveness of Angular and the complexity of Rxjs has been the huge blocker to learn the framework. Now that a new state management system has been implemented in-house will get rid of all the unwanted "learning curve" that Angular has been infamous for
@TheXeropax
@TheXeropax 7 месяцев назад
@@JoshuaMorony Thank you for clarification :)
@supirman
@supirman 7 месяцев назад
I wish I fully understood the rxjs the way you do. My app uses rxjs crudely, but has a smattering of imperative stuff built in still as I don't fully know how to do all the rxjs awesomeness you show.
@JoshuaMorony
@JoshuaMorony 7 месяцев назад
It clicks more as time goes on (although I definitely don't "fully" understand RxJS) - it wasn't really until I became a bit obsessed with the benefits of declarative code that I really started understanding the more fundamental ideas, and trying to do everything in a declarative way helped pushed my understanding. I think if you appreciate the benefits of declarative code your RxJS knowledge will definitely get there eventually.
@ico0z
@ico0z 7 месяцев назад
This approach isn't it require a lot memory keeping the gifts in an observable and in a signal again? Isn't it the same data twice in memory?
@JoshuaMorony
@JoshuaMorony 7 месяцев назад
The data is only stored in the signal - the bottleneck here would likely be the DOM before anything else, so some kind of handling should be added at some point in case the user wants to scroll through 100 pages or something without switching subreddits. I think virtual scroll would be a bit tricky here due to the different sizes/dimensions of the videos, but a simple solution would probably be to just reset the data at some point (say after 500 have been loaded remove the first 250 or something like that)
@tolstoievski4926
@tolstoievski4926 7 месяцев назад
are you going to make a video about your transition to neovim and how you configured it
@JoshuaMorony
@JoshuaMorony 7 месяцев назад
I sort of have some videos like that - this one sort of covers the transition: ru-vid.com/video/%D0%B2%D0%B8%D0%B4%D0%B5%D0%BE-aCgDs8Nv-jo.html and this one covers the general workflow I use: ru-vid.com/video/%D0%B2%D0%B8%D0%B4%D0%B5%D0%BE-P5QPacz2-ao.html but I don't have a full from scratch style configuration video
@CptProv
@CptProv 6 месяцев назад
Would you use signals to keep the state of errors too? Also, is this approach in your course?
@JoshuaMorony
@JoshuaMorony 6 месяцев назад
Yes you can see at 4:23 an example of errors being set into the state signal - and yes, this course is based around these sorts of ideas
@Animesh878
@Animesh878 3 месяца назад
Is angular still relavent in 2024?
@jonathangamble
@jonathangamble 7 месяцев назад
The problem with all this is that in reality it would be first loaded on the server with SSR for seo (which would be promises), then hydrated and subscribed to changes...
@JoshuaMorony
@JoshuaMorony 7 месяцев назад
I don't have any actual stats handy but I'm pretty sure most Angular apps aren't using SSR - this particular app was also originally a mobile app (as in built natively with a web view) so SSR isn't an option there in any case. But yes, if you were doing all this on the server then our poor expand operator would miss out on the action.
@jonathangamble
@jonathangamble 7 месяцев назад
@@JoshuaMorony - Yeah, IMHO that is the problem with Angular in the last few years... it didn't prioritize the server, which is the direction of every other framework. But because of that, I sometimes wonder why use observables when your data doesn't change.. just use promises... but I do appreciate declarative rxjs
@JoshuaMorony
@JoshuaMorony 7 месяцев назад
@@jonathangamble it'll be interesting to see if the Angular landscape changes at all with the improvements coming for SSR - but yes I generally agree, observables are good when you want to represent changing data declaratively. If you are dealing with pagination on the client side and want to trigger an HTTP request, observables are good/necessary here. If your pagination is controlled via SSR and going to a /page/2 route or whatever, and you don't need to combine that with any other data changing on the client side, then an observable isn't as useful because there is no changing data that needs to be reacted to. Like I consider the SvelteKit PageData approach of const { myThing } = data; to be a declarative approach - the load function determines what the data will be and you have everything you need upfront/its not going to be imperatively modified later. Unless of course you do want to imperatively modify it after the load due to some other thing changing... in which case I might want observables ;)
@neilfarted
@neilfarted 6 месяцев назад
I'm not sure I understand the choices made with error handling, maybe I'm missing some context. You have an error property in the state signal that doesn't appear to be used, instead, you are handling errors with a stream (rxjs Subject). Why is this? Why have an error property in the state if you are not going to use it, and why is a stream of errors more helpful than updating the state with any errors that are caught?
@boris8983
@boris8983 2 месяца назад
so Rignals it is - let's make it a thing
@sulaimantriarjo8097
@sulaimantriarjo8097 7 месяцев назад
i think, it makes more complicated for beginner to learn angular. btw is the current signal implementation still using zonejs?
@LarsRyeJeppesen
@LarsRyeJeppesen 7 месяцев назад
yes, until we get signal based component inputs/outputs we are still stuck with zonejs. But expect those to drop as a point-release soon after v17
@sulaimantriarjo8097
@sulaimantriarjo8097 7 месяцев назад
@@LarsRyeJeppesen if so I wonder about angular material, ng zorro and so on components will work
@alandragicevich2421
@alandragicevich2421 7 месяцев назад
The one big shame about this RxJS/Signal interop all the extra closures. RxJS isn't that performant in the first place (although still fast enough in my opinion, supposedly just as fast as native iterators). Signals on the other hand are much more efficient I think, due to being built off of the proxy object system? So deep down, it hurts thinking about the extra unnecessary memory overhead. But at the end of the day, probably doesn't matter... Plus I can't stop using RxJS. I love declarative programming too damn much 😅 Edit: I still don't know why they couldn't just modify the async pipe to be smarter so it doesn't rely on ZoneJS.
@JoshuaMorony
@JoshuaMorony 7 месяцев назад
I tend to weight DX much more heavily than performance, because generally I think that better DX will ultimately lead to developers building better apps that might end up being more performant anyway. So for me, there has to be a pretty significant/obvious reduction in performance to justify using an approach with worse DX. Happy devs build good apps is my philosophy pretty much lol
@TheBlockUniverse
@TheBlockUniverse 7 месяцев назад
What's hard about storing the state in RXJS? Is it the fact that the state is asynchronous? I feel like the whole signals implementation could have been avoided if they integrated more tightly with observables. Coming from react to angular, I've noticed that not having to care about updates/re-rendering in angular has only lead to a bunch of horrible code, where it's impossible to understand the flow of data, but maybe that's just bad developers I've encountered ¯\_(ツ)_/¯
@JoshuaMorony
@JoshuaMorony 7 месяцев назад
Sorry to just link to a video, but I do have my thoughts on this specifically in my "Why didn't the Angular team just use RxJS?" video: ru-vid.com/video/%D0%B2%D0%B8%D0%B4%D0%B5%D0%BE-iA6iyoantuo.html
Далее
ЭТО ВООБЩЕ НЕ БОЛЬНО !
00:15
Просмотров 237 тыс.
g-squad assembles (skibidi toilet 74)
00:46
Просмотров 10 млн
Попили кофе 😁
00:11
Просмотров 12 тыс.
Common Mistakes and Advanced Typescript Techniques
10:49
Angular Signals vs RxJS - Reuse It Effectively
9:02
Просмотров 4,7 тыс.
Why are people SO obsessed with useSignal()?
3:41
Просмотров 66 тыс.
Everything you need to know about Angular signals
6:25
Learn Angular Signals - The Future of State Management
10:02
Angular Signal Output - It’s Getting Even Better
6:09