[英]Why does removing BehaviourSubject cause my service to delay for 3-4 seconds?
I had earlier created a BehaviourSubject
and on subscribing it I was making a service call.我之前创建了一个
BehaviourSubject
并且在订阅它时我正在拨打服务电话。 But, due to the nature of BehaviorSubject
, there was an unnecessary on the initialisation of the BehaviouSubject
.但是,由于 BehaviorSubject 的性质,
BehaviorSubject
的初始化是BehaviouSubject
的。 So, on further reading, I changed BehaviourSubjedct
to ReplaySubject(1)
.因此,在进一步阅读时,我将
BehaviourSubjedct
更改为ReplaySubject(1)
。
Now, In the main @Injectable
class where I have defined the services, there is another service which is independent, that gets delayed once I made the above changes.现在,在我定义了服务的主要
@Injectable
class 中,还有另一个独立的服务,一旦我进行了上述更改,就会延迟。 How do I determine the cause of the issue here?我如何在这里确定问题的原因?
component.ts组件.ts
constructor(private _subjectService: SubjectService, private _s1: Service1){
this._s1.Call1();
this.testFunction();
}
testFunction(){
this._subjectService.GetData().subscribe(res => {
if (res !== null) {
this._s1.Call2();
}
}
}
SubjectService主题服务
// filterSubject = new BehaviorSubject(<KendoRequestVM>{});
// filterDataSubject = new BehaviorSubject(<KendoRequestVM>{});
filterSubject = new ReplaySubject<KendoRequestVM>(1);
filterDataSubject = new ReplaySubject<KendoRequestVM>(1);
Here, Call1() is getting called when I use BehaviouSubject
instead of ReplaySubject
.在这里,当我使用
BehaviouSubject
而不是ReplaySubject
时,会调用 Call1() 。
A behavior subject will always return immediately (synchronously even).行为主体总是会立即返回(甚至同步)。 A replay subject will only return immediately / synchronously if it has a value.
重播主题只有在有值的情况下才会立即/同步返回。 It's impossible to tell with just the code you've shared because you haven't shared the part that is populating the subjects (eg calling
next
) but that seems like the most likely explanation.仅使用您共享的代码是不可能的,因为您没有共享填充主题的部分(例如调用
next
),但这似乎是最有可能的解释。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.