Skip to content

Instantly share code, notes, and snippets.

@codediodeio
Last active July 3, 2018 19:03
Show Gist options
  • Save codediodeio/aca44be19caf922eec974a7d16171b53 to your computer and use it in GitHub Desktop.
Save codediodeio/aca44be19caf922eec974a7d16171b53 to your computer and use it in GitHub Desktop.
export class DocRef {
private ref: firebase.firestore.DocumentReference;
private stream;
constructor(private path: string) {
this.ref = firebase.firestore().doc(path);
this.stream = Observable.create(observer => {
this.ref.onSnapshot({
next(doc) {
observer.next(doc);
}
});
}).pipe(shareReplay(1));
}
snapshot() {
return this.stream;
}
data() {
return this.stream.pipe(map(v => (v as any).data()));
}
update(data) {
return from(this.ref.update(data));
}
log() {
this.stream.subscribe(x => console.log('doc read') )
}
}
const elephant = rxfire.ref('animals/elephant');
ref.snapshot().subscribe(console.log);
ref.data().subscribe(console.log);
// Multiple subscribers, all sharing one doc read
ref.data().subscribe(console.log);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment