Last active
July 3, 2018 19:03
-
-
Save codediodeio/aca44be19caf922eec974a7d16171b53 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') ) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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