Skip to content

Commit 45123e1

Browse files
authored
Merge pull request #266 from reflex-frp/ts-requestert-inconsistency
Tests showing RequesterT inconsistency with EventWriterT
2 parents b1c9506 + baadc4c commit 45123e1

File tree

2 files changed

+119
-1
lines changed

2 files changed

+119
-1
lines changed

test/EventWriterT.hs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
{-# LANGUAGE BangPatterns #-}
12
{-# LANGUAGE FlexibleContexts #-}
23
{-# LANGUAGE GADTs #-}
34
{-# LANGUAGE RecursiveDo #-}
@@ -36,6 +37,9 @@ main = do
3637
print os4
3738
os5@[[Nothing, Just [1, 2]]] <- runApp' (unwrapApp testLiveTellEventDMap) [Just ()]
3839
print os5
40+
os6 <- runApp' (unwrapApp delayedPulse) [Just ()]
41+
print os6
42+
let ![[Nothing, Nothing]] = os6
3943
return ()
4044

4145
unwrapApp :: (Reflex t, Monad m) => (a -> EventWriterT t [Int] m ()) -> a -> m (Event t [Int])
@@ -112,3 +116,17 @@ testLiveTellEventDMap pulse = do
112116
(mapToDMap $ M.singleton 1 ())
113117
((PatchDMap $ DMap.map (ComposeMaybe . Just) $ mapToDMap $ M.singleton 2 ()) <$ pulse)
114118
return ()
119+
120+
delayedPulse
121+
:: forall t m
122+
. ( Reflex t
123+
, Adjustable t m
124+
, MonadHold t m
125+
, MonadFix m
126+
)
127+
=> Event t ()
128+
-> EventWriterT t [Int] m ()
129+
delayedPulse pulse = void $ flip runWithReplace (pure () <$ pulse) $ do
130+
-- This has the effect of delaying pulse' from pulse
131+
(_, pulse') <- runWithReplace (pure ()) $ pure [1] <$ pulse
132+
tellEvent pulse'

test/RequesterT.hs

Lines changed: 101 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,17 @@
22
{-# LANGUAGE FlexibleContexts #-}
33
{-# LANGUAGE GADTs #-}
44
{-# LANGUAGE RankNTypes #-}
5+
{-# LANGUAGE RecursiveDo #-}
56
{-# LANGUAGE ScopedTypeVariables #-}
67
module Main where
78

89
import Control.Lens
910
import Control.Monad
11+
import Control.Monad.Fix
1012
import qualified Data.Dependent.Map as DMap
1113
import Data.Dependent.Sum
14+
import Data.Functor.Misc
15+
import qualified Data.Map as M
1216
import Data.These
1317

1418
import Reflex
@@ -25,15 +29,27 @@ main = do
2529
[ Just ()
2630
]
2731
print os1
28-
let ![[Just [10,9,8,7,6,5,4,3,2,1]]] = os1
2932
os2 <- runApp' (unwrapApp testSimultaneous) $ map Just $
3033
[ This ()
3134
, That ()
3235
, This ()
3336
, These () ()
3437
]
3538
print os2
39+
os3 <- runApp' (unwrapApp testMoribundRequest) [Just ()]
40+
print os3
41+
os4 <- runApp' (unwrapApp testMoribundRequestDMap) [Just ()]
42+
print os4
43+
os5 <- runApp' (unwrapApp testLiveRequestDMap) [Just ()]
44+
print os5
45+
os6 <- runApp' (unwrapApp delayedPulse) [Just ()]
46+
print os6
47+
let ![[Just [10,9,8,7,6,5,4,3,2,1]]] = os1
3648
let ![[Just [1,3,5,7,9]],[Nothing,Nothing],[Just [2,4,6,8,10]],[Just [2,4,6,8,10],Nothing]] = os2
49+
let ![[Nothing, Just [2]]] = os3
50+
let ![[Nothing, Just [2]]] = os4
51+
let ![[Nothing, Just [1, 2]]] = os5
52+
let ![[Nothing, Nothing]] = os6
3753
return ()
3854

3955
unwrapRequest :: DSum tag RequestInt -> Int
@@ -67,3 +83,87 @@ testSimultaneous pulse = do
6783
switchE = fmapMaybe (^? there) pulse
6884
forM_ [1,3..9] $ \i -> runWithReplace (requestingIdentity (RequestInt i <$ tellE)) $ ffor switchE $ \_ ->
6985
requestingIdentity (RequestInt (i+1) <$ tellE)
86+
87+
-- | Test that a widget requesting and event which fires at the same time it has been replaced
88+
-- doesn't count along with the new widget.
89+
testMoribundRequest
90+
:: forall t m
91+
. ( Reflex t
92+
, Adjustable t m
93+
, MonadHold t m
94+
, MonadFix m
95+
, Response m ~ Identity
96+
, Request m ~ RequestInt
97+
, Requester t m
98+
)
99+
=> Event t ()
100+
-> m ()
101+
testMoribundRequest pulse = do
102+
rec let requestIntOnReplace x = requestingIdentity $ RequestInt x <$ rwrFinished
103+
(_, rwrFinished) <- runWithReplace (requestIntOnReplace 1) $ requestIntOnReplace 2 <$ pulse
104+
return ()
105+
106+
-- | The equivalent of 'testMoribundRequest' for 'traverseDMapWithKeyWithAdjust'.
107+
testMoribundRequestDMap
108+
:: forall t m
109+
. ( Reflex t
110+
, Adjustable t m
111+
, MonadHold t m
112+
, MonadFix m
113+
, Response m ~ Identity
114+
, Request m ~ RequestInt
115+
, Requester t m
116+
)
117+
=> Event t ()
118+
-> m ()
119+
testMoribundRequestDMap pulse = do
120+
rec let requestIntOnReplace :: Int -> m ()
121+
requestIntOnReplace x = void $ requestingIdentity $ RequestInt x <$ rwrFinished
122+
(_, rwrFinished :: Event t (PatchDMap (Const2 () Int) Identity)) <-
123+
traverseDMapWithKeyWithAdjust
124+
(\(Const2 ()) (Identity v) -> Identity . const v <$> requestIntOnReplace v)
125+
(mapToDMap $ M.singleton () 1)
126+
((PatchDMap $ DMap.map (ComposeMaybe . Just) $ mapToDMap $ M.singleton () 2) <$ pulse)
127+
return ()
128+
129+
-- | Ensures that elements which are _not_ removed can still fire requests
130+
-- during the same frame as other elements are updated.
131+
testLiveRequestDMap
132+
:: forall t m
133+
. ( Reflex t
134+
, Adjustable t m
135+
, MonadHold t m
136+
, MonadFix m
137+
, Response m ~ Identity
138+
, Request m ~ RequestInt
139+
, Requester t m
140+
)
141+
=> Event t ()
142+
-> m ()
143+
testLiveRequestDMap pulse = do
144+
rec let requestIntOnReplace :: Int -> m ()
145+
requestIntOnReplace x = void $ requestingIdentity $ RequestInt x <$ rwrFinished
146+
(_, rwrFinished :: Event t (PatchDMap (Const2 Int ()) Identity)) <-
147+
traverseDMapWithKeyWithAdjust
148+
(\(Const2 k) (Identity ()) -> Identity <$> requestIntOnReplace k)
149+
(mapToDMap $ M.singleton 1 ())
150+
((PatchDMap $ DMap.map (ComposeMaybe . Just) $ mapToDMap $ M.singleton 2 ()) <$ pulse)
151+
return ()
152+
153+
delayedPulse
154+
:: forall t m
155+
. ( Reflex t
156+
, Adjustable t m
157+
, MonadHold t m
158+
, MonadFix m
159+
, Response m ~ Identity
160+
, Request m ~ RequestInt
161+
, PerformEvent t m
162+
, Requester t m
163+
)
164+
=> Event t ()
165+
-> m ()
166+
delayedPulse pulse = void $ flip runWithReplace (pure () <$ pulse) $ do
167+
-- This has the effect of delaying pulse' from pulse
168+
(_, pulse') <- runWithReplace (pure ()) $ pure (RequestInt 1) <$ pulse
169+
requestingIdentity pulse'

0 commit comments

Comments
 (0)