Rxjs / Observable: run the function once after receiving the first thread (continuous observable)

First of all, sorry for the long headline.

I am trying to subscribe to an array of continuous streams with forEach from angularfire2 , but I would also like to run the function after I confirm that the first data set has arrived:

this.people.forEach((person) => {
    person.items = this.database.list('/items' + person.key);
    person.items.subscribe((data) => {person.itemsList = data});
});

myIntendedFunction();

Is there any way to place myIntendedFunction()so that:

  • It starts after receiving the first thread datafor each personand
  • Does it work only once?
+4
source share
3 answers

, , :

const connectables: ConnectableObservable<any>[] = [];

this.people.forEach(person => {
    person.items = this.database.list('/items' + person.key);
    const connectable = person.items.publish();
    connectables.push(connectable);
    connectable.subscribe((data) => {person.itemsList = data});
});

Observable.zip(...connectables).take(1).subscribe(myIntendedFunction);

connectables.forEach(c => c.connect());

: publish() , . , connect(). person.items.share(), person.items.publish().connect(), , - .

zip() , . , , take(1).

+3

, . , "myIntendedFunction" . ( ) first() .

this.people.subscribe((person) => {
    person.items= this.database.list('/items');
    person.items.subscribe((data) => {person.itemsList = data});
});

this.people.first().subscribe(myIntendedFunction);
0

.first() import 'rxjs/add/operator/first' firebase, . . .toPromise() import 'rxjs/add/operator/toPromise' . .then(), .

    this.forEach.person((people) => {
        person.items = this.database.list('/items' + person.key);
        person.items
            .first()
            .toPromise()
            .then(() => {
                myIntendedFunction();
         });
    })
0

Source: https://habr.com/ru/post/1652007/


All Articles