Skip to content

Instantly share code, notes, and snippets.

@codediodeio

codediodeio/ref.ts

Last active Jul 3, 2018
Embed
What would you like to do?
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