WX インターフェイスで Reactive-Banana を使用しています。ボタンが押されたときに、外部サービス API から値を取得する必要があります。
関数変換に基づいて変換された変更を「蓄積」するBehavior
データ型に基づくジェネリックがあります ( )。変換された値はイベントによって転送され、インターフェイスのボタンが押されたときにリモート API ( ) から取得されます。重要な部分を表すコードのスリムなバージョンを作成しました。AppState
doSomeTransformation
getRemoteValue
module Main where
{-# LANGUAGE ScopedTypeVariables #-} -- allows "forall t. Moment t"
import Graphics.UI.WX hiding (Event)
import Reactive.Banana
import Reactive.Banana.WX
{-----------------------------------------------------------------------------
Main
------------------------------------------------------------------------------}
data AppState = AppState {
count :: Int
} deriving (Show)
type String = [Char]
main :: IO ()
main = start $ do
f <- frame [text := "AppState"]
myButton <- button f [text := "Go"]
output <- staticText f []
set f [layout := margin 10 $
column 5 [widget myButton, widget output]]
let networkDescription :: forall t. Frameworks t => Moment t ()
networkDescription = do
ebt <- event0 myButton command
remoteValueB <- fromPoll getRemoteApiValue
myRemoteValue <- changes remoteValueB
let
doSomeTransformation :: AppState -> AppState
doSomeTransformation ast = ast { count = count ast }
coreOfTheApp :: Behavior t AppState
coreOfTheApp = accumB initialState $ (doSomeTransformation to combine with myRemoteValue) <$ ebt
sink output [text :== show <$> coreOfTheApp]
network <- compile networkDescription
actuate network
getRemoteApiValue :: IO Int
getRemoteApiValue = return 5
そしてカバールのconf:
name: brg
version: 0.1.0.0
synopsis: sample frp gui
-- description:
license: PublicDomain
license-file: LICENSE
author: me
maintainer: me@gmail.com
-- copyright:
category: fun
build-type: Simple
-- extra-source-files:
cabal-version: >=1.10
executable bgr
main-is: Main.hs
-- other-modules:
-- other-extensions:
build-depends: base >=4.7 && <4.8
, text
, wx ==0.92.0.0
, wxcore ==0.92.0.0
, transformers-base
, reactive-banana >=0.9 && <0.10
, reactive-banana-wx ==0.9.0.2
hs-source-dirs: src
default-language: Haskell2010
ghc-options: -Wall -O2
ここでの問題は、リモート API 値を通常のイベント値として使用できるように構成する方法です
doSomeTransformation
。バナナ反応性から次の署名があります。myRemoteValue
changes
changes :: Frameworks t => Behavior t a -> Moment t (Event t (Future a))
IO Int
fromをラップしgetRemoteApiValue
ます。
だから基本的に私はどのように私はから行くことができます:
IO Int -> Moment t (Event t (Future AppState)) -> AppState
?
ところで、この異なる関数シグネチャを持つ方がきれいかどうかはわかりません:
doSomeTransformation :: Int -> AppState -> AppState
、Int
値は API の戻り値で表されます。Behavior
2 つの と 1 つのストリームのように聞こえます。問題を解決するための悪い方法でしょうか?