Created
March 17, 2019 20:42
-
-
Save zyla/10c5344cea9245542a631c1e0fad6481 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Specular.FRP.DVar | |
( DVar | |
, new | |
, read | |
, value | |
, modify_ | |
) where | |
import Prelude | |
import Control.Monad.Cleanup (runCleanupT) | |
import Data.Tuple (fst) | |
import Effect.Class (class MonadEffect, liftEffect) | |
import Effect.Uncurried (EffectFn1, mkEffectFn1, runEffectFn1) | |
import Specular.FRP (Dynamic, current, foldDyn, newEvent, pull) | |
import Specular.FRP.Base (readBehavior) | |
data DVar a = DVar (Dynamic a) (EffectFn1 (a -> a) Unit) | |
new :: forall m a. MonadEffect m => a -> m (DVar a) | |
new initial = do | |
-- Note: It's morally correct here to ignore cleanup action, | |
-- because we're not subscribing to any outside events. | |
-- | |
-- It will interact badly with the leak checker, but there's no easy | |
-- solution atm. | |
map fst $ runCleanupT do | |
update <- newEvent | |
dyn <- foldDyn ($) initial update.event | |
pure (DVar dyn (mkEffectFn1 update.fire)) | |
read :: forall m a. MonadEffect m => DVar a -> m a | |
read (DVar dyn _) = pull $ readBehavior $ current dyn | |
value :: forall a. DVar a -> Dynamic a | |
value (DVar dyn _) = dyn | |
modify_ :: forall m a. MonadEffect m => (a -> a) -> DVar a -> m Unit | |
modify_ f (DVar _ modify) = liftEffect $ runEffectFn1 modify f |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment