@@ -11,78 +11,73 @@ module Routing.Hash
11
11
12
12
import Prelude
13
13
14
- import Control.Monad.Eff (Eff )
15
- import Control.Monad.Eff.Ref (newRef , readRef , writeRef )
16
- import DOM (DOM )
17
- import DOM.Event.EventTarget (addEventListener , eventListener , removeEventListener )
18
- import DOM.HTML (window )
19
- import DOM.HTML.Event.EventTypes (hashchange )
20
- import DOM.HTML.Location as L
21
- import DOM.HTML.Types (windowToEventTarget )
22
- import DOM.HTML.Window (location )
23
14
import Data.Foldable (class Foldable , indexl )
24
15
import Data.Maybe (Maybe (..), fromMaybe , maybe )
25
16
import Data.String (Pattern (..), stripPrefix )
26
- import Routing (RoutingEffects , match , matchWith )
17
+ import Effect (Effect )
18
+ import Effect.Ref as Ref
19
+ import Routing (match , matchWith )
27
20
import Routing.Match (Match )
21
+ import Web.Event.EventTarget (addEventListener , eventListener , removeEventListener )
22
+ import Web.HTML (window )
23
+ import Web.HTML.Event.HashChangeEvent.EventTypes as ET
24
+ import Web.HTML.Location as L
25
+ import Web.HTML.Window as Window
28
26
29
27
-- | Gets the global location hash.
30
- getHash :: forall eff . Eff ( dom :: DOM | eff ) String
31
- getHash = window >>= location >>= L .hash >>> map (stripPrefix (Pattern " #" ) >>> fromMaybe " " )
28
+ getHash :: Effect String
29
+ getHash = window >>= Window . location >>= L .hash >>> map (stripPrefix (Pattern " #" ) >>> fromMaybe " " )
32
30
33
31
-- | Sets the global location hash.
34
- setHash :: forall eff . String -> Eff ( dom :: DOM | eff ) Unit
35
- setHash h = window >>= location >>= L .setHash h
32
+ setHash :: String -> Effect Unit
33
+ setHash h = window >>= Window . location >>= L .setHash h
36
34
37
35
-- | Modifies the global location hash.
38
- modifyHash :: forall eff . (String -> String ) -> Eff ( dom :: DOM | eff ) Unit
36
+ modifyHash :: (String -> String ) -> Effect Unit
39
37
modifyHash fn = (fn <$> getHash) >>= setHash
40
38
41
39
-- | Folds effectfully over hash changes given a callback and an initial hash.
42
40
-- | The provided String is the hash portion of the `Location` with the '#'
43
41
-- | prefix stripped. Returns an effect which will remove the listener.
44
42
foldHashes
45
- :: forall eff a
46
- . (a -> String -> Eff ( RoutingEffects eff ) a )
47
- -> (String -> Eff ( RoutingEffects eff ) a )
48
- -> Eff ( RoutingEffects eff ) ( Eff ( RoutingEffects eff ) Unit )
43
+ :: forall a
44
+ . (a -> String -> Effect a )
45
+ -> (String -> Effect a )
46
+ -> Effect ( Effect Unit )
49
47
foldHashes cb init = do
50
- ref <- newRef =<< init =<< getHash
51
- win <- windowToEventTarget <$> window
52
- let listener = eventListener \_ -> writeRef ref =<< join (cb <$> readRef ref <*> getHash)
53
- addEventListener hashchange listener false win
54
- pure $ removeEventListener hashchange listener false win
48
+ ref <- Ref .new =<< init =<< getHash
49
+ win <- Window .toEventTarget <$> window
50
+ listener <- eventListener \_ -> flip Ref .write ref =<< join (cb <$> Ref .read ref <*> getHash)
51
+ addEventListener ET . hashchange listener false win
52
+ pure $ removeEventListener ET . hashchange listener false win
55
53
56
54
-- | Runs the callback on every hash change providing the previous hash and the
57
55
-- | latest hash. The provided String is the hash portion of the `Location` with
58
56
-- | the '#' prefix stripped. Returns an effect which will remove the listener.
59
- hashes
60
- :: forall eff
61
- . (Maybe String -> String -> Eff (RoutingEffects eff ) Unit )
62
- -> Eff (RoutingEffects eff ) (Eff (RoutingEffects eff ) Unit )
57
+ hashes :: (Maybe String -> String -> Effect Unit ) -> Effect (Effect Unit )
63
58
hashes = matchesWith Just
64
59
65
60
-- | Runs the callback on every hash change using a given `Match` parser to
66
61
-- | extract a route from the hash. If a hash fails to parse, it is ignored.
67
62
-- | To avoid dropping hashes, provide a fallback alternative in your parser.
68
63
-- | Returns an effect which will remove the listener.
69
64
matches
70
- :: forall eff a
65
+ :: forall a
71
66
. Match a
72
- -> (Maybe a -> a -> Eff ( RoutingEffects eff ) Unit )
73
- -> Eff ( RoutingEffects eff ) ( Eff ( RoutingEffects eff ) Unit )
67
+ -> (Maybe a -> a -> Effect Unit )
68
+ -> Effect ( Effect Unit )
74
69
matches = matchesWith <<< match
75
70
76
71
-- | Runs the callback on every hash change using a given custom parser to
77
72
-- | extract a route from the hash. If a hash fails to parse, it is ignored.
78
73
-- | To avoid dropping hashes, provide a fallback alternative in your parser.
79
74
-- | Returns an effect which will remove the listener.
80
75
matchesWith
81
- :: forall eff f a
76
+ :: forall f a
82
77
. Foldable f
83
78
=> (String -> f a )
84
- -> (Maybe a -> a -> Eff ( RoutingEffects eff ) Unit )
85
- -> Eff ( RoutingEffects eff ) ( Eff ( RoutingEffects eff ) Unit )
79
+ -> (Maybe a -> a -> Effect Unit )
80
+ -> Effect ( Effect Unit )
86
81
matchesWith parser cb = foldHashes go (go Nothing )
87
82
where
88
83
go a =
0 commit comments