Last active
March 29, 2017 13:01
-
-
Save couzic/1461ed74d1f43d9ea8f98df48d7bdd08 to your computer and use it in GitHub Desktop.
react-rx-pure-connect Data Access Layer
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' | |
| type PersonId = string | |
| interface Person { | |
| id: PersonId | |
| name: string | |
| } | |
| // A type that represents simple JS objects that map personId -> person | |
| // Could be replaced by a Map<PersonId, Person> | |
| type PeopleById = Record<PersonId, Person> | |
| const peopleById: PeopleById = {} | |
| const peopleById$ = new Subject<PeopleById>() | |
| export const peopleData = { | |
| get(id: PersonId): Observable<Person> { | |
| Observable.ajax | |
| .getJSON(`/api/person/${id}`) | |
| .subscribe(person => { | |
| peopleById[person.id] = person | |
| peopleById$.next(peopleById) | |
| }) | |
| return peopleById$.map(people => people[id]).distinctUntilChanged() | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment