Skip to content

Instantly share code, notes, and snippets.

@couzic
Last active March 29, 2017 13:01
Show Gist options
  • Select an option

  • Save couzic/1461ed74d1f43d9ea8f98df48d7bdd08 to your computer and use it in GitHub Desktop.

Select an option

Save couzic/1461ed74d1f43d9ea8f98df48d7bdd08 to your computer and use it in GitHub Desktop.
react-rx-pure-connect Data Access Layer
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