@@ -16,29 +16,24 @@ import React.Basic.Hooks (JSX)
16
16
import React.Basic.Hooks as React
17
17
import Routing.PushState (PushStateInterface )
18
18
import Routing.PushState as PushState
19
- import Wire.React.Router.Control (Command (..), Resolved , Route (..), Router (..), Transitioning )
20
- import Wire.React.Router.Control (Command , Resolved , Route (..), Router , Transitioning , _Resolved , _Route , _Transitioning , continue , isResolved , isTransitioning , override , redirect ) as Control
21
- import Wire.Signal (Signal )
22
- import Wire.Signal as Signal
23
-
24
- type Interface route
25
- = { signal :: Signal (Route route )
26
- , component :: JSX
27
- , navigate :: route -> Effect Unit
28
- , redirect :: route -> Effect Unit
29
- }
19
+ import Wire.React.Router.Control (Command (..), Resolved , Router (..), Transition (..), Transitioning )
20
+ import Wire.React.Router.Control (Command , Resolved , Router , Transition (..), Transitioning , _Resolved , _Transition , _Transitioning , continue , isResolved , isTransitioning , override , redirect ) as Control
30
21
31
22
makeRouter ::
32
23
forall f route .
33
24
Foldable f =>
34
25
PushStateInterface ->
35
- { fallback :: route
36
- , parse :: String -> f route
26
+ { parse :: String -> f route
37
27
, print :: route -> String
38
28
, onRoute :: route -> Router route Transitioning Resolved Unit
29
+ , onTransition :: Transition route -> Effect Unit
39
30
} ->
40
- Effect (Interface route )
41
- makeRouter interface { fallback, parse, print, onRoute } =
31
+ Effect
32
+ { component :: JSX
33
+ , navigate :: route -> Effect Unit
34
+ , redirect :: route -> Effect Unit
35
+ }
36
+ makeRouter interface { parse, print, onRoute, onTransition } =
42
37
let
43
38
onPushState k = PushState .matchesWith parse (\_ -> k) interface
44
39
@@ -47,11 +42,9 @@ makeRouter interface { fallback, parse, print, onRoute } =
47
42
redirect route = interface.replaceState (unsafeToForeign {}) (print route)
48
43
in
49
44
do
50
- { modify, signal } <- Signal .create (Transitioning Nothing fallback)
51
- do
52
- -- replace the user-supplied fallback route with the current route, if possible
53
- { path } <- interface.locationState
54
- for_ (parse path) \route -> modify \_ -> Transitioning Nothing route
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
55
48
fiberRef <- Ref .new Nothing
56
49
previousRouteRef <- Ref .new Nothing
57
50
let
@@ -62,12 +55,12 @@ makeRouter interface { fallback, parse, print, onRoute } =
62
55
for_ oldFiber \fiber -> launchAff_ (killFiber (error " Transition cancelled" ) fiber)
63
56
previousRoute <- Ref .read previousRouteRef
64
57
-- set the route state to "transitioning" with the previous successful route
65
- modify \_ -> Transitioning previousRoute route
58
+ onTransition $ Transitioning previousRoute route
66
59
let
67
60
finalise r =
68
61
liftEffect do
69
62
Ref .write (Just r) previousRouteRef
70
- modify \_ -> Resolved previousRoute r
63
+ onTransition $ Resolved previousRoute r
71
64
fiber <-
72
65
launchAff case onRoute route of
73
66
Router router ->
@@ -84,4 +77,4 @@ makeRouter interface { fallback, parse, print, onRoute } =
84
77
React .component " Wire.Router" \_ -> React .do
85
78
React .useEffectOnce (onPushState runRouter)
86
79
pure React .empty
87
- pure { signal, component: component unit, navigate, redirect }
80
+ pure { component: component unit, navigate, redirect }
0 commit comments