Wait for latest values from dependent streams in BaconJS?

帅比萌擦擦* 提交于 2020-01-07 02:49:04

问题


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


回答1:


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);



回答2:


Looks like plugging the Buses instead of pushing the value inside studentResponse.onValue does the trick:

var studentResponse = new Bacon.Bus();
var gradingResult = new Bacon.Bus();
var contextId = new Bacon.Bus();

gradingResult.plug(studentResponse);
contextId.plug(studentResponse);

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);


来源:https://stackoverflow.com/questions/32344788/wait-for-latest-values-from-dependent-streams-in-baconjs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!