bodil/purescript-signal

Atomic update

Open

#55 opened on 2016年12月9日

GitHub で見る
 (1 comment) (3 reactions) (0 assignees)PureScript (41 forks)github user discovery
enhancementhelp wanted

Repository metrics

Stars
 (260 stars)
PR merge metrics
 (PR metrics pending)

説明

Is it possible to implement atomic updates, so that if a signal receives multiple updates from its dependent signals, it will only emit one signal value if these updates are originally from the same signal update?

For example, let's say signal b and signal c depend on signal a, and signal d depends on both b and c. If there is new signal value sent to signal a, it should trigger only one signal. This is the dependency graph:

    a
  /   \
 b     c
  \   /
    d

As if right now, it triggers twice.

main = do
  cn <- channel 1
  let a = subscribe cn
  let b = (_ + 1) <$> a
  let c = (_ + 3) <$> a
  let d = (+) <$> b <*> c
  let ds = (\x -> [x]) <$> d
  let all = foldp (<>) [] ds
  send cn 100
  runSignal (all ~> show >>> log)
* Build successful.
[204,105,6]

^^^^^^^^^^^ Expect to be [204, 6], but got [204,105,6]. 204 = (100 + 1) + (100 + 3) 105 = (100 + 1) + (4) 6 = (1 + 1) + (1 + 3)

105 is an intermediate value that could be ignored if supports atomic updates.

Atomic updates is useful for UI rendering optimization. If a view's rendering depends on multiple signals, and they are all updated by the same signal value from their dependencies, atomic updates can make sure the view will only be rerendered once.

コントリビューターガイド