Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save couzic/128d66186d5c919f449dbe23ea7faafc to your computer and use it in GitHub Desktop.

Select an option

Save couzic/128d66186d5c919f449dbe23ea7faafc to your computer and use it in GitHub Desktop.
Temporal Transparency - CQRS 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 = {
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