Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to combine 3 firebase observables without losing dynamic updates

I have a 3 parallel firebase requires. And I need to join them as 1 single object. Code as below:

import {Http} from "@angular/http";
import {AngularFireDatabase} from "angularfire2";
import {Thread} from "../model/thread";
import {Message} from "../model/message";
import {Participant} from "../model/participant";

constructor(private  http: Http, private db:AngularFireDatabase) { }

loadUserThreads(uid): Observable<AllUserData> {
return Observable.forkJoin([
  this.db.list('participant/' + uid + '/threads').first(),
  this.db.list('participant/' + uid + '/messages').first(),
  this.db.list('participant/' + uid + '/participants').first()
])
  .map((data:any[]) => {
    let threads: Thread[] = data[0];
    let messages: Message[] = data[1];
    let participants: Participant[] = data[2];
    let AllUserData = {
      threads: threads,
      messages: messages,
      participants: participants
    }
    return AllUserData;
  })
  }

This work and return the exact format I need, which is with this interface:

export interface AllUserData {
  participants: Participant[];
  threads: Thread[];
  messages: Message[];
}

The problem is forkJoin won't run without .first() all the firebase observables. And if I use .first() to complete all the firebase observables, I LOST the dynamic updates which why I use firebase the first place. How can I get the best of both world? - Keeping the firebase observables live but can join them like my interface format? I am pulling my hair out on this. Any help will be greatly appreciated!

like image 358
Hugh Hou Avatar asked Jan 21 '17 22:01

Hugh Hou


1 Answers

Instead of .forkJoin(...) you could use .combineLatest(...).

I also did streamline your map a bit:

Observable.combineLatest(
  this.db.list('participant/' + uid + '/threads'),
  this.db.list('participant/' + uid + '/messages'),
  this.db.list('participant/' + uid + '/participants')
)
.map(([threads, messages, participants]: [Thread[], Message[], Participant[]]) =>
  ({ threads, messages, participants })
)
like image 114
olsn Avatar answered Oct 11 '22 07:10

olsn