Last active
May 16, 2017 12:32
-
-
Save couzic/128d66186d5c919f449dbe23ea7faafc to your computer and use it in GitHub Desktop.
Temporal Transparency - CQRS 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 = { | |
| update(id: Person) { | |
| Observable.ajax | |
| .getJSON(`/api/person/${id}`) | |
| .subscribe(person => { | |
| peopleById.set(id, person) | |
| peopleById$.next(peopleById) | |
| }) | |
| } | |
| select(id: PersonId): Observable<Person> { | |
| 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