Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pull-based behaviors? #131

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion reactive-banana/src/Reactive/Banana/Frameworks.hs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ module Reactive.Banana.Frameworks (
-- ** Core functions
compile, MomentIO,
module Control.Event.Handler,
fromAddHandler, fromChanges, fromPoll,
fromAddHandler, fromChanges, fromPoll, fromPull,
reactimate, Future, reactimate',
changes,
-- $changes
Expand Down Expand Up @@ -185,6 +185,9 @@ fromAddHandler = MIO . fmap E . Prim.fromAddHandler
fromPoll :: IO a -> MomentIO (Behavior a)
fromPoll = MIO . fmap B . Prim.fromPoll

fromPull :: IO a -> MomentIO (Behavior a)
fromPull = MIO . fmap B . Prim.fromPull

-- | Input,
-- obtain a 'Behavior' from an 'AddHandler' that notifies changes.
--
Expand Down
13 changes: 13 additions & 0 deletions reactive-banana/src/Reactive/Banana/Internal/Combinators.hs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
reactive-banana
------------------------------------------------------------------------------}
{-# LANGUAGE RecursiveDo, FlexibleInstances, NoMonomorphismRestriction #-}
{-# LANGUAGE ScopedTypeVariables #-}
module Reactive.Banana.Internal.Combinators where

import Control.Concurrent.MVar
Expand All @@ -16,6 +17,8 @@ import Data.Functor.Identity
import Data.IORef
import qualified Reactive.Banana.Prim as Prim
import Reactive.Banana.Prim.Cached
import Reactive.Banana.Prim.Types (EvalP, _evalP)
import Reactive.Banana.Prim.Plumbing (newLatchIO, alwaysP)

type Build = Prim.Build
type Latch a = Prim.Latch a
Expand Down Expand Up @@ -100,6 +103,16 @@ fromPoll poll = do
return $ Prim.fromPure p
stepperB a e

-- | Like fromPoll, but the IO is run only when the value of the Behavior is
-- demanded.
--
-- FIXME it should only run the action at most once per evaluation cycle.
fromPull :: forall a . IO a -> Moment (Behavior a)
fromPull pull = cacheAndSchedule $ liftBuild $ do
latch <- newLatchIO pull
pulse <- alwaysP
pure (latch, pulse)

liftIONow :: IO a -> Moment a
liftIONow = liftIO

Expand Down
28 changes: 28 additions & 0 deletions reactive-banana/src/Reactive/Banana/Prim/Plumbing.hs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,34 @@ newLatch a = mdo

return (updateOn, latch)

-- | Make a new 'Latch' which runs an IO whenever it is checked.
-- FIXME Please review this. Ideally the IO would run at most once per
-- cycle.
newLatchIO :: IO a -> Build (Latch a)
newLatchIO ioa = mdo
a <- liftIO ioa
latch <- liftIO $ newRef $ Latch
{ _seenL = beginning
, _valueL = a
, _evalL = do
Latch {..} <- readRef latch
RW.tell _seenL
a <- liftIO ioa
modify' latch (\x -> x { _valueL = a })
pure a
}
w <- liftIO $ mkWeakRefValue latch latch
lw <- liftIO $ newRef $ LatchWrite
{ _evalLW = do
Latch {..} <- readRef latch
pure _valueL
, _latchLW = w
}
_ <- liftIO $ mkWeakRefValue latch lw
always <- alwaysP
(P always) `addChild` (L lw)
pure latch

-- | Make a new 'Latch' that caches a previous computation.
cachedLatch :: EvalL a -> Latch a
cachedLatch eval = unsafePerformIO $ mdo
Expand Down