Skip to content

Commit c11efec

Browse files
authored
Merge pull request #59 from slamdata/compiler/0.12
Compiler/0.12
2 parents 9436bbd + ced7965 commit c11efec

12 files changed

+296
-351
lines changed

GUIDE.md

+3-4
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,13 @@ If you can only construct a URL from your route, then it's impossible to
4747
construct an invalid URL.
4848

4949
To turn a stringy path into our data type, we need to define a parser using
50-
combinators in `Routing.Match.Class` as well as standard `Applicative` and
50+
combinators in `Routing.Match` as well as standard `Applicative` and
5151
`Alternative` combinators.
5252

5353
```purescript
5454
import Prelude
5555
import Control.Alternative ((<|>))
56-
import Routing.Match (Match)
57-
import Routing.Match.Class (lit, int, str, end)
56+
import Routing.Match (Match, lit, int, str, end)
5857
```
5958

6059
The available `Match` combinators are:
@@ -245,7 +244,7 @@ import Routing.Hash (matches)
245244
import MyRoute (myRoute)
246245
```
247246

248-
The `matches` combinator takes a `Match` parser and an `Eff` callback,
247+
The `matches` combinator takes a `Match` parser and an `Effect` callback,
249248
providing the previously matched route (wrapped in `Maybe` since it may be
250249
the initial route) and the currently matched route. You might use this
251250
callback to push an input to an instance of a running application.

bower.json

+19-20
Original file line numberDiff line numberDiff line change
@@ -24,27 +24,26 @@
2424
"package.json"
2525
],
2626
"dependencies": {
27-
"purescript-dom": "^4.15.0",
28-
"purescript-eff": "^3.1.0",
29-
"purescript-either": "^3.0.0",
30-
"purescript-globals": "^3.0.0",
31-
"purescript-lists": "^4.0.1",
32-
"purescript-maps": "^3.0.0",
33-
"purescript-maybe": "^3.0.0",
34-
"purescript-prelude": "^3.0.0",
35-
"purescript-semirings": "^4.0.0",
36-
"purescript-tuples": "^4.0.0",
37-
"purescript-validation": "^3.0.0",
38-
"purescript-aff": "^4.0.0",
39-
"purescript-control": "^3.0.0",
40-
"purescript-console": "^3.0.0",
41-
"purescript-integers": "^3.0.0",
42-
"purescript-foldable-traversable": "^3.7.1"
27+
"purescript-web-html": "^1.0.0",
28+
"purescript-effect": "^2.0.0",
29+
"purescript-either": "^4.0.0",
30+
"purescript-globals": "^4.0.0",
31+
"purescript-lists": "^5.0.0",
32+
"purescript-maybe": "^4.0.0",
33+
"purescript-prelude": "^4.0.0",
34+
"purescript-semirings": "^5.0.0",
35+
"purescript-tuples": "^5.0.0",
36+
"purescript-validation": "^4.0.0",
37+
"purescript-aff": "^5.0.0",
38+
"purescript-control": "^4.0.0",
39+
"purescript-console": "^4.1.0",
40+
"purescript-integers": "^4.0.0",
41+
"purescript-foldable-traversable": "^4.0.0"
4342
},
4443
"devDependencies": {
45-
"purescript-console": "^3.0.0",
46-
"purescript-assert": "^3.1.0",
47-
"purescript-record": "^0.2.6",
48-
"purescript-generics-rep": "^5.4.0"
44+
"purescript-console": "^4.1.0",
45+
"purescript-assert": "^4.0.0",
46+
"purescript-record": "^1.0.0",
47+
"purescript-generics-rep": "^6.0.0"
4948
}
5049
}

package.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99
},
1010
"devDependencies": {
1111
"node-static": "^0.7.10",
12-
"pulp": "^11.0.0",
13-
"purescript": "^0.11.7",
14-
"purescript-psa": "^0.5.0",
12+
"pulp": "^12.0.0",
13+
"purescript": "^0.12.0",
14+
"purescript-psa": "^0.6.0",
1515
"rimraf": "^2.5.4"
1616
}
1717
}

src/Routing.purs

+3-12
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,18 @@
11
module Routing
2-
( RoutingEffects
3-
, match
2+
( match
43
, matchWith
54
) where
65

76
import Prelude
87

9-
import Control.Monad.Eff.Ref (REF)
108
import Data.Either (Either)
11-
import DOM (DOM)
12-
import Global (decodeURIComponent)
9+
import Global.Unsafe (unsafeDecodeURIComponent)
1310
import Routing.Match (Match, runMatch)
1411
import Routing.Parser (parse)
1512

16-
type RoutingEffects eff =
17-
( dom :: DOM
18-
, ref :: REF
19-
| eff
20-
)
21-
2213
-- | Runs a `Match` parser.
2314
match :: forall a. Match a -> String -> Either String a
24-
match = matchWith decodeURIComponent
15+
match = matchWith unsafeDecodeURIComponent
2516

2617
-- | Runs a `Match` parser given a custom String decoder.
2718
matchWith :: forall a. (String -> String) -> Match a -> String -> Either String a

src/Routing/Hash.purs

+29-34
Original file line numberDiff line numberDiff line change
@@ -11,78 +11,73 @@ module Routing.Hash
1111

1212
import Prelude
1313

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)
2314
import Data.Foldable (class Foldable, indexl)
2415
import Data.Maybe (Maybe(..), fromMaybe, maybe)
2516
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)
2720
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
2826

2927
-- | 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 "")
3230

3331
-- | 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
3634

3735
-- | Modifies the global location hash.
38-
modifyHash :: forall eff. (String -> String) -> Eff (dom :: DOM | eff) Unit
36+
modifyHash :: (String -> String) -> Effect Unit
3937
modifyHash fn = (fn <$> getHash) >>= setHash
4038

4139
-- | Folds effectfully over hash changes given a callback and an initial hash.
4240
-- | The provided String is the hash portion of the `Location` with the '#'
4341
-- | prefix stripped. Returns an effect which will remove the listener.
4442
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)
4947
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
5553

5654
-- | Runs the callback on every hash change providing the previous hash and the
5755
-- | latest hash. The provided String is the hash portion of the `Location` with
5856
-- | 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)
6358
hashes = matchesWith Just
6459

6560
-- | Runs the callback on every hash change using a given `Match` parser to
6661
-- | extract a route from the hash. If a hash fails to parse, it is ignored.
6762
-- | To avoid dropping hashes, provide a fallback alternative in your parser.
6863
-- | Returns an effect which will remove the listener.
6964
matches
70-
:: forall eff a
65+
:: forall a
7166
. 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)
7469
matches = matchesWith <<< match
7570

7671
-- | Runs the callback on every hash change using a given custom parser to
7772
-- | extract a route from the hash. If a hash fails to parse, it is ignored.
7873
-- | To avoid dropping hashes, provide a fallback alternative in your parser.
7974
-- | Returns an effect which will remove the listener.
8075
matchesWith
81-
:: forall eff f a
76+
:: forall f a
8277
. Foldable f
8378
=> (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)
8681
matchesWith parser cb = foldHashes go (go Nothing)
8782
where
8883
go a =

0 commit comments

Comments
 (0)