等待BaconJS中从属流的最新值? [英] Wait for latest values from dependent streams in BaconJS?
问题描述
gradingResult
和 contextId
取决于 studentResponse
。当所有3个具有最新值时,我需要发起一个事件,而且只有一个事件(否则,这是微不足道的)。 我试过#combineTemplate和#sampledBy studentResponse
。不幸的是,我总是看到错误的数据--- gradingResult
和 contextId
在组合模板中有旧值。我如何等待所有流都具有最新值?
代码如下所示:
var studentResponse = new Bacon.Bus();
var gradingResult = new Bacon.Bus();
var contextId = new Bacon.Bus();
studentResponse.onValue(function(f){
gradingResult.push(f);
contextId.push(f);
});
Bacon.combineTemplate({
studentResponse:studentResponse,
gradingResult:gradingResult,
contextId:contextId
})。sampledBy(studentResponse)
.onValue(function(t){
console.log(t);
});
studentResponse.push(1);
studentResponse.push(2);
studentResponse.push(3);
链接到jsfiddle: https://jsfiddle.net/3o4c9sm8/1/
更新:这是一个人为的例子。在真实的代码中,gradingResult是一个ajax请求。 gradingResult和contextId都对studentResponse有时间依赖关系。解决方案是通过最后更新的流进行采样。在这种情况下,它是 contextId
。将代码更改为以下代码将生效:
var studentResponse = new Bacon.Bus();
var gradingResult = new Bacon.Bus();
var contextId = new Bacon.Bus();
studentResponse.onValue(function(f){
gradingResult.push(f);
contextId.push(f);
});
Bacon.combineTemplate({
studentResponse:studentResponse,
gradingResult:gradingResult,
contextId:contextId
})。sampledBy(contextId)// Sampling (t){
console.log(t);
});
studentResponse.push(1);
studentResponse.push(2);
studentResponse.push(3);
I have 3 streams. gradingResult
and contextId
depend on studentResponse
. I need to fire an event and only one event (otherwise, this is trivial) when all 3 have the latest values.
I've tried #combineTemplate and #sampledBy studentResponse
. Unfortunately, I always see the wrong data---gradingResult
and contextId
have the old values in the combined template. How can I wait for all streams to have the latest values?
Code is shown below:
var studentResponse = new Bacon.Bus();
var gradingResult = new Bacon.Bus();
var contextId = new Bacon.Bus();
studentResponse.onValue(function(f) {
gradingResult.push(f);
contextId.push(f);
});
Bacon.combineTemplate({
studentResponse: studentResponse,
gradingResult: gradingResult,
contextId: contextId
}).sampledBy(studentResponse)
.onValue(function(t) {
console.log(t);
});
studentResponse.push(1);
studentResponse.push(2);
studentResponse.push(3);
Link to jsfiddle: https://jsfiddle.net/3o4c9sm8/1/
UPDATE: this is a contrived example. In the real code, gradingResult is an ajax request. Both gradingResult and contextId have time dependencies on studentResponse
The solution is to sample by the stream that updates last. In this case, it's contextId
. Changing the code to the following makes it work:
var studentResponse = new Bacon.Bus();
var gradingResult = new Bacon.Bus();
var contextId = new Bacon.Bus();
studentResponse.onValue(function(f) {
gradingResult.push(f);
contextId.push(f);
});
Bacon.combineTemplate({
studentResponse: studentResponse,
gradingResult: gradingResult,
contextId: contextId
}).sampledBy(contextId) //Sampling by stream that updates last <---
.onValue(function(t) {
console.log(t);
});
studentResponse.push(1);
studentResponse.push(2);
studentResponse.push(3);
这篇关于等待BaconJS中从属流的最新值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!