Data and Page Content Refresh patterns in Angular
Tags: angular, angular2, rxjs, observable
Published: 2018-12-14 19:55:59

---
title: Data and Page Content Refresh patterns in Angular
published: true
description: Helpful patterns for data refresh in Angular and RxJS Observables
tags: Angular, Angular 2, RxJS, Observable
canonical_url: https://blog.eyas.sh/2018/12/data-and-page-content-refresh-patterns-in-angular/
---

Part of why I recommend using RxJS `Observable`s all the way through in Angular TypeScript code, and [only unpacking them at the closest point to where the UI is declared (often using the `| async` pipe)](https://blog.eyas.sh/2018/12/use-asyncpipe-when-possible/), 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:

```ts
@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"/>`
})
export class TaskComponent {
  constructor(private readonly http: HttpClient) {}
  readonly task$ = 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:

```ts
export class TaskComponent {
  constructor(private readonly http: HttpClient) {}
  
  private readonly refreshToken$ = new BehaviorSubject(undefined);
  private readonly task$ = 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 call `refreshToken$.next` to make sure new data is loaded.

> Other patterns and examples, including **regularly polling for updates**, and **using input Observables to request data** (e.g. `ActivatedRoute.params`) are available [in the full blog post](https://blog.eyas.sh/2018/12/data-and-page-content-refresh-patterns-in-angular/).

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 include `catchError` 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](https://blog.eyas.sh/2018/12/data-and-page-content-refresh-patterns-in-angular/)