我正在尝试在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 不是最新的。
如何修复它以便按时更新。此外,欢迎对代码进行任何改进。
谢谢。