简体   繁体   中英

Does combineLatest not fire becuase of the behaviorSubject?

The Following code is from my service component. It requires a user observable and the data to submit in a BehaviorSubject. I call form.service.formToSubmit$.next(form); to update it. because combineLatest also fires when the user observable emits I do a check if the formToSubmit is not null and it is set to null after it is submitted. But its not firing when I update formToSubmit. I have tried just bout everything.

     this.formToSubmit$ = new BehaviorSubject<Forms>(null);  
    constructor(){
    this.updateForm();

}
    updateForm(){
            combineLatest(this.authService.user$, this.formToSubmit$).pipe(
                switchMap(([user, form]) =>{
                    if(form == null) return;
                    return this.afs.collection('networks').doc(user.activeNetworkProfile.id).collection('companyProfiles').doc(user.activeCompanyProfile.id).collection('inspectionForms').doc(form.id).set(JSON.parse(JSON.stringify(form))).then(success=>{
                        this.formToSubmit$.next(null);
                    }).catch(err=>{
                        this.formToSubmit$.next(null);  
                    });
                })
            );
        }

This is the previous code that worked just fine.

updateFormdd(form: Forms){
        return Observable.create(observer=>{
            this.authService.user$.subscribe(user=>{
                this.afs.collection('networks').doc(user.activeNetworkProfile.id).collection('companyProfiles').doc(user.activeCompanyProfile.id).collection('inspectionForms').doc(form.id).set(JSON.parse(JSON.stringify(form))).then(success=>{
                    observer.next(success);
                    observer.complete();
                }).catch(err=>{
                    observer.err(err);
                });
            })
        })
    }

But ultimately the goal is to figure out how to do the above function but allow me to combine up to 3 observable like in the following function.

  updateInspection(){

            combineLatest(this.authService.user$, this.equipmentId$, this.inspectionToSubmit$).pipe(
                switchMap(([user, equipmentId, form]) =>{
                    if(form == null) return;
                    return this.afs.collection('networks').doc(user.activeNetworkProfile.id).collection('companyProfiles').doc(user.activeCompanyProfile.id).collection('equipment').doc(equipmentId).set(JSON.parse(JSON.stringify(form))).then(success=>{
                        this.formToSubmit$.next(null);
                    }).catch(err=>{
                        this.formToSubmit$.next(null);
                    });
                })
            );

    }

I just want to ensure Im using observable's properly. Thank you

The key of the difference between your two pieces of code is the "subscribe".

The working version had a subscription. Since your observables are cold observables, they will not emit until something is subscribed to them.

Figure out where you want to consume that observable and then add the subscription there.

this.formToSubmit$ = new BehaviorSubject<Forms>(null);  
constructor(){
let mySubscription = this.updateForm().subscribe(
    (dataFromCombineLatest) => {
        // This subscription should cause the code inside the combine to fire
    }
);

Also, if this is inside a "component", I do recommend to run that bit in the "onInit" instead of the constructor (I like my constructor code to be lean).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM