Last active
April 8, 2017 07:07
-
-
Save couzic/242a8015ed67f972fc29fe6bb2a97d1f to your computer and use it in GitHub Desktop.
Temporal Transparency - Data Access Layer Example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import {Observable, Subject} from 'rxjs' | |
| export type PersonId = string | |
| export interface Person { | |
| id: PersonId | |
| name: string | |
| } | |
| const peopleById = new Map<PersonId, Person>() | |
| const peopleById$ = new Subject<Map<PersonId, Person>>() | |
| export const peopleData = { | |
| fetch(id: PersonId): Observable<Person> { | |
| Observable.ajax | |
| .getJSON(`/api/person/${id}`) | |
| .subscribe(person => { | |
| peopleById.set(id, person) | |
| peopleById$.next(peopleById) | |
| }) | |
| return peopleById$ | |
| .map(people => people.get(id)) | |
| .filter(Boolean) | |
| .distinctUntilChanged() | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment