The "Behavior" recursive update in the sodium crop stream is blocked ... "

i will update the current value of Behavior (Cell / Val).

but the following code produces a thread blocked indefinitely in the event of an MVA exception .

I expected it to print the triple value of i: '. what did I miss? - thank.

  {-# LANGUAGE RecursiveDo #-}
  module Main where

  import           FRP.Sodium

  main :: IO ()
  main = do
    (e, t) <- sync newEvent

    rec
      b <- sync $ hold 0 $ snapshot (\_ i -> i + 1) e b

    sync $ listen (value b) (\i -> putStrLn $ "value of i: " ++ show i)

    sync $ t "ping"
    sync $ t "ping"
    sync $ t "ping"

    return ()

  • GHCi version 7.8.3
  • sodium-0.11.0.3
+2
source share
1 answer

The recursive let from RecursiveDois in the monad IO. The monad Reactivealso has an instance MonadFix. You can fully define bwithin Reactive, and then use syncto complete the entire definition as a transaction.

main = do
    (e, t) <- sync newEvent

    b <- sync $
        do
            rec
                b <- hold 0 $ snapshot (\_ i -> i + 1) e b
            return b

    sync $ listen (value b) (\i -> putStrLn $ "value of i: " ++ show i)

    ...

RecursiveDo . mfix.

main = do
    (e, t) <- sync newEvent

    b <- sync . mfix $ hold 0 . snapshot (\_ i -> i + 1) e

    sync $ listen (value b) (\i -> putStrLn $ "value of i: " ++ show i)

    ...

, , Behavior Event accum.

b <- sync $ accum 0 (const (+1) <$> e)

hold, snapshot mfix.

accum :: Context r => a -> Event r (a -> a) -> Reactive r (Behavior r a)
accum z efa = do
    rec
        s <- hold z $ snapshot ($) efa s
    return s
+2

Source: https://habr.com/ru/post/1663369/


All Articles