Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Eyas
Eyas

Posted on • Edited on • Originally published atblog.eyas.sh

     

Data and Page Content Refresh patterns in Angular

Part of why I recommend using RxJSObservables all the way through in Angular TypeScript code, andonly unpacking them at the closest point to where the UI is declared (often using the| async pipe), is because it makes other transformations on an Observable available and convenient. Two such examples include retry and refresh logic.

Two common reasons to reload/refresh data being displayed by a component include:

  1. A user action in the application causes the data to change (especially, if it does so in ways that might result in complex state changes, etc.), and/or
  2. The data being displayed can change over time (e.g. due to a progression/change of it's state in the backend, or by another user, etc.)

Let's start with a simple example:

@Component({selector:'task',template:`<ng-container *ngIf="(task$ | async) as task">    <h1>{{task.name}}</h1>    <p>Status: {{task.status}}</p>    <sub-task *ngFor="let subTask of task.subtasks" [subTask]="subTask"/>`})exportclassTaskComponent{constructor(privatereadonlyhttp:HttpClient){}readonlytask$=this.http.get('/api/tasks/foo');}

Suppose the user adds a 'Mark as Complete' button, that mutates the server-side state of all sub-tasks. How do obtain the latest authoritative data from the server about the state of our side? Here's an approach:

exportclassTaskComponent{constructor(privatereadonlyhttp:HttpClient){}privatereadonlyrefreshToken$=newBehaviorSubject(undefined);privatereadonlytask$=this.refreshToken$.pipe(switchMap(()=>this.http.get('/api/tasks/foo')));markAsComplete(){this.http.post('/api/tasks/foo',{state:State.Done})// N.B. contrary to my advice elsewhere, I'm happy to// directly subscribe here because this subscribe// callback has side effects.// Further, I don't worry about unsubscribing since// this returned Observable is a one-shot observable// that will complete after a single request..subscribe(()=>this.refreshToken$.next(undefined));}}

Adding refresh logic this way will minimally affect our template code and looks relatively clean. Better yet, adding additional mutating functions simply need to callrefreshToken$.next to make sure new data is loaded.

Other patterns and examples, includingregularly polling for updates, andusing input Observables to request data (e.g.ActivatedRoute.params) are availablein the full blog post.

As a monad, an Observable is a neat and tidy functional construct. You can transform it using a rich set of operators. In RxJS, those also includecatchError for error handling and retrying, timed events, and combinations of multiple monads into a monad of multiple items. With the view of Observables as just another monad, reactive programming becomes just a simple extension on top of functional programming.

Dealing with these Observables for as much of the data lifecycle as possible means that you can take advantage of these constructs to transform immutable data using neat operators, rather than dealing with unpacking this data into mutable scalars.

Read more here

Top comments(2)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss
CollapseExpand
 
melroy89 profile image
Melroy van den Berg
Software Engineer & DevOps Architect. Open-source lover. Blogger.
  • Location
    Netherlands
  • Joined

Thanks!

CollapseExpand
 
siimveskilt profile image
Siim Veskilt
  • Joined
• Edited on• Edited

Hello! Thanks for this post! Also, in your example, task$ is set as private.

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Software Engineer @ Google. New York, NY.
  • Location
    New York City
  • Education
    MIT
  • Work
    Software Engineer at Google
  • Joined

More fromEyas

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp