@@ -5,7 +5,7 @@ module Wire.React.Router
5
5
6
6
import Prelude
7
7
import Control.Monad.Free.Trans (runFreeT )
8
- import Data.Foldable (class Foldable , for_ )
8
+ import Data.Foldable (class Foldable , traverse_ )
9
9
import Data.Maybe (Maybe (..))
10
10
import Effect (Effect )
11
11
import Effect.Aff (error , killFiber , launchAff , launchAff_ )
@@ -42,17 +42,12 @@ makeRouter interface { parse, print, onRoute, onTransition } =
42
42
redirect route = interface.replaceState (unsafeToForeign {}) (print route)
43
43
in
44
44
do
45
- -- replace the user-supplied fallback route with the current route, if possible
46
- { path } <- interface.locationState
47
- for_ (parse path) \route -> onTransition $ Transitioning Nothing route
48
45
fiberRef <- Ref .new Nothing
49
46
previousRouteRef <- Ref .new Nothing
50
47
let
51
48
runRouter route = do
52
- do
53
- -- if some previous long-running routing logic is still active, kill it
54
- oldFiber <- Ref .read fiberRef
55
- for_ oldFiber \fiber -> launchAff_ (killFiber (error " Transition cancelled" ) fiber)
49
+ -- if some previous long-running routing logic is still active, kill it
50
+ Ref .read fiberRef >>= traverse_ (launchAff_ <<< killFiber (error " Transition cancelled" ))
56
51
previousRoute <- Ref .read previousRouteRef
57
52
-- set the route state to "transitioning" with the previous successful route
58
53
onTransition $ Transitioning previousRoute route
@@ -73,6 +68,9 @@ makeRouter interface { parse, print, onRoute, onTransition } =
73
68
Continue -> finalise route
74
69
mempty
75
70
Ref .write (Just fiber) fiberRef
71
+ -- run on the current path
72
+ { path } <- interface.locationState
73
+ traverse_ runRouter (parse path)
76
74
component <-
77
75
React .component " Wire.Router" \_ -> React .do
78
76
React .useEffectOnce (onPushState runRouter)
0 commit comments