on develop help sign in
Add more comments from the reflex-vty host
commit3e36d452e234c8f3910040f7425738d9ce475303 authorCarlo Nucera <carlo.nucera@protonmail.com> dateThu 30 Apr 2020, 17:25 (6 years ago) parent7e75086
1 file changed+46−7
src/Reflex/Host/Headless.hs+46−7
src/Reflex/Host/Headless.hs+46−7
@@ -48,28 +48,67 @@ runHeadlessApp
4848 -- closed at the first occurrence of the resulting 'Event'.
4949 -> IO ()
5050runHeadlessApp guest =
51+ -- We are using the 'Spider' implementation of reflex. Running the host
52+ -- allows us to take actions on the FRP timeline. The scoped type signature
53+ -- specifies that our host runs on the Global timeline.
54+ -- For more information, see 'Reflex.Spider.Internal.runSpiderHost'.
5155 (runSpiderHost :: SpiderHost Global a -> IO a) $ do
56+ -- Create the "post-build" event and associated trigger. This event fires
57+ -- once, when the application starts.
5258 (postBuild, postBuildTriggerRef) <- newEventWithTriggerRef
59+ -- Create a queue to which we will write 'Event's that need to be
60+ -- processed.
5361 events <- liftIO newChan
62+ -- Run the "guest" application, providing the appropriate context. We'll
63+ -- return the result of the action, and a 'FireCommand' that will be used to
64+ -- trigger events.
5465 (result, fc@(FireCommand fire)) <- do
55− hostPerformEventT $
56− flip runPostBuildT postBuild $
57− flip runTriggerEventT events $
66+ hostPerformEventT $ -- Allows the guest app to run
67+ -- 'performEvent', so that actions
68+ -- (e.g., IO actions) can be run when
69+ -- 'Event's fire.
70+
71+ flip runPostBuildT postBuild $ -- Allows the guest app to access to
72+ -- a "post-build" 'Event'
73+
74+ flip runTriggerEventT events $ -- Allows the guest app to create new
75+ -- events and triggers and write
76+ -- those triggers to a channel from
77+ -- which they will be read and
78+ -- processed.
5879 guest
80+
81+ -- Read the trigger reference for the post-build event. This will be
82+ -- 'Nothing' if the guest application hasn't subscribed to this event.
5983 mPostBuildTrigger <- readRef postBuildTriggerRef
84+
85+ -- When there is a subscriber to the post-build event, fire the event.
6086 forM_ mPostBuildTrigger $ \postBuildTrigger ->
6187 fire [postBuildTrigger :=> Identity ()] $ return ()
88+
89+ -- Subscribe to an 'Event' of that the guest application can use to
90+ -- request application shutdown. We'll check whether this 'Event' is firing
91+ -- to determine whether to terminate.
6292 shutdown <- subscribeEvent result
93+
94+ -- 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+ -- terminates.
6397 fix $ \loop -> do
98+ -- Read the next event (blocking).
6499 ers <- liftIO $ readChan events
65− stop <- fireEventTriggerRefs fc ers $ readEvent shutdown >>= \case
66− Nothing -> return False
67− Just _ -> return True
100+ stop <- do
101+ -- Fire events that have subscribers.
102+ fireEventTriggerRefs fc ers $
103+ -- Check if the shutdown 'Event' is firing.
104+ readEvent shutdown >>= \case
105+ Nothing -> return False
106+ Just _ -> return True
68107 if or stop
69108 then return ()
70109 else loop
71110 where
72− -- | Use the given 'FireCommand' to fire events that have subscribers
111+ -- Use the given 'FireCommand' to fire events that have subscribers
73112 -- and call the callback for the 'TriggerInvocation' of each.
74113 fireEventTriggerRefs
75114 :: (MonadIO m)