paf31 / purescript-behaviors

A simple push-pull FRP implementation
MIT License
135 stars 21 forks source link

Switcher wrong time semantics. #34

Open voland62 opened 6 years ago

voland62 commented 6 years ago
module Test.Issue where

import Prelude

import Control.Monad.Eff (Eff)
import Control.Monad.Eff.Console (CONSOLE, log)
import FRP (FRP)
import FRP.Behavior (Behavior, sample_, switcher)
import FRP.Event (Event, subscribe)
import FRP.Event.Mouse (down)
import FRP.Event.Time (interval)

main ∷ ∀ e. Eff (console ∷ CONSOLE, frp ∷ FRP | e) Unit
main = do
  _ ← subscribe badStream $ log <<< show
  pure unit

testStream ∷ Event (Behavior Int) -- stream of behaviors
testStream = pure <$> down

behavior ∷ Behavior Int
behavior = switcher (pure 0) testStream

badStream ∷ Event Int
badStream = sample_ behavior (interval 1000)

Problem: badStream here fires only a second after last mouse down event. So if for ex. testStream would fire more often than once per second, badStream will never fire.

Expected behavior: badStream should tick exactly once per second, regardless of testStream.

paf31 commented 6 years ago

The problem seems to be with interval, which sets up a new timer for each subscription. It sounds like you want an "always on" event whose subscription exists outside of the event network. You could try creating such an event using the FFI, to see if that solves the problem. If it does, I will think about if/how this can fit into the semantics.