Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save couzic/242a8015ed67f972fc29fe6bb2a97d1f to your computer and use it in GitHub Desktop.

Select an option

Save couzic/242a8015ed67f972fc29fe6bb2a97d1f to your computer and use it in GitHub Desktop.
Temporal Transparency - Data Access Layer Example
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