WICG / observable

Observable API proposal
https://wicg.github.io/observable/
Other
575 stars 14 forks source link

Atomic transactions #91

Open gordonbrander opened 9 months ago

gordonbrander commented 9 months ago

In functional reactive programming formulations, observables (sometimes called "events" or "streams") implement the concept of an atomic "moment". Observables that fire at the same time will update during the same atomic moment. Within a discrete push-based FRP system, such as observables, this is typically implemented using some kind of transaction system.

Atomic transactions prevent inconsistent state issues, where some upstream observables have updated, while other downstream observables have not yet updated, resulting in different views of state. This kind of inconsistent state issue can be caused by order of callback registration, or by the "diamond problem" (see below).

Has there been any discussion within the Observable WG around atomic updates? Is this something that might be considered? Thanks!

Background

Atomicity makes it possible to create networks of observables that update together, during the same conceptual moment, avoiding inconsistent states that arise due to order of callback registration, or when merging multiple observables.

One area where this often comes up is the so-called "diamond problem". Picture a network of observables:

     A
   /   \
  B     C
   \   /
     D
     |
     E

Where A is a source, B and C are mapped from A, and D merges B and C together. E is writing to some sink. A is sent a new value. What happens next?

An FRP observable system that implements transactions is sometimes called "glitch-free", since it avoids the kinds of glitches that can be caused by inconsistent/intermediate states.

To combine simultaneous events, transactions typically include the concept of a "merge" function ((left, right) => choice) that allows the developer to choose between simultaneous events, or otherwise combine them into a single event for that transaction.

In addition to more deterministic/reliable state updates that do not depend upon order of callback registration or position in the network, atomicity can also allow Observable streams to be combined with other concepts such as FRP signals (atomically updated reactive state containers, sometimes called "behaviors" or "cells"). When both observables and signals are atomic, observables can sample from, and update signals without problems of inconsistent state.