The end of Angular's "service with a subject" approach?

  Рет қаралды 25,267

Joshua Morony

Joshua Morony

Күн бұрын

My modern Angular course: angularstart.com/
A "service with a subject" is one of Angular's most popular state management approaches, but what is this going to look like when signals become available?
Signals vs subject: • Why didn't the Angular...
What is "declarative": • The easier way to code...
Get weekly content and tips exclusive to my newsletter: mobirony.ck.page/4a331b9076
Learn Angular with Ionic: ionicstart.com
0:00 Introduction
0:20 Service with a subject
0:53 Service with a signal
1:58 Async with subjects
3:53 Async with signals
4:37 Combining
#angular #signals #rxjs
- More tutorials: eliteionic.com
- Follow me on Twitter: / joshuamorony

Пікірлер: 84
@JoshuaMorony
@JoshuaMorony Жыл бұрын
Join my mailing list for more exclusive content and access to the archive of my private tips of the week: mobirony.ck.page/4a331b9076
@TayambaMwanza
@TayambaMwanza Жыл бұрын
I've noticed RXJS is a religion for some people
@DannyMcPfister
@DannyMcPfister Жыл бұрын
RxJesus gave his life for us.
@justsomeguy8385
@justsomeguy8385 Жыл бұрын
I find it's just so good once you learn and understand it, that it hurts to go back. Boiling data streams down to contained pipes of operators frees up mental bandwidth, and Angular allowing us to subscribe in the template and not need to worry about unsubscribing removes minimizes one of the major pain-points of RxJS. It's not perfect, but it's a nice mental model imo.
@santiagogomez8070
@santiagogomez8070 Жыл бұрын
Amazed by your knowledge and grateful for your sharing! Greetings from Argentina, and keep up the fantastic work! Eager to get that Pro license soon!
@draylegend
@draylegend Жыл бұрын
I think, signals can be used in templates to trigger change detection in a zone-less app and observables as communication layer between api and the app's state represented as signals
@martinemanuel8239
@martinemanuel8239 10 ай бұрын
100% agree
@clorek
@clorek Жыл бұрын
Quality content as always, thanks Joshua 👌
@grzegorzsokalski7328
@grzegorzsokalski7328 Жыл бұрын
Very good content, mate! Do NOT change the way u showing it to us, plss. Thanks to u I start to understand a few things that I didn't before. keep going that way ;)
@i.j.5513
@i.j.5513 Жыл бұрын
Great example! Even though the signals approach here is more "imperative" it still seems easier to understand and deconstruct than the RxJs version - at least to me, while the declarative version is a lot less obvious if you are not already quite familiar with the way RxJs works. A code should fit its purpose, be understandable and easy to maintain - whether ot not it's imperative or declarative is a secondary concern.
@JoshuaMorony
@JoshuaMorony Жыл бұрын
I see this concern "A code should fit its purpose, be understandable and easy to maintain" as related to a choice between imperative and declarative code, not as something secondary to it. Imperative style code is fine, but I would argue declarative code is generally more understandable and easier to maintain BUT that comes with the pre-requisite of learning/understanding and following the declarative mental model. Most (all?) devs start on an imperative model, so it's by default the more "obvious" approach, but imo that's only because of familiarity. Getting everyone on board with a declarative approach is certainly no small investment, but I think does lead to more stable/understandable/maintainable code.
@nerminkarapandzic5176
@nerminkarapandzic5176 Жыл бұрын
Exactly, can't agree more. Let's scrap this idea that declarative is always best.
@i.j.5513
@i.j.5513 Жыл бұрын
@@JoshuaMorony That observation of a historical progression of a developer from imperative to declarative programming is spot on! Also, can't wait for more videos about upcoming changes in Angular :) 1. With respect to declarative / imperative it might also depends on what level the programming process is coming on. You might need to implement some specific logic from scratch and there you use imperative code --- but you abstract it in a separate self-contained function that you then use elsewhere declaratively. 2. Sometimes there are also operators that seem declarative but can be quite limited -- a forEach method for JS arrays being an example. Oftentimes a simple for -of loop is better, since you can break the iteration, while forEach will always run through. 3. In the end, it's fine to prefer a certain coding style - it's just important that preference does not become too ossified. I prefer the declarative style - but if there is a rare situation where an imperative solution is obviously better, I won't force the declarative one for the sake of preference :)
@TimurMishagin
@TimurMishagin Жыл бұрын
Sadly, but I usually see more errors are made in imperative style than in declarative one. This means that maybe RxJs is harder to understand but it eventually it gives us less error prone solutions. But I like the signals anyway because they stay in the middle between plain variables and observables. They make Angular more flexible to use
@TimurMishagin
@TimurMishagin Жыл бұрын
@@JoshuaMorony Totally agree!
@Tjommel
@Tjommel Жыл бұрын
i think i stick with rxjs for a bit longer bevore switching or "adding" signals to my code. Seems like there is a lot of "dust" in the air and very much that might be changing. I think if someone is unsure what to do and he/she knows rxjs, it'S a good idea to keep on rxjs for a little longer
@JoshuaMorony
@JoshuaMorony Жыл бұрын
Yes this is good advice - or generally just sticking with whatever it is you are currently doing and waiting probably like a year. It's all backwards compatible so existing things will still work anyway, and then after everyone is done fighting about everything you can just adopt whatever best patterns emerge (...or just stick with what you're doing in the end!)
@retrogamessocietybrasil3372
@retrogamessocietybrasil3372 Жыл бұрын
That was great. Can you compare signals with component store?
@marcwinner567
@marcwinner567 Жыл бұрын
Can you please link to the source code shown at 3:01. Preferrably in a project where it is widely used for different entities. Because that base setup with scan and modification streams should be applicable to all entities right?: )
@daveyjones501
@daveyjones501 Жыл бұрын
You mention that with a declarative approach we only need to look in one place. But that one place is still 20 lines of code which, by your own admission, contains some non-obvious concepts. I find the argument of in one place is equally achieved by the signals as a service, ok that one place is also some 20 lines long and spans several methods, but at least it is understandable.
@nemanjazivaljevic1656
@nemanjazivaljevic1656 Жыл бұрын
In my experience, the main problem with RxJs is memory leaks. Even if you unsubscribe from your observers there are observers from components from UI libs like "PrimeNG" that cause memory leaks.
@infodusha
@infodusha Жыл бұрын
I do like the shown approach, but the thing I dislike about it is racing when doing requests. E.g. when we save data it can be saved in different order and we will end up having wrong data in database
@chaos_monster
@chaos_monster Жыл бұрын
4:09 Disclaimer: I did not yet play extensively wit Signals, but from my understanding the effect function wouldn't be called, or would it?
@JoshuaMorony
@JoshuaMorony Жыл бұрын
Effects run once initially (which you can trigger either by having them in the constructor or by initialising a class member) and then again any time any of the signals they depend on change
@chaos_monster
@chaos_monster Жыл бұрын
@@JoshuaMorony does your init effect make sense at all? As it doesn't depend on a signal at all? shouldn't it be in a constructor?
@JoshuaMorony
@JoshuaMorony Жыл бұрын
@@chaos_monster it could be, I'm playing with ideas here - putting things in the constructor might be where we end up going, but it's also generally a good idea not to do "work" in the constructor so maybe an init effect like this is a good idea for running code on init - not sure yet!
@michaelrentmeister7693
@michaelrentmeister7693 Жыл бұрын
Not the point of the video, but TIL that private fields could be created with a #... Thanks for the video!
@returncode0000
@returncode0000 Жыл бұрын
2:55 I don't understand the # sign in front of the #modifyEmployees$ variable? Is this this a template variable or Is it private field of a typescript class?
@theanswer1993
@theanswer1993 Жыл бұрын
It's a private variable. I am not really a big fan of it. I much rather use public and private prefix
@JoshuaMorony
@JoshuaMorony Жыл бұрын
One thing I like about this approach vs public/private is that I often want to do something like .asObservable() for the public one, but that means it needs to come after the private one. But the default ESLint rules don't like you declaring private members before public members, so this is a nice way around that and it's also technically a stronger enforcer of private-ness as it enforces it at runtime not just compile time (not really a big deal for me though)
@returncode0000
@returncode0000 Жыл бұрын
@@JoshuaMorony Thanks Josh for your detailed explanation, I now understand your intention. Thanks for making such great videos, they all help 🙏👍🏻
@CodingAbroad
@CodingAbroad Жыл бұрын
Does using signals decrease the need for ngrx?
@JoshuaMorony
@JoshuaMorony Жыл бұрын
Not really imo, I think NgRx is solving a different problem - we will probably see a signal based NgRx (not sure if that will be the default or if there will be signal and RxJS based versions). I know some toying around has already been done with this.
@nathanalberg
@nathanalberg Жыл бұрын
also... doesnt your rxjs also immediately save on first load?
@JoshuaMorony
@JoshuaMorony Жыл бұрын
It does, the redundant save could be avoided if you wanted to
@radvilardian740
@radvilardian740 Жыл бұрын
I thought it's not a good practice to always spreading like that, since already use rxjs or signal, which by means it will trigger the cdr.
@deatho0ne587
@deatho0ne587 Жыл бұрын
I want try to learn from the videos like this by you, but it is still confussing from my 30 years of impertive coding brain. FYI: I am not complaining below just stating. Code 3:55 vs 4:40 -I can understand the code at 3:55, without you saying anything -I would not understand the code at 4:40 if you were not talking about Meaning a tech lead who maybe has not actually coded in 5 years or does not work in JS/TS/Angular is going to say the same thing. An intern/newer person is going to look at this and also not know how to update it if ever needed in the future. The above is one of my major issues with none imperative code. Please keep doing what you are doing for it.
@JoshuaMorony
@JoshuaMorony Жыл бұрын
Yes I agree it's a totally different mental model that would need to be learned, and it's not easy
@practicalgnome7372
@practicalgnome7372 Жыл бұрын
RxJS and declarative approach requires some time and effort to switch thinking in that way. But man, when you get it there's no way back. It's highly addictive, powerful and fun. Everything is a stream 👾
@TimurMishagin
@TimurMishagin Жыл бұрын
Totally agree. But I like signals too because they offer sth like "middle" path
@coder5877
@coder5877 Жыл бұрын
The public computed property and the public Observable aren’t schematically the same. The computed property (employees in this case) has the downside of consumers not knowing when to read/re-read it after it’s updated. The observable let external consumer observe and react to changes over time. Does signals have the capability to expose a read only version from your class? By read only mean a ref that will not allow writing but allow writing new computed/effects with it.
@JoshuaMorony
@JoshuaMorony Жыл бұрын
I might be missing the point of your comment, but the computed does more or less behave the same as the public observable (with one technical difference being that a signal will only notify things that depend on it when the value *changes*, with an observable you could also get emission of a new value even if that value was the same as the previous value). If some other computed signal uses the public computed signal from the service or if its used in an effect, or a template then it will react to the signal in the service changing.
@oysthau
@oysthau Жыл бұрын
2:49 Will .save() inside tap work? I don’t think tap subscribes.
@JoshuaMorony
@JoshuaMorony Жыл бұрын
Yes, as long as the employees$ stream is subscribed to from somewhere (which it will be because that data will be consumed by the app to display a list)
@oysthau
@oysthau Жыл бұрын
@@JoshuaMorony I think you need it to be like this tap((employees) => this.myApiService.save(employee).subscribe()). I don't think only subscribing to employee$ will be enough
@madeOfClay99
@madeOfClay99 Жыл бұрын
Why are you naming your signal or subject variable with #? I mean, why though... lol Where did you get that naming convention?
@JoshuaMorony
@JoshuaMorony Жыл бұрын
It's the private field syntax, functionally it makes the field private but also has the side benefit of what I think is a nice naming convention for a public/private version of the same thing
@nyashachiroro2531
@nyashachiroro2531 Жыл бұрын
It's actually official JavaScript syntax for private fields in classes
@madeOfClay99
@madeOfClay99 Жыл бұрын
​@@nyashachiroro2531 I learn something every day. Thank you Nyasha. Since I'm more familiar with Typescript, when something is private. I just simply use the private access modifier keyword.
@nyashachiroro2531
@nyashachiroro2531 Жыл бұрын
@@madeOfClay99 You're welcome
@nathanalberg
@nathanalberg Жыл бұрын
how is `await lastValueFrom(...)` not a potential memory leak?
@JoshuaMorony
@JoshuaMorony Жыл бұрын
I would say it is something you need to be careful of - this is not a pattern I use personally, but generally it is used in conjunction with HTTP requests to get the value without having to subscribe, so ideally there is just an observable that emits one value (but even with just a HTTP request that emits one value this could still cause problems).
@lucasgiunta8874
@lucasgiunta8874 Жыл бұрын
what is the # syntaxe ? It is a new convention ? Or it is just for the exemple ?
@yurilins25
@yurilins25 Жыл бұрын
putting the # as a prefix makes the variable private
@JoshuaMorony
@JoshuaMorony Жыл бұрын
Yes as Yuri pointed out its the private field syntax, but mostly the reason I do this is because it allows me use the same name for a public/private version of the same thing
@zoltanboros8963
@zoltanboros8963 Жыл бұрын
Okay, but signal is not available at the moment.
@nerminkarapandzic5176
@nerminkarapandzic5176 Жыл бұрын
I don't care about declarative vs imperative, if the declarative code will take me 30 minutes to realize what it's doing vs 5 seconds of looking at imperative code then I choose imperative every time. Signals win.
@cocoscacao6102
@cocoscacao6102 Жыл бұрын
Not to mention debugging when you have more complex stuff. But hype is hype I guess...
@EtoBARAKUDAvasa
@EtoBARAKUDAvasa Жыл бұрын
NGRX the BEST State managment flow
@JoshuaMorony
@JoshuaMorony Жыл бұрын
I'm team NgRx Component Store
@billy8461
@billy8461 Жыл бұрын
Agreed!
@magmarrex5939
@magmarrex5939 Жыл бұрын
​@@JoshuaMorony Doesn't NgRx have too big bolderplate ?
@TimurMishagin
@TimurMishagin Жыл бұрын
@@magmarrex5939 If you use global store for everything - perhaps
@tolstoievski4926
@tolstoievski4926 Жыл бұрын
I prefer thé declarative approach with rxjs
@szymonadamczuk5367
@szymonadamczuk5367 Жыл бұрын
I didn't understand this rxjs code at all...
@adambickford8720
@adambickford8720 Жыл бұрын
Feels like churn w/little gain to me. Easy enough to understand so if it makes the js kids feel less insecure about not using react, great.
@michaelcorleone1832
@michaelcorleone1832 Жыл бұрын
It's so clear how overkill this RXJS in Angular specially in sending HTTP request. It's good to use RXJS in keyboard events, but totally overkill in using it in HTTP Client.
@JoshuaMorony
@JoshuaMorony Жыл бұрын
I disagree on it being overkill - RxJS is what enables declarative code in Angular when dealing with async situations. Certainly you can write imperative code if that is what you prefer, and many (most?) people do, but I'm yet to see a declarative (or mostly declarative at least) alternative without RxJS.
@jgcommander
@jgcommander Жыл бұрын
Isn't if you see client as set of possibilities (that includes different states) that are attached to request - most probably you never utilised all of them intentionally (like most of us) but they are here and sometimes needed. One of this possibly in use is using switchmap attached to some event that cancels another request.
@cosmokenney
@cosmokenney Жыл бұрын
I'll take that signals version any day. The declarative rxis mess is best described as puke.
@oupayalghzoul9842
@oupayalghzoul9842 Жыл бұрын
🙏👋 I need a rich editor for angular with adding image feature, any help?
@cocoscacao6102
@cocoscacao6102 Жыл бұрын
Uhm, regarding that beautiful declarative approach... You're calling an api to save a list of users, instead of just newly added one ...
@JoshuaMorony
@JoshuaMorony Жыл бұрын
It's just an off the cuff example and I had saving to local storage in mind, but if you wanted to optimise you could just move the tap operator up so that it sends the add/remove action to the server instead of sending the list of data
@Ernestas23
@Ernestas23 Жыл бұрын
Not gonna lie, RxJs example looks an over-engineered solution.
@g-luu
@g-luu Жыл бұрын
You just made Signals look like a downgrade to be honest with that imperative code. I wish Angular team just worked on reactivity on top of rxjs instead.
@luczztem
@luczztem Жыл бұрын
Service with a subject FTW
@mileselam641
@mileselam641 Жыл бұрын
Angular: Use rxjs to create an observable, call the load method, pipe it to... Angular with signals: Make a signal, use it with computed, load from the service in the init... Svelte: Just use a store. Angular: But what about observability? Svelte: Just use a store. Angular: But what about reactivity? Svelte: Just use a store. Angular: It can't be that simple and easy. Svelte: It is. You're doing it the hard way.
@MyPhone-th7od
@MyPhone-th7od 10 ай бұрын
Giving you a dislike just for that rxjs mess that you had created. That code is unreadable, un-understandable and lacks kiss design pattern !!
@user-zw3kc8qd1z
@user-zw3kc8qd1z 18 күн бұрын
Not gonna lie, it took me some seconds to realise what's going in there and it's not the best
@mugatu2017
@mugatu2017 Жыл бұрын
What a waste of time expending time learning rxjs
@marcush3997
@marcush3997 Жыл бұрын
so seems like, the api would get the whole array every time from the example? Anyway to just modify the employees$ to make it work with the api only take in 1 new employee at a time? The only way I can take of is creating another obs$ but would love to make this work with just the two from the example 🥲
@JoshuaMorony
@JoshuaMorony Жыл бұрын
If you add the tap before the scan you should be able to just send the "add/remove" action off to the API, and then the API could handle inserting/removing just that one bit of data
Angular is about to get its most IMPORTANT change in a long time...
10:15
Why didn't the Angular team just use RxJS instead of Signals?
8:15
Joshua Morony
Рет қаралды 90 М.
Best Toilet Gadgets and #Hacks you must try!!💩💩
00:49
Poly Holy Yow
Рет қаралды 20 МЛН
Каха заблудился в горах
00:57
К-Media
Рет қаралды 9 МЛН
Iron Chin ✅ Isaih made this look too easy
00:13
Power Slap
Рет қаралды 36 МЛН
WTF is "Zone.js" and is it making your app slow?
13:21
Joshua Morony
Рет қаралды 53 М.
Why I decided to switch to the inject() function in Angular
6:10
Joshua Morony
Рет қаралды 55 М.
Angular Clean Architecture Part 4 - DTOs And Mappers
4:21
Kirill Ushakov - Web & Mobile Development
Рет қаралды 952
Here's what I've figured out about Angular signals
8:33
Joshua Morony
Рет қаралды 15 М.
Why Signals Are Better Than React Hooks
16:30
Web Dev Simplified
Рет қаралды 466 М.
ngTemplateOutlet is WAY more useful than I realised
16:36
Joshua Morony
Рет қаралды 73 М.
Why Angular Signals? Write Your First Signal
14:25
Angular University
Рет қаралды 8 М.
The easier way to code Angular apps
9:54
Joshua Morony
Рет қаралды 65 М.
TOP 6 Mistakes in RxJS code
18:35
Decoded Frontend
Рет қаралды 16 М.