ghcjs + sodium: no events after some time
Asked Answered
T

0

6

UPDATE: there is a issue in ghcjs: https://github.com/ghcjs/ghcjs/issues/296


i play with ghcjs and sodium but after 3 seconds my application doesn't emit events anymore.

a minimal example:

  • a button: emit events
  • a counter behavior: counts the button clicks
  • a div: displays the counter behavior
  • after 3 seconds, the div doesn't update anymore
  • if i reload the page, the counter updates again - for 3 seconds

{-# 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

The full example is under https://github.com/j-keck/ghcjs-sodium

thanks

Trivandrum answered 31/5, 2015 at 19:37 Comment(2)
This sounds like the behaviour got garbage collected. JavaScript does not have weak references, and sodium depends on weak references. It is true that GHCJS has its own simulation of weak references, however reachability of things finishes after the execution of main I believe.Maclaine
yes, i know. i have a link to the bug @github addedTrivandrum

© 2022 - 2024 — McMap. All rights reserved.