术语:什么是“故障"?在函数反应式编程/RX? [英] Terminology: What is a "glitch" in Functional Reactive Programming / RX?

查看:49
本文介绍了术语:什么是“故障"?在函数反应式编程/RX?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在函数式响应式编程的上下文中,故障"的定义是什么?

What is the definition of a "glitch" in the context of Functional Reactive Programming?

我知道在某些 FRP 框架中可能会出现故障",而在其他框架中则不会.例如,RX 不是无故障的,而 ReactFX 是无故障的 [1].

I know that in some FRP frameworks "glitches" can occur while in others not. For example RX is not glitch free while ReactFX is glitch free [1].

有人可以举一个非常简单的例子来说明在使用 RX 时故障如何以及何时发生,并在同一个例子中说明相应的 ReactFX 解决方案如何以及为什么没有故障.

Could someone give a very simple example demonstrating how and when glitches can occur when using RX and show on the same example how and why the corresponding ReactFX solution is glitch free.

感谢阅读.

推荐答案

定义

我(自己的)最喜欢的定义:

Definition

My (own) favorite definition:

故障是可观察状态的暂时不一致.

A glitch is a temporary inconsistency in the observable state.

来自 Scala.Rx 的定义:

在 FRP 的上下文中,故障是数据流图中的暂时不一致.由于更新不会立即发生,而是需要时间来计算,因此 FRP 系统内的值可能会在更新过程中暂时不同步.此外,根据 FRP 系统的性质,在一次传播中可能会多次更新节点.

In the context of FRP, a glitch is a temporary inconsistency in the dataflow graph. Due to the fact that updates do not happen instantaneously, but instead take time to compute, the values within an FRP system may be transiently out of sync during the update process. Furthermore, depending on the nature of the FRP system, it is possible to have nodes be updated more than once in a propagation.

示例

考虑整数变量ab.定义 sumprod 使得
sum := a + b,
prod := a * b.

Example

Consider integer variables a, b. Define sum and prod such that
sum := a + b,
prod := a * b.

让我们将这个示例重写为 JavaFX:

Let's rewrite this example to JavaFX:

IntegerProperty a = new SimpleIntegerProperty();
IntegerProperty b = new SimpleIntegerProperty();
NumberBinding sum = a.add(b);
NumberBinding prod = a.multiply(b);

现在让我们写一点一致性检查:

Now let's write a little consistency check:

InvalidationListener consistencyCheck = obs -> {
    assert sum.intValue() == a.get() + b.get();
    assert prod.intValue() == a.get() * b.get();
};

sum.addListener(consistencyCheck);
prod.addListener(consistencyCheck);

a.set(1);
b.set(2);

这段代码失败,最后一行出现断言错误,因为:

This code fails with an assertion error on the last line, because:

  • b 更新(到 2)
    • sum 更新(到 3)
      • `consistencyCheck`被触发,`a == 1`,`b == 2`,但是`prod == 0`,因为`prod`还没有更新
      • b is updated (to 2)
        • sum is updated (to 3)
          • `consistencyCheck` is triggered, `a == 1`, `b == 2`, but `prod == 0`, because `prod` has not been updated yet

          这是一个小故障—prod 暂时与 ab 不一致.

          This is a glitch — prod is temporarily inconsistent with a and b.

          首先请注意,ReactFX 并非无故障",但它为您提供了消除故障的工具.除非您有意识地努力使用它们,否则 ReactFX 并不比 RX(例如 rxJava)更没有故障.

          First note that ReactFX is not "glitch free" out of the box, but it gives you tools to eliminate glitches. Unless you take some conscious effort to use them, ReactFX is not more glitch-free than RX (e.g. rxJava).

          在 ReactFX 中消除故障的技术依赖于事件传播是同步的这一事实.另一方面,RX 中的事件传播始终是异步的,因此这些技术无法在 RX 系统中实现.

          The techniques to eliminate glitches in ReactFX rely on the fact that event propagation is synchronous. On the other hand, event propagation in RX is always asynchronous, thus these techniques cannot be implemented in an RX system.

          在上面的示例中,我们希望延迟侦听器通知,直到 sumprod 都已更新.这是使用 ReactFX 实现此目的的方法:

          In the example above, we want to defer listener notifications until both sum and prod have been updated. This is how to achieve this with ReactFX:

          import org.reactfx.Guardian;
          import org.reactfx.inhibeans.binding.Binding;
          
          IntegerProperty a = new SimpleIntegerProperty();
          IntegerProperty b = new SimpleIntegerProperty();
          Binding<Number> sum = Binding.wrap(a.add(b)); // Binding imported from ReactFX
          Binding<Number> prod = Binding.wrap(a.multiply(b)); // Binding imported from ReactFX
          
          InvalidationListener consistencyCheck = obs -> {
              assert sum.getValue().intValue() == a.get() + b.get();
              assert prod.getValue().intValue() == a.get() * b.get();
          };
          
          sum.addListener(consistencyCheck);
          prod.addListener(consistencyCheck);
          
          // defer sum and prod listeners until the end of the block
          Guardian.combine(sum, prod).guardWhile(() -> {
              a.set(1);
              b.set(2);
          });
          

          这篇关于术语:什么是“故障"?在函数反应式编程/RX?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆