投稿時間:2023-07-30 07:03:50 RSSフィード2023-07-30 07:00 分まとめ(4件)

カテゴリー等 サイト名等 記事タイトル・トレンドワード等 リンクURL 頻出ワード・要約等/検索ボリューム 登録日
海外TECH DEV Community Using Angular Signals for Global State https://dev.to/nordyj/using-angular-signals-for-global-state-3pja Using Angular Signals for Global StateWith the release of Angular we got access to the new Signals API along with a host of other features They are currently in developer preview meaning that they could technically change between now and the next release of Angular However you can use them now and they provide a much cleaner way to have reactive code in your web application They DO not replace RxJS Observables Angular services like HttpClient and resolvers still rely on RxJS However they do provide another tool in the box for responding to changes in your application that are often easier to understand than the RxJS alternative Code With RxJSIn an application that I m working on before Angular shipped I was using a custom StoreService to hold global application state I ve tried libraries like NgRx and Akita to manage global state but found them to be way too heavy handed for what I wanted not saying ANYTHING negative towards these libraries not every tool is right for every job and the authors of these libraries would probably be the first to tell you that This custom StoreService was created using RxJS and looked like this import Injectable from angular core import ApplicationState from shared models import UserCard from shared models import BehaviorSubject map from rxjs const initialState ApplicationState userCard null Injectable providedIn root export class StoreService private readonly store new BehaviorSubject lt ApplicationState gt initialState readonly userCard this store pipe map state gt state userCard setUserCard userCard UserCard null this store next this store value userCard userCard Here the UserCard interface represents the basic details of the logged in user things like name and data points for putting together a URL to their Avatar image When I wanted to retrieve the user card for the logged in user a service would be used import Inject Injectable from angular core import HttpClient from angular common http import catchError mergeMap Observable of tap from rxjs import StoreService from shared services import UserCard from shared models Injectable providedIn root export class UserCardService constructor private http HttpClient private storeService StoreService Inject API BASE URL private baseUrl string getUserCard Observable lt UserCard null gt return this storeService userCard pipe mergeMap existingUserCard gt existingUserCard of existingUserCard this http get lt UserCard gt this baseUrl v users user card pipe tap userCard gt this storeService setUserCard userCard catchError gt this storeService setUserCard null return of null In the getUserCard method on this service I first check to see if the StoreService has a previously loaded UserCard instance If it does I just return that However if the StoreService does not it will make an HTPP call to retrieve the user card from the server store that in the StoreService and then return the user card For the component that displays the user card data I would expose properties like this as public properties on the component get avatarUrl Observable lt string null gt return this storeService userCard pipe map uc gt uc avatarVersionKey amp amp uc avatarVersionKey this apiBaseUrl v users uc uniqueKey avatar size amp v uc avatarVersionKey assets images avatar small png get userFullName Observable lt string null gt return this storeService userCard pipe map siu gt siu siu givenName siu surName Then in the HTML I need to use the async pipe to get the values out lt span class name gt userFullName async lt span gt Coding With SignalsThis of course works and it s what we ve been doing for years in Angular However now that Signals have arrived this code can be greatly simplified Let s start with the Signals based implementation of the StoreService class import Injectable computed signal from angular core import ApplicationState from shared models import UserCard from shared models const initialState ApplicationState userCard null Injectable providedIn root export class StoreService private readonly store signal initialState readonly userCard computed gt this store userCard setUserCard userCard UserCard null this store update s gt s userCard userCard So far it s not a ton smaller or simplified from the RxJS version Instead of a BehaviorSubject instance I m using a signal The initial state is still being passed in to initialize the signal I then define the userCard value to be a computed value This creates a new signal based on the store signal in this case that will automatically notify all of its listeners whenever the store signal is updated and return just the UserCard instance When calling setUserCard I don t need to call a next method Calling next is an RxJS thing and at least for me it s counter intuitive You have to remember that Observable is a stream of events and then next makes sense However with signals I m calling update which feels more natural The update method passes the current value of the signal to an arrow function which I m then spreading into a new object and replacing the userCard value with the new UserCard Currently my state ONLY has the UserCard field but as it starts to expand to hold other global state then this becomes more useful The real “Oh… moment for the cleanliness of the code for me comes in the new implementation of the UserCardService class import Inject Injectable from angular core import HttpClient from angular common http import catchError Observable of tap from rxjs import StoreService from shared services import UserCard from shared models Injectable providedIn root export class UserCardService constructor private http HttpClient private storeService StoreService Inject API BASE URL private baseUrl string getUserCard Observable lt UserCard null gt const uc this storeService userCard if uc return of uc return this http get lt UserCard gt this baseUrl v users user card pipe tap userCard gt this storeService setUserCard userCard catchError gt this storeService setUserCard null return of null The getUserCard method still returns an Observable as the resolver consuming it wants an observable However the implementation is much easier to understand in my opinion I can execute the userCard method off the StoreService to get the current user card if there is one and because it s just a method not an RxJS Observable I can just return it straight away if I already have it no need for mergeMap I then call the HTTP endpoint like before and add the user card to the StoreService The component fields are also greatly simplified it s really nice not having to use the RxJS pipe function avatarUrl computed gt const uc this storeService userCard return uc avatarVersionKey amp amp uc avatarVersionKey this apiBaseUrl v users uc uniqueKey avatar size amp v uc avatarVersionKey assets images avatar small png userFullName computed gt const uc this storeService userCard return uc uc givenName uc surName And now in the HTML we no longer need the async pipe Instead since each of these properties are signals we access them like methods in the HTML lt span class name gt userFullName lt span gt Final ThoughtsI really like the new Signals API and am looking forward to seeing where else the Angular team takes it Will the HttpClient class eventually use Signals maybe via a new class called HttpSignalsClient to maintain backward compatibility Will there be new constructs for our HTML templates that are Signals aware Only time will tell But so far I really like what I m seeing 2023-07-29 21:13:05
Apple AppleInsider - Frontpage News Apple Watch Series 8 vs Samsung Galaxy Watch 6 - compared https://appleinsider.com/inside/apple-watch-series-8/vs/apple-watch-series-8-vs-samsung-galaxy-watch-6---compared?utm_medium=rss Apple Watch Series vs Samsung Galaxy Watch comparedAs Samsung launches the brand new Galaxy Watch series it s time to compare how the company s latest effort compares to Apple s current model the Apple Watch Series This year Samsung has introduced a couple of new models with the Galaxy Watch and Galaxy Watch Classic The former is a slightly less expensive option sacrificing a few key features to help save on cost while the Galaxy Watch Classic bumps the price materials and size a bit But for comparison s sake we ll take a look at how Samsung s new Galaxy Watch stacks up to the Apple Watch Series Read more 2023-07-29 21:09:16
海外TECH CodeProject Latest Articles Get the Best of Both Worlds: Command Line and GUI https://www.codeproject.com/Articles/649950/Get-the-Best-of-Both-Worlds-Command-Line-and-GUI command 2023-07-29 21:54:00
ニュース BBC News - Home Environmental groups warn Rishi Sunak over green pledges https://www.bbc.co.uk/news/uk-politics-66350486?at_medium=RSS&at_campaign=KARANGA proportionate 2023-07-29 21:36:58

コメント

このブログの人気の投稿

投稿時間:2021-06-17 05:05:34 RSSフィード2021-06-17 05:00 分まとめ(1274件)

投稿時間:2021-06-20 02:06:12 RSSフィード2021-06-20 02:00 分まとめ(3871件)

投稿時間:2020-12-01 09:41:49 RSSフィード2020-12-01 09:00 分まとめ(69件)