How to combine 3 firebase observables without losi

2019-02-05 08:52发布

问题:

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!

回答1:

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 })
)