Тёмный

This ONE function makes RxJS + Signals in Angular so much better 

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

My modern Angular course: angularstart.com/
I was recently introduced to the 'connect' function from the ngxtension library for Angular, and it instantly made the way I work with RxJS and Signals so much easier.
Get weekly content and tips exclusive to my newsletter: mobirony.ck.page/4a331b9076
Ngxtension: ngxtension.netlify.app/
Source code: github.com/joshuamorony/angul...
State management approach: • My NEW default for sta...
#angular #rxjs #signals
0:00 Introduction
0:57 The 'connect' utility
1:53 Further improvements
2:59 Complex example
3:16 More declarative?
4:15 Conclusion
- More tutorials: eliteionic.com
- Follow me on Twitter: / joshuamorony

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

 

11 июн 2024

Поделиться:

Ссылка:

Скачать:

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

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 45   
@JoshuaMorony
@JoshuaMorony 7 месяцев назад
My Angular course launches next week, join the newsletter if you don't want to miss the launch discount: mobirony.ck.page/4a331b9076 - I'll also be sending out some more info about the course in tomorrow's newsletter
@alexandertorres8854
@alexandertorres8854 7 месяцев назад
I don't usually comment on any of these videos, but damn!! This is awesome!! Thank you Josh!
@TaylorKingKing
@TaylorKingKing 7 месяцев назад
Same here. I dont comment on videos but that was amazing. We have some greenfield work upcoming and Ill be using this approach.
@karamuto1565
@karamuto1565 7 месяцев назад
Great video especially as I watched the the Angular Signals talk from ng-conf the other day. The women up there seemed to also desire a complete declarative way and was aware that there still things missing for it to be perfekt.
@basedovi
@basedovi 7 месяцев назад
Appreciate how you think about the decisions you're taking to make the code (the final part of the video). I think of those as well, every time.
@darrennienaber6488
@darrennienaber6488 7 месяцев назад
Keen for your course Josh 👌🏻
@Ilcohere
@Ilcohere 7 месяцев назад
I'm always saving your videos to watch later. Will I ever watch one ?😂😂
@JoshuaMorony
@JoshuaMorony 7 месяцев назад
You definitely want to watch this one, watch it now 😉
@LarsRyeJeppesen
@LarsRyeJeppesen 7 месяцев назад
You should
@Ilcohere
@Ilcohere 7 месяцев назад
@@JoshuaMorony man I want to watch all of them 🤣😂 you rock in angular space
@johncerpa3782
@johncerpa3782 7 месяцев назад
connect() is really nice
@Wielorybkek
@Wielorybkek 7 месяцев назад
Signals seem like taking a step back from declarative code to more imperative. I would use signals to handle code that I would normally write imperatively but not the other way around. RxJs subjects are great for simple state management and if we need more complex state we can jump to NgRx.
@kylerjohnson988
@kylerjohnson988 7 месяцев назад
Respectfully, I think you're only looking at one side of the equation. The reason Angular has adopted signals is not about storing state, but about how change detection works. Ergo, the real benefit to signals isn't their usage for state, but for the dramatic performance increase they offer in terms of change detection. RxJS is great for async operations which populate state, but Angular sorely needed a reactive primitive that could track dependencies automatically and only mark components for rerender that depend on the signal that changed rather than dirty checking the entire component tree every time a DOM event emits. That's why Enea and Chau are using RxJS for the aysc bits (AKA fetching data and merging streams), but converting them to signals for use in the component.
@Wielorybkek
@Wielorybkek 7 месяцев назад
@@kylerjohnson988 Good point
@SWAT11113
@SWAT11113 7 месяцев назад
Hi Josh, I may sound ignorant or like noob but why do I need to program all of the selectors and reducers ? Instead I can use ngrx-store and further ngrx-forms. In the video you showed that to make things more declarative you used 3th party library. I can use ngrx-store and ngrx-forms they are 3th party too and everything will be similarly declarative. btw. great videos. I watch every single one of the just to learn something new.
@JoshuaMorony
@JoshuaMorony 7 месяцев назад
Yes absolutely you can use NgRx Store if you want (the only thing I think is slightly more declarative in the approach from the video is that you don't need to imperatively trigger things like load actions) - my main motivation for this style is to keep things more bare bones/vanilla for teaching purposes, but also I like to avoid using a library if I can (i.e. it isn't providing a significant benefit). At the moment I feel like with this default/vanilla approach + the connect function I don't need much else, but that also might change depending on the circumstance
@just_O
@just_O 7 месяцев назад
Hi Josh! What about using toSignal instead? toSignal also transform an observable to a signal and also subscribe/unsubscribe automatically? By the way, I really love your work, you teach us a lot, so thanks for sharing your knowledge 😊
@ChauTran
@ChauTran 7 месяцев назад
`toSignal` does work the way you describe but it implies that your state is stored on an RxJS Stream. In a Signal-first world, we want to hold our state in a Signal (or some other signalState/signalStore abstractions). And `connect` allows us to bridge between the RxJS Event Sources (no state) to the Signals (state) in a more declarative way. You can connect a stream that emits the whole state, or in the case that the Signal state is an Object, you can also connect a stream to a specific slice of that object (like Josh shows in this video)
@JoshuaMorony
@JoshuaMorony 7 месяцев назад
​@@ChauTran "it implies that your state is stored on an RxJS stream" is such a succinct way to explain the difference
@recconectt
@recconectt 7 месяцев назад
I think Mike Person wrote about his rxjs-signals "auto-signal" pattern a long time ago :) The approach seems to be the same, right?
@JoshuaMorony
@JoshuaMorony 7 месяцев назад
This approach also made me think of this but I only really looked at auto signals when Mike was first talking about them, I should probably give that a proper look again - I get the impression that connect is similar but still different
@recconectt
@recconectt 7 месяцев назад
@@JoshuaMoronyIf you think its worth sharing your thoughts I would really like to share them in a video like this one. It is awesome content, thank you!
@x2TruNation
@x2TruNation 2 месяца назад
At around 3:12 why do you keep wrapping things in { }, like {messages} or {error}? Is there a purpose for it?
@inwerpsel
@inwerpsel 7 месяцев назад
Can anyone explain why it has to be inside a constructor inside a class? It doesn't exactly help reduce the boilerplatiness.
@JoshuaMorony
@JoshuaMorony 7 месяцев назад
If you are using takeUntilDestroyed (which connect does under the hood) it needs to be used within an injection context (which includes the constructor) otherwise you will manually have to supply it with a DestroyRef - this is how it knows what it needs to watch for it to be destroyed
@inwerpsel
@inwerpsel 7 месяцев назад
Thanks, haha I think I need to take an Angular course to fully understand that sentence, coming from React where I haven't used a class in almost 4 years and you can clean up just by returning a function :D It's interesting though to see similar things like signals trickling into Angular.
@alifnaiech6972
@alifnaiech6972 7 месяцев назад
Like before watch the video
@kingpinkent
@kingpinkent 7 месяцев назад
Cool! So the difference between this and the toSignal method is that you get a WriteableSignal instead of readonly? Correct? Is that desireable? Can you connect it to a specific property on a signal? That could probably be useful
@JoshuaMorony
@JoshuaMorony 7 месяцев назад
Essentially yes and it's desirable (at least for my purposes) for the same reason that it was desirable to have the RxJS -> subscribe -> Signal set up with manually updating the state rather than using toSignal - it allows for this "imperative shortcut" zone where all state can be accessed and updated however you like, rather than strictly having to have everything derived in RxJS streams and then just consumed with toSignal. Adds flexibility and reduces complexity, at the cost of making the code a bit less declarative (but I think it is worth the cost). And to your final question, yes - if the stream emits { someProperty: someValue } it will update the 'someProperty' property on the signal - you can just map it to whatever you like and it will patch just those properties in the signal (assuming your signal stores an object).
@kingpinkent
@kingpinkent 7 месяцев назад
@@JoshuaMorony I understand, that's cool! Mapping specific properties on a state signal I can see being very powerful and something I could use. Wherever possible though it's probably better to use a readonly signal
@ChauTran
@ChauTran 7 месяцев назад
@@kingpinkentDefinitely `readonly signal` is the way to go but currently there's no way to create a readonly signal WITHOUT a writeable signal at the moment. And we want to create a state signal so `connect` (and sometimes computedFrom) is a natural bridge between Signal and RxJS at the moment if you choose not to use ngrx or rx-angular (which I think you should :D)
@kingpinkent
@kingpinkent 7 месяцев назад
​@@ChauTran Are you sure? I'm pretty new to signals so I might be wrong but the toSignal method on an observable does return a readonly signal. Theres no writeable signal in that context other than what the observable passes in to it. I get the idea with the writeable state signal, it can however introduce unexpected behavior with the state being altered from other places, the common tradeoff essentially. I use ngxs at the moment, been looking at rx-angular though, a lot of nice things there, feels more well maintained as well, so a switch is probably likely :)
@ChauTran
@ChauTran 7 месяцев назад
toSignal implies you use rxjs as the source of truth for your state. That’s the main difference. Yes, both ngrx/signals and rx-angular come out with awesome APIs. Until we have everything Signals (signal inputs, signals viewchild, signal form etc…), things like connect/computedFrom are 100% more valuable than toSignal alone
@_Yaroslav
@_Yaroslav 5 месяцев назад
How about this? ``` state$ = of(INITIAL_STATE).pipe( mergeWith(nextMessages$, nextError$), scan((state, patch) => ({ ...state, ...patch })), ); // and further you can turn it into a signal state = toSignal(this.state$); ```
@c0mpuipf
@c0mpuipf 7 месяцев назад
i like watching these videos but other than food for thought or following the operator gymnastics, i'd never use this in production;
@tingol10
@tingol10 7 месяцев назад
Following your videos and signals in particular makes me kind of sad. I don't think signals offer that much and watching experienced angular developers trying to be creative and make them work with rxjs feels like a bad sign. Major changes like these in other frameworks simplify stuff in an OBVIOUS way, but here it feels like we're trying to shove the new thing into our workflow for dubious benefits. Hoping the ng team improves on this, because I'm not impressed at all. All we wanted was writing less code for the subject/observable/rxjs interop but now it just seems they added a layer of complexity instead.
@mikemarkovich69
@mikemarkovich69 7 месяцев назад
I feel like their purpose was adequately explained in his previous videos, as well as the videos of other prominent bleeding edge creators. Data and event streams can still be entirely rxjs based in services. Signals don't have to change that setup, and shouldn't. It does change the subject-as-a-service pattern if you're using that. Otherwise, adding signals to the template and component files allows us to communicate with those streams better imo, and makes a case for limiting the automatic decision to use NgRx and/or Component Store. You have to subscribe way less, use the async pipe way less, have to worry about whether or not a binding has a value way less, and so on. I'm not sure how the benefits could be clearer than that, but I'm open to be argued.
@tingol10
@tingol10 7 месяцев назад
@@mikemarkovich69 Subscribe less? I never subscribe at all (some rare exceptions). I guess not writing async in the template is somehow an improvement? I don't think so. I don't have to use external state management now but I do need external libs like explained here or I get almost the same amount of boilerplate as I would with subject based state management. Not saying they're totally useless, just expected more when it was announced...
Далее
Cypress Testing with React - Simple Tutorial
12:43
Просмотров 35 тыс.
С Анджилишей как в раю🥰
00:10
Просмотров 117 тыс.
More updates from the Angularv17
4:37
Просмотров 15 тыс.
ngTemplateOutlet is WAY more useful than I realised
16:36
How Angular Signals and RxJS Work Together
16:15
Просмотров 25 тыс.
The easier way to code Angular apps
9:54
Просмотров 64 тыс.
First look at Signals in Angular
18:43
Просмотров 43 тыс.
Why did we need Signal Inputs in Angular, again?
12:19
Просмотров 1,7 тыс.
Why I use a view model stream for my Angular templates
15:11
Input Signals in Angular 17.1 - How To Use & Test
14:34