@@ -5,7 +5,7 @@ module Wire.React.Router
55
66import  Prelude 
77import  Control.Monad.Free.Trans  (runFreeT )
8- import  Data.Foldable  (class  Foldable , for_ )
8+ import  Data.Foldable  (class  Foldable , traverse_ )
99import  Data.Maybe  (Maybe (..))
1010import  Effect  (Effect )
1111import  Effect.Aff  (error , killFiber , launchAff , launchAff_ )
@@ -42,17 +42,12 @@ makeRouter interface { parse, print, onRoute, onTransition } =
4242    redirect route = interface.replaceState (unsafeToForeign {}) (print route)
4343  in 
4444    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
4845      fiberRef <-  Ref .new Nothing 
4946      previousRouteRef <-  Ref .new Nothing 
5047      let 
5148        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" 
49+           --  if some previous long-running routing logic is still active, kill it
50+           Ref .read fiberRef >>= traverse_ (launchAff_ <<< killFiber (error " Transition cancelled" 
5651          previousRoute <-  Ref .read previousRouteRef
5752          --  set the route state to "transitioning" with the previous successful route
5853          onTransition $ Transitioning  previousRoute route
@@ -73,6 +68,9 @@ makeRouter interface { parse, print, onRoute, onTransition } =
7368                        Continue  ->  finalise route
7469                      mempty
7570          Ref .write (Just  fiber) fiberRef
71+       --  run on the current path
72+       { path } <-  interface.locationState
73+       traverse_ runRouter (parse path)
7674      component <- 
7775        React .component " Wire.Router" ->  React .do 
7876          React .useEffectOnce (onPushState runRouter)
0 commit comments