ghcjs + sodium:一段时间后没有事件发生

时间:2015-05-31 19:37:17

标签: haskell frp ghcjs sodiumfrp

更新: ghcjs中存在问题:https://github.com/ghcjs/ghcjs/issues/296

我玩ghcjs和钠但是3秒后我的申请不再发出事件。

一个最小的例子:

  • 按钮:发出事件
  • 计数器行为:计算按钮点击次数
  • div:显示反击行为
  • 3秒后,div不再更新
  • 如果我重新加载页面,计数器会再次更新 - 持续3秒
{-# LANGUAGE OverloadedStrings #-}
module Main where

import           Control.Applicative ((<$>))
import           Control.Concurrent  (forkIO, threadDelay)
import           Control.Monad       (forever)
import           Data.Default        (def)
import           Data.Text           (Text, pack)
import           FRP.Sodium
import           JavaScript.JQuery   hiding (Event)


main :: IO ()
main = do
  body <- select "body"

  -- a button
  (btn, btnE) <- mkBtnE "Click"
  appendJQuery btn body


  -- a behavior: counter - increment when btnE (button event) arrive
  counterB <- sync $ accum 0 (const (+1) <$> btnE)


  -- a div with the counter value
  counterView <- mkDiv $ fmap (pack . show) counterB
  appendJQuery counterView body


  -- wait -> nothing changed
  -- forkIO $ forever (threadDelay 1000000000)
  return ()




mkBtn :: Text -> IO JQuery
mkBtn label = select "<button/>" >>= setText label


mkBtnE :: Text -> IO (JQuery, Event ())
mkBtnE label = do
  (e, t) <- sync newEvent
  btn <- mkBtn label
  on (const $ sync $ t ()) "click" def btn
  return (btn, e)


mkDiv :: Behaviour Text -> IO JQuery
mkDiv b = do
  div <- select "<div/>"
  sync $ listen (value b) (\t -> setText t div >> return ())
  return div

完整示例位于https://github.com/j-keck/ghcjs-sodium

感谢

0 个答案:

没有答案