Using 3-way / reactive client / server programs
I am trying to figure out how to use Haskell threepenny-gui using reactive in order to write a program that allows
- user selects an item from the listbox
- send selection to external server
- get list of results from server
- fill the listBox with results
- to repeat
It seems I will need to use Handler
, newEvent
and register
to do this. If someone can point me to some existing code that does something like the above, that would be great.
The closest I've found is GameThing.hs
in the threepenny-gui directory samples
(but it doesn't use register
).
UPDATE:
I am asking if I should use Handler
, newEvent
and register
--- and if so, some clarification or examples of these functions.
Also, to be clear, the big picture is:
browser --> threepenny-gui (on localhost) --> backend server (anywhere on network)
<-- <--
In other words, I need to do some I / O (based on the user's choice) and then display the results of that I / O.
UPDATE: here is my solution (based on @Taldykin answer): https://github.com/haroldcarr/rdf-triple-browser/tree/master/haskell/src
source to share
Here is a code snippet. I'll add a description later.
{-# LANGUAGE LambdaCase #-}
import Graphics.UI.Threepenny as UI
main :: IO ()
main = do
(evFillList, doFillList) <- newEvent
initialList <- valuesSupply ""
behFillList <- stepper initialList evFillList
startGUI defaultConfig $ \win -> do
list <- ul
sel <- listBox
behFillList
(pure Nothing)
(pure $ \it -> UI.span # set text it)
getBody win #+ [grid [[element list, element sel]]]
setFocus $ getElement sel
on selectionChange (getElement sel) $ \case
Nothing -> return ()
Just ix -> do
items <- currentValue behFillList
let it = items !! ix
liftIO $ valuesSupply it >>= doFillList
element list #+ [li # set html it]
setFocus $ getElement sel
valuesSupply :: String -> IO [String]
valuesSupply x = return [x ++ show i | i <- [0..9]]
source to share