简体   繁体   English

CombineLatest是否不会因为behaviorSubject而开火?

[英]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. 它要求用户可观察,并且数据要在BehaviorSubject中提交。 I call form.service.formToSubmit$.next(form); 我叫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. 因为combinateLatest也在用户可观察到的发射时触发,所以我检查formToSubmit是否不为空,并且在提交后将其设置为null。 But its not firing when I update formToSubmit. 但是当我更新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. 但最终目标是弄清楚如何执行上述功能,但允许我像以下功能一样组合多达3个可观察到的值。

  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. 我只是想确保Im正确地使用了Observable。 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). 另外,如果这是在“组件”内部,则我建议在“ onInit”中而不是在构造函数中运行该位(我喜欢构造函数代码精简)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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