how to avoid glitches in Rx

前端 未结 1 1633
孤街浪徒
孤街浪徒 2021-02-06 07:26

Unlike other \"FRP\" libraries, Rx doesn\'t prevent glitches: callbacks invoked with time-mismatched data. Is there a good way to work around this?

As an example, imagin

1条回答
  •  不知归路
    2021-02-06 08:09

    The concept

    both a and b update

    where both a and b are observables, doesn't exist as a primitive in Rx.

    There is no lossless, general operator that can be defined to decide when it receives a notification from a whether it should pass it downstream or hold off until it receives a notification from b. Notifications in Rx do not natively carry "both" semantics, or any semantics beyond the Rx Grammar for that matter.

    Furthermore, Rx's serial contract prevents an operator from taking advantage of overlapping notifications in an attempt to achieve this goal. (Though I suspect that relying on race conditions isn't your desired approach anyway.)

    See §§4.2, 6.7 in the Rx Design Guidelines.

    Thus, what I meant above by "There is no lossless, general operator that can be defined..." is that given two observables a and b with independent notifications, any operator that attempts to decide when it receives a notification from a or b whether it must push immediately or wait for the "other" value, must rely on arbitrary timings. It's guesswork. So this hypothetical operator must either drop values (e.g., DistinctUntilChanged or Throttle), or drop time (e.g., Zip or Buffer), though probably some combination of both.

    Therefore, if the agent has the ability to push a alone, or b alone, or a and b together as a notification unit, then it's the developer's responsibility to reify this concept of notification unit themselves.

    A 3-state type is required: a | b | {a,b}

    (Please excuse my lousy JS)

    var ab = function(a, b) { this.a = a; this.b = b; }
    sub.onNext(new ab('a'));        // process a alone
    sub.onNext(new ab('a', 'b'));   // process a and b together
    sub.onNext(new ab(null, 'c'));  // process c alone
    

    The shape of the observable's query no longer matters. Observers must be defined to accept this data type. It's the generator's responsibility to apply any necessary buffering or timing calculations based on the semantics of its internal state in order to produce correct notifications for its observers.

    By the way, thank you for providing a simple explanation in your edit (it seems clear to me anyway). I had first heard about "glitches" in this Rx forum discussion. As you can see, it was never really concluded. Now I wonder whether that OP's problem was really as simple as this, assuming that I've understood your problem correctly, of course. :-)

    Update:

    Here's another related discussion, including some more of my thoughts on why Rx is not FRP:

    https://social.msdn.microsoft.com/Forums/en-US/bc2c4b71-c97b-428e-ad71-324055a3cd03/another-discussion-on-glitches-and-rx?forum=rx

    0 讨论(0)
提交回复
热议问题