1

threepenny-gui で IORef を設定しようとしていますが、うまくいきません。私のアプリでは、IORef 自体はより複雑になり、それ自体は表示されませんが、この例は私が考える問題を示しています。

これが私の試みです:

testIORef2 :: IORef String -> Window -> UI ()
testIORef2 ref window = void $ do
    return window # set title "Test IORef"

    inCell <- UI.input
    outCell   <- UI.input

    getBody window #+ [
            column [
                grid [[string " In cell::", element inCell]
                     ,[string "Out cell::"  , element outCell  ]]
            , string "Cells should update while typing."
            ]]

    -- When value changes write to IORef
    on  UI.valueChange inCell $ \_ -> do
        inValue <- get value inCell
        liftIO $ writeIORef ref inValue    

    -- Read the IORef
    refVal <- liftIO $ readIORef ref

    -- Behaviour which holds the string value in the input cell
    inValue <- stepper "0" $ UI.valueChange inCell
    -- Behaviour which holds the value in the ref
    let outValue = (const refVal) <$> inValue

    -- Set the value of the output cell to the outValue
    element outCell # sink value outValue

コードの並べ替えは機能しますが、outValue は最新ではありません。

更新が時間どおりになるように修正するにはどうすればよいですか。また、コードの改善は大歓迎です。

ありがとう。

4

2 に答える 2

2

あなたが書いたコードは、おそらくあなたが意図したものではありません。この線

let outValue = (const refVal) <$> inValue

outValueは、Behavior値が定数で に等しい であることを指定しrefValueます。次に、後者の値は次の式から得られます。

refVal <- liftIO $ readIORef ref

つまり、IORefこの時点でUIモナドに格納されている値です。


を使用するIORef場合、何かが変更されたときに参照の値を読み取り、この値を使用して UI コンテンツを変更します。たとえば、次のようになります。

on  UI.valueChange inCell $ \_ -> do
    inValue  <- get value inCell
    liftIO $ writeIORef ref inValue
    outValue <- liftIO $ readIORef ref    
    element outCell # set value outValue

一貫性 (操作の順序) の理由から、IORefa のソースとしてan を使用することはお勧めできませんBehavior。後者または前者のいずれかです。

于 2015-05-02T08:37:38.260 に答える