You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Had some time to look at the repo, so here are my 2 cents of feedback 😄
state property is redundant. The BehaviorSubject that is used for the store property has a method .getValue() that lets you retrieve the state value outside of observable stream.
When using a BehaviorSubject, it's a good idea to keep it private, and expose an observable counterpart like public data$ = dataSubject.asObservable().
Regarding fetching the game details. In the game-details.service you can remove selectedGameId and make some changes similar to these: game-details.service
[...]getGameById$(id: string): Observable<Game>{if(id==null){// something went wrongreturnof(null);}elseif(this.cache[id]){// game details were already requested previousreturnof(this.cache[id]);}else{returnthis.findGameDetails(id);}}privatefindGameDetails(id: string): Observable<Game>{const gameDetails$ =this.findGameById(id).pipe(catchError((err)=>{console.error(err);returnof(null);}),retry(2));constscreenshots$=this.findScreenshotsById(id).pipe(catchError((err)=>{console.error(err);returnof(null);}),retry(2));constdata$=forkJoin([gameDetails$,screenshots$]).pipe(filter((data): data is [Game,{image: string}[]]=>data[0]!=null&&data[1]!=null),map(([gameDetails,screenshots])=>{// enhance game object with screenshotsgameDetails.screenshots=screenshots;this.setState({selectedGame: gameDetails});returngameDetails;}),tap((game)=>this.updateCache(game)));this.setState({loading: true});returndata$.pipe(filter(Boolean),finalize(()=>this.setState({loading: false})));}[...]
The next thing would be to decide the way of invalidating the cache.
And in the game-details.component you would have something like:
...
exportclassGameDetailsComponent{game$=this.route.params.pipe(delay(0),switchMap(({ id })=>{returnthis.facade.getGameById$(id);}));loading$=this.facade.loadingGame$;constructor(publicfacade: GameDetailsFacade,privateroute: ActivatedRoute){}}
Basically, wherever possible, try and use streams that end up in the templates through async pipe, avoiding subscribes in the component.
Regarding the facade, you can have one file .facade.ts that would have the selectors and methods to interact with the store (as you have here), or you can have .selector.ts/.query.ts to select data from the store and .service.ts or whatever you like to call it, to update the store. In the end it's all just actions/events up, date/streams down so I don't have much to add here.
Had some time to look at the repo, so here are my 2 cents of feedback 😄
BehaviorSubject
that is used for thestore
property has a method.getValue()
that lets you retrieve the state value outside of observable stream.BehaviorSubject
, it's a good idea to keep it private, and expose an observable counterpart likepublic data$ = dataSubject.asObservable()
.game-details.service
you can removeselectedGameId
and make some changes similar to these:game-details.service
The next thing would be to decide the way of invalidating the cache.
And in the
game-details.component
you would have something like:Basically, wherever possible, try and use streams that end up in the templates through
async
pipe, avoiding subscribes in the component.Regarding the facade, you can have one file
.facade.ts
that would have the selectors and methods to interact with the store (as you have here), or you can have.selector.ts
/.query.ts
to select data from the store and.service.ts
or whatever you like to call it, to update the store. In the end it's all just actions/events up, date/streams down so I don't have much to add here.I would recommend using an existing state management solution, though :)
Some libraries are:
https://ngrx.io/
https://opensource.salesforce.com/akita/
https://github.com/ngneat/elf
https://github.com/ngneat/query (something like react-query)
This account has a lot of nice packages for angular https://github.com/ngneat
For dynamic forms: https://formly.dev/ worked well last time I tried it.
The text was updated successfully, but these errors were encountered: