Skip to content

Commit 8067404

Browse files
authored
Remove use of Signal to allow the user to hook in their preferred state management (#3)
* Removed Wire as a dependency * Remove the Interface alias
1 parent 1c46847 commit 8067404

File tree

5 files changed

+37
-47
lines changed

5 files changed

+37
-47
lines changed

examples/RoutingDuplex.purs

+12-8
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
{-| This example also requires the following dependencies to be installed:
22
- react-basic-dom
33
- routing-duplex
4+
- wire
45
- wire-react
56
-}
67
module Example.RoutingDuplex where
78

89
import Prelude hiding ((/))
10+
import Data.Foldable (for_)
911
import Data.Generic.Rep (class Generic)
12+
import Data.Lens (preview, review)
1013
import Data.Lens as Lens
1114
import Effect (Effect)
1215
import React.Basic.DOM as R
@@ -18,7 +21,6 @@ import Routing.Duplex.Generic (noArgs, sum)
1821
import Routing.Duplex.Generic.Syntax ((/))
1922
import Routing.PushState as PushState
2023
import Wire.React as Wire
21-
import Wire.React.Router (_Route)
2224
import Wire.React.Router as Router
2325

2426
data Route
@@ -41,27 +43,29 @@ routes =
4143
makeApp :: Effect (Unit -> JSX)
4244
makeApp = do
4345
interface <- PushState.makeInterface
46+
{ signal: routeSignal, modify: modifyRouteSignal } <- Signal.create NotFound
4447
router <-
4548
Router.makeRouter interface
46-
{ fallback:
47-
-- used as the initial route if the parser fails
48-
NotFound
49-
, parse: parse routes
49+
{ parse: parse routes
5050
, print: print routes
5151
, onRoute:
5252
-- this skips any async routing logic by accepting the parsed route immediately
53-
\route -> Router.continue
53+
const Router.continue
54+
, onTransition:
55+
case _ of
56+
Router.Transitioning _ _ -> pure unit
57+
Router.Resolved _ route -> modifyRouteSignal (const route)
5458
}
5559
React.component "App" \props -> React.do
5660
route <-
5761
-- subscribe to the signal containing the current route
58-
Wire.useSignal router.signal
62+
Wire.useSignal routeSignal
5963
pure
6064
$ React.fragment
6165
[ -- the router subscribes to pushstate events when this component is mounted, and unsubscribes when unmounted
6266
router.component
6367
, R.h1_
64-
[ R.text case Lens.view _Route route of
68+
[ R.text case route of
6569
Home -> "Home"
6670
About -> "About"
6771
NotFound -> "Not Found"

packages.dhall

+1-7
Original file line numberDiff line numberDiff line change
@@ -123,12 +123,6 @@ let upstream =
123123

124124
let overrides = {=}
125125

126-
let additions =
127-
{ wire =
128-
{ dependencies = [ "aff", "filterable", "refs", "unsafe-reference" ]
129-
, repo = "https://github.com/robertdp/purescript-wire.git"
130-
, version = "v0.4.2"
131-
}
132-
}
126+
let additions = {=}
133127

134128
in upstream // overrides // additions

spago.dhall

-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ You can edit this file as you like.
1010
, "profunctor-lenses"
1111
, "react-basic-hooks"
1212
, "routing"
13-
, "wire"
1413
]
1514
, packages = ./packages.dhall
1615
, sources = [ "src/**/*.purs", "test/**/*.purs" ]

src/Router.purs

+16-23
Original file line numberDiff line numberDiff line change
@@ -16,29 +16,24 @@ import React.Basic.Hooks (JSX)
1616
import React.Basic.Hooks as React
1717
import Routing.PushState (PushStateInterface)
1818
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
3021

3122
makeRouter ::
3223
forall f route.
3324
Foldable f =>
3425
PushStateInterface ->
35-
{ fallback :: route
36-
, parse :: String -> f route
26+
{ parse :: String -> f route
3727
, print :: route -> String
3828
, onRoute :: route -> Router route Transitioning Resolved Unit
29+
, onTransition :: Transition route -> Effect Unit
3930
} ->
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 } =
4237
let
4338
onPushState k = PushState.matchesWith parse (\_ -> k) interface
4439

@@ -47,11 +42,9 @@ makeRouter interface { fallback, parse, print, onRoute } =
4742
redirect route = interface.replaceState (unsafeToForeign {}) (print route)
4843
in
4944
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
5548
fiberRef <- Ref.new Nothing
5649
previousRouteRef <- Ref.new Nothing
5750
let
@@ -62,12 +55,12 @@ makeRouter interface { fallback, parse, print, onRoute } =
6255
for_ oldFiber \fiber -> launchAff_ (killFiber (error "Transition cancelled") fiber)
6356
previousRoute <- Ref.read previousRouteRef
6457
-- set the route state to "transitioning" with the previous successful route
65-
modify \_ -> Transitioning previousRoute route
58+
onTransition $ Transitioning previousRoute route
6659
let
6760
finalise r =
6861
liftEffect do
6962
Ref.write (Just r) previousRouteRef
70-
modify \_ -> Resolved previousRoute r
63+
onTransition $ Resolved previousRoute r
7164
fiber <-
7265
launchAff case onRoute route of
7366
Router router ->
@@ -84,4 +77,4 @@ makeRouter interface { fallback, parse, print, onRoute } =
8477
React.component "Wire.Router" \_ -> React.do
8578
React.useEffectOnce (onPushState runRouter)
8679
pure React.empty
87-
pure { signal, component: component unit, navigate, redirect }
80+
pure { component: component unit, navigate, redirect }

src/Router/Control.purs

+8-8
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,14 @@ import Effect.Aff.Class (class MonadAff, liftAff)
1111
import Effect.Class (class MonadEffect, liftEffect)
1212
import Type.Equality (class TypeEquals)
1313

14-
data Route route
14+
data Transition route
1515
= Transitioning (Maybe route) route
1616
| Resolved (Maybe route) route
1717

18-
derive instance eqRoute :: Eq route => Eq (Route route)
18+
derive instance eqRoute :: Eq route => Eq (Transition route)
1919

20-
_Route :: forall route. Lens' (Route route) route
21-
_Route = lens getter setter
20+
_Transition :: forall route. Lens' (Transition route) route
21+
_Transition = lens getter setter
2222
where
2323
getter = case _ of
2424
Transitioning _ route -> route
@@ -28,22 +28,22 @@ _Route = lens getter setter
2828
Transitioning route _ -> Transitioning route
2929
Resolved route _ -> Resolved route
3030

31-
_Transitioning :: forall route. Prism' (Route route) route
31+
_Transitioning :: forall route. Prism' (Transition route) route
3232
_Transitioning =
3333
prism' (Transitioning Nothing) case _ of
3434
Transitioning _ route -> Just route
3535
_ -> Nothing
3636

37-
_Resolved :: forall route. Prism' (Route route) route
37+
_Resolved :: forall route. Prism' (Transition route) route
3838
_Resolved =
3939
prism' (Resolved Nothing) case _ of
4040
Resolved _ route -> Just route
4141
_ -> Nothing
4242

43-
isTransitioning :: forall route. Route route -> Boolean
43+
isTransitioning :: forall route. Transition route -> Boolean
4444
isTransitioning = is _Transitioning
4545

46-
isResolved :: forall route. Route route -> Boolean
46+
isResolved :: forall route. Transition route -> Boolean
4747
isResolved = is _Resolved
4848

4949
data Command route a

0 commit comments

Comments
 (0)