Skip to content

Commit 570d474

Browse files
authored
Fix logic for initial routing (#4)
1 parent 847cf6e commit 570d474

File tree

1 file changed

+6
-8
lines changed

1 file changed

+6
-8
lines changed

src/Router.purs

+6-8
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ module Wire.React.Router
55

66
import Prelude
77
import Control.Monad.Free.Trans (runFreeT)
8-
import Data.Foldable (class Foldable, for_)
8+
import Data.Foldable (class Foldable, traverse_)
99
import Data.Maybe (Maybe(..))
1010
import Effect (Effect)
1111
import 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") fiber)
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

Comments
 (0)