on develop help sign in
Merge branch 'develop' into ghc810
78ee62aAli Abrar
reflex.cabal+8
@@ -342,6 +342,14 @@ test-suite RequesterT
342 Reflex.Plan.Pure
342 Reflex.Plan.Pure
343 Test.Run
343 Test.Run
344
344
345+test-suite Headless
346+ default-language: Haskell2010
347+ type: exitcode-stdio-1.0
348+ main-is: Headless.hs
349+ hs-source-dirs: test
350+ build-depends: base
351+ , reflex
352+
345test-suite Adjustable
353test-suite Adjustable
346 default-language: Haskell2010
354 default-language: Haskell2010
347 type: exitcode-stdio-1.0
355 type: exitcode-stdio-1.0
src/Reflex/Host/Headless.hs+11−9
@@ -7,6 +7,7 @@
7module Reflex.Host.Headless where
7module Reflex.Host.Headless where
8
8
9import Control.Concurrent.Chan (newChan, readChan)
9import Control.Concurrent.Chan (newChan, readChan)
10+import Control.Monad (unless)
10import Control.Monad.Fix (MonadFix, fix)
11import Control.Monad.Fix (MonadFix, fix)
11import Control.Monad.IO.Class (MonadIO, liftIO)
12import Control.Monad.IO.Class (MonadIO, liftIO)
12import Control.Monad.Primitive (PrimMonad)
13import Control.Monad.Primitive (PrimMonad)
@@ -15,7 +16,7 @@ import Data.Dependent.Sum (DSum (..), (==>))
15import Data.Foldable (for_)
16import Data.Foldable (for_)
16import Data.Functor.Identity (Identity(..))
17import Data.Functor.Identity (Identity(..))
17import Data.IORef (IORef, readIORef)
18import Data.IORef (IORef, readIORef)
18−import Data.Maybe (catMaybes)
19+import Data.Maybe (catMaybes, fromMaybe)
19import Data.Traversable (for)
20import Data.Traversable (for)
20
21
21import Reflex
22import Reflex
@@ -82,32 +83,33 @@ runHeadlessApp guest =
82 -- 'Nothing' if the guest application hasn't subscribed to this event.
83 -- 'Nothing' if the guest application hasn't subscribed to this event.
83 mPostBuildTrigger <- readRef postBuildTriggerRef
84 mPostBuildTrigger <- readRef postBuildTriggerRef
84
85
85− -- When there is a subscriber to the post-build event, fire the event.
86− for_ mPostBuildTrigger $ \postBuildTrigger ->
87− fire [postBuildTrigger :=> Identity ()] $ pure ()
88−
89 -- Subscribe to an 'Event' of that the guest application can use to
86 -- Subscribe to an 'Event' of that the guest application can use to
90 -- request application shutdown. We'll check whether this 'Event' is firing
87 -- request application shutdown. We'll check whether this 'Event' is firing
91 -- to determine whether to terminate.
88 -- to determine whether to terminate.
92 shutdown <- subscribeEvent result
89 shutdown <- subscribeEvent result
93
90
91+ -- When there is a subscriber to the post-build event, fire the event.
92+ soa <- for mPostBuildTrigger $ \postBuildTrigger ->
93+ fire [postBuildTrigger :=> Identity ()] $ isFiring shutdown
94+
94 -- The main application loop. We wait for new events and fire those that
95 -- The main application loop. We wait for new events and fire those that
95 -- have subscribers. If we detect a shutdown request, the application
96 -- have subscribers. If we detect a shutdown request, the application
96 -- terminates.
97 -- terminates.
97− fix $ \loop -> do
98+ unless (or (fromMaybe [] soa)) $ fix $ \loop -> do
98 -- Read the next event (blocking).
99 -- Read the next event (blocking).
99 ers <- liftIO $ readChan events
100 ers <- liftIO $ readChan events
100 stop <- do
101 stop <- do
101 -- Fire events that have subscribers.
102 -- Fire events that have subscribers.
102 fireEventTriggerRefs fc ers $
103 fireEventTriggerRefs fc ers $
103 -- Check if the shutdown 'Event' is firing.
104 -- Check if the shutdown 'Event' is firing.
104− readEvent shutdown >>= \case
105+ isFiring shutdown
105− Nothing -> pure False
106− Just _ -> pure True
107 if or stop
106 if or stop
108 then pure ()
107 then pure ()
109 else loop
108 else loop
110 where
109 where
110+ isFiring ev = readEvent ev >>= \case
111+ Nothing -> pure False
112+ Just _ -> pure True
111 -- Use the given 'FireCommand' to fire events that have subscribers
113 -- Use the given 'FireCommand' to fire events that have subscribers
112 -- and call the callback for the 'TriggerInvocation' of each.
114 -- and call the callback for the 'TriggerInvocation' of each.
113 fireEventTriggerRefs
115 fireEventTriggerRefs
test/Headless.hsnew file+10
@@ -0,0 +1,10 @@
1+module Main where
2+
3+import Reflex
4+import Reflex.Host.Headless (runHeadlessApp)
5+
6+main :: IO ()
7+main = do
8+ runHeadlessApp $ do
9+ pb <- getPostBuild
10+ performEvent (pure <$> pb)