monokee / Sekoia

Sekoia.js - Reactivity Engine
Other
33 stars 0 forks source link

Decide on synchronous vs asynchronous data handling #12

Open monokee opened 4 years ago

monokee commented 4 years ago

The current implementation of the reactor is a brute-force approach to auto-batch incoming change requests to avoid multiple iterations of the same computations or reactions. This batching is done by scheduling the computation until the next available frame via the browsers RAF interface which makes the reactor and all data handling asynchronous.

If this behaviour is how we want to progress in the future Component.set()and ```Store.set```` both have to return a promise object. If we decide against asynchronous data setting we have to refactor to a non-raf based, synchronous reactor.

Pros of asynchronous reactor Many subsequent requests to change the same piece of data will only result in a single execution of the data's side effects (computations, reactions). Consider a slider that is being dragged by a user which can easily result in more data change requests than reactions which can be painted onto the screen on a frame by frame basis. By buffering reactions to such inputs to only execute once per available frame we essentially optimize the engine for the purpose of user interface drawing which is good. This could be a good place to more deeply integrate the (currently rather pointless) Server object or more generally all asynchronous tasks such as network requests. Side effects like computations and even reactions could contain arbitrary async logic.

Cons of asynchronous reactor Syntax in user implemented code becomes less straight forward as we move away from simple synchronous procedural logic to the asynchronous promise syntax. This could be confusing as the data changes are really synchronous in nature as far as the user can tell because there are no network requests or user implemented timeouts.

monokee commented 4 years ago

Async handling implemented in 1.04 beta. Keeping this open b/c I'm undecided if this is the way to go and probably will be until I've written more example components

monokee commented 2 years ago

Fast forward to 2022 and I eventually settled on a hybrid approach were all state read/write ops are synchronous and observer resolution is asynchronous.