on develop help sign in
reflex/src/Reflex/Network.hs
45 lines 2.0 kB history raw
1{-# LANGUAGE CPP #-}
2{-# LANGUAGE MultiParamTypeClasses #-}
3#ifdef USE_REFLEX_OPTIMIZER
4{-# OPTIONS_GHC -fplugin=Reflex.Optimizer #-}
5#endif
6-- |
7-- Module:
8-- Reflex.Network
9-- Description:
10-- This module provides combinators for building FRP graphs/networks and modifying them dynamically.
11module Reflex.Network
12 ( networkView
13 , networkHold
14 , untilReady
15 ) where
17import Reflex.Class
18import Reflex.Adjustable.Class
19import Reflex.NotReady.Class
20import Reflex.PostBuild.Class
22-- | A 'Dynamic' "network": Takes a 'Dynamic' of network-creating actions and replaces the network whenever the 'Dynamic' updates.
23-- The returned Event of network results fires at post-build time and when the 'Dynamic' updates.
24-- Note: Often, the type 'a' is an Event, in which case the return value is an Event-of-Events, where the outer 'Event' fires
25-- when switching networks. Such an 'Event' would typically be flattened (via 'switchPromptly').
26networkView :: (NotReady t m, Adjustable t m, PostBuild t m) => Dynamic t (m a) -> m (Event t a)
27networkView child = do
28 postBuild <- getPostBuild
29 let newChild = leftmost [updated child, tagCheap (current child) postBuild]
30 snd <$> runWithReplace notReady newChild
32-- | Given an initial "network" and an 'Event' of network-creating actions, create a network that is recreated whenever the Event fires.
33-- The returned Dynamic of network results occurs when the Event does.
34-- Note: Often, the type 'a' is an Event, in which case the return value is a Dynamic-of-Events that would typically be flattened.
35networkHold :: (Adjustable t m, MonadHold t m) => m a -> Event t (m a) -> m (Dynamic t a)
36networkHold child0 newChild = do
37 (result0, newResult) <- runWithReplace child0 newChild
38 holdDyn result0 newResult
40-- | Render a placeholder network to be shown while another network is not yet
41-- done building
42untilReady :: (Adjustable t m, PostBuild t m) => m a -> m b -> m (a, Event t b)
43untilReady a b = do
44 postBuild <- getPostBuild
45 runWithReplace a $ b <$ postBuild