Skip to content

Commit 0cc3df3

Browse files
committed
Merge pull request #23 from purescript-contrib/topic/promise-refactoring
Topic/promise refactoring
2 parents cf2be69 + 733218f commit 0cc3df3

File tree

9 files changed

+238
-253
lines changed

9 files changed

+238
-253
lines changed

MODULE.md

100755100644
Lines changed: 18 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -381,7 +381,7 @@
381381

382382
data Http :: *
383383

384-
type HttpResponse e r a b c d = HttpEff e (Promise (Response r a b c d) (Response r a b c d))
384+
type HttpResponse e r a b c d = HttpEff e (Promise I.ForeignResponse (Response r a b c d))
385385

386386
type Response r a b c d = { statusText :: String, config :: Config a b c d, headers :: [String] -> String, status :: Status, "data" :: D.HttpData r }
387387

@@ -681,37 +681,30 @@
681681

682682
data Promise :: * -> * -> *
683683

684+
type PromiseEC e a b = ErrorT a (ContT Unit (Eff e)) b
684685

685-
### Type Class Instances
686-
687-
instance applicativePromise :: Applicative (Promise a)
688-
689-
instance applyPromise :: Apply (Promise a)
690-
691-
instance bifunctorPromise :: Bifunctor Promise
692-
693-
instance bindPromise :: Bind (Promise a)
694686

695-
instance functorPromise :: Functor (Promise a)
687+
### Values
696688

697-
instance monadPromise :: Monad (Promise a)
689+
catch :: forall a b c d. (a -> Promise c d) -> Promise a b -> Promise c d
698690

691+
finally :: forall e r a b. Eff e r -> Promise a b -> Promise a b
699692

700-
### Values
693+
liftPromiseEC :: forall e a b. (Error a) => Eff e (Promise a b) -> PromiseEC e a b
701694

702-
catch' :: forall a b c d. (a -> Promise c d) -> Promise a b -> Promise c d
695+
runPromiseEC :: forall e a b. PromiseEC e a b -> (Either a b -> Eff e Unit) -> Eff e Unit
703696

704-
finally' :: forall e r a b. Eff e r -> Promise a b -> Promise a b
697+
then1 :: forall a b c. (b -> Promise a c) -> Promise a b -> Promise a c
705698

706-
pureReject :: forall a b. a -> Promise a b
699+
then1' :: forall a b c. (b -> c) -> Promise a b -> Promise a c
707700

708-
pureResolve :: forall a b. b -> Promise a b
701+
then2 :: forall a b c d. (b -> Promise c d) -> (a -> Promise c d) -> Promise a b -> Promise c d
709702

710-
then' :: forall a b c. (b -> Promise a c) -> Promise a b -> Promise a c
703+
then2' :: forall a b c d. (b -> d) -> (a -> c) -> Promise a b -> Promise c d
711704

712-
then'' :: forall a b c d. (b -> Promise c d) -> (a -> Promise c d) -> Promise a b -> Promise c d
705+
then3 :: forall e s t a b c d. (b -> Promise c d) -> (a -> Promise c d) -> (s -> Eff e t) -> Promise a b -> Promise c d
713706

714-
then''' :: forall e s t a b c d. (b -> Promise c d) -> (a -> Promise c d) -> (s -> Eff e t) -> Promise a b -> Promise c d
707+
then3' :: forall e s t a b c d. (b -> d) -> (a -> c) -> (s -> Eff e t) -> Promise a b -> Promise c d
715708

716709

717710
## Module Angular.Q
@@ -897,6 +890,11 @@
897890
data ForeignResponse :: *
898891

899892

893+
### Type Class Instances
894+
895+
instance errorForeignResponse :: Error ForeignResponse
896+
897+
900898
### Values
901899

902900
foreignConfig :: forall e. HttpEff e ForeignConfig
@@ -1025,42 +1023,4 @@
10251023
writeRequestData :: forall a. RequestData a -> ForeignRequestData
10261024

10271025

1028-
## Module Angular.Promise.Eff
1029-
1030-
### Types
1031-
1032-
newtype PromiseEff e f a b where
1033-
PromiseEff :: Promise (Eff e a) (Eff f b) -> PromiseEff e f a b
1034-
1035-
1036-
### Type Class Instances
1037-
1038-
instance applicativePromiseEff :: Applicative (PromiseEff e f a)
1039-
1040-
instance applyPromise :: Apply (PromiseEff e f a)
1041-
1042-
instance bifunctorPromise :: Bifunctor (PromiseEff e f)
1043-
1044-
instance bindPromiseEff :: Bind (PromiseEff e f a)
1045-
1046-
instance functorPromiseEff :: Functor (PromiseEff e f a)
1047-
1048-
1049-
### Values
1050-
1051-
liftPromiseEff :: forall e f a b. Eff e a -> Eff f b -> PromiseEff e f a b
1052-
1053-
liftPromiseEff' :: forall e f a b. Eff f b -> PromiseEff e f a b
1054-
1055-
promiseEff :: forall e f a b. Promise a b -> PromiseEff e f a b
1056-
1057-
promiseEff' :: forall e f a b. Promise a (Eff f b) -> PromiseEff e f a b
1058-
1059-
promiseEff'' :: forall e f a b. Promise (Eff e a) b -> PromiseEff e f a b
1060-
1061-
runPromiseEff :: forall e f a b. PromiseEff e f a b -> Promise (Eff e a) (Eff f b)
1062-
1063-
unsafeRunPromiseEff :: forall e f a b. PromiseEff e f a b -> Promise a b
1064-
1065-
10661026

bower.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@
1717
"dist"
1818
],
1919
"dependencies": {
20-
"purescript-bifunctors": "0.0.6",
2120
"purescript-either": "0.1.3",
2221
"purescript-exceptions": "0.2.1",
2322
"purescript-foldable-traversable": "0.1.4",
2423
"purescript-maybe": "0.2.1",
24+
"purescript-transformers": "0.3.0",
2525
"purescript-tuples": "0.2.1",
2626
"purescript-simple-dom": "git://github.com/aktowns/purescript-simple-dom.git"
2727
},

examples/Backend/Main.purs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
module Backend.Main where
2+
3+
import Control.Monad.Eff
4+
import Data.Either (either)
5+
6+
import Angular.Http (Http(), get)
7+
import Angular.Module (controller, ngmodule')
8+
import Angular.Promise (liftPromiseEC, runPromiseEC)
9+
import Angular.This (extendThis)
10+
11+
mainctrl http this = extendThis { text: ""
12+
, submit: submit } this
13+
where
14+
url = "http://localhost:9501/examples/Backend/main.html"
15+
submit a = runPromiseEC (liftPromiseEC $ get url http)
16+
(either (\e -> extendThis { value: "Failed to get" ++ url } this)
17+
(\a -> extendThis { value: a."data" } this))
18+
19+
main = do
20+
m <- ngmodule' "backend" []
21+
controller "Main" mainctrl' m
22+
23+
foreign import mainctrl'
24+
"""
25+
/*@ngInject*/function mainctrl$prime($http) {
26+
var impl = mainctrl($http)(this);
27+
return impl.apply(this, []);
28+
}
29+
""" :: forall e a. Http -> Eff e Unit

examples/Backend/main.html

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<!doctype html>
2+
<html class="no-js"
3+
lang=""
4+
ng-app="backend">
5+
<head>
6+
<meta charset="utf-8">
7+
<meta http-equiv="X-UA-Compatible"
8+
content="IE=edge">
9+
<title>Backend</title>
10+
<meta name="description"
11+
content="">
12+
<meta name="viewport"
13+
content="width=device-width, initial-scale=1">
14+
<link rel="stylesheet"
15+
href="../../bower_components/angular/angular-csp.css">
16+
</head>
17+
<body ng-controller="Main as mainctrl">
18+
<form ng-submit="mainctrl.submit(mainctrl.text)()"
19+
novalidate>
20+
<button type="submit">
21+
Submit
22+
</button>
23+
</form>
24+
<code ng-cloak>
25+
{{mainctrl.value.value0}}
26+
</code>
27+
<script src="../../bower_components/angular/angular.js"></script>
28+
<script src="../../dist/backend.js"></script>
29+
</body>
30+
</html>

gulpfile.js

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,23 @@ var gulp = require('gulp')
1313
'src/**/*.purs'
1414
],
1515
examples: {
16-
todomvc: 'examples/Todomvc/**/*.purs'
16+
todomvc: {
17+
src: 'examples/Todomvc/**/*.purs',
18+
options: {
19+
main: 'Todomvc.Main',
20+
output: 'todomvc.js'
21+
}
22+
},
23+
backend: {
24+
src: 'examples/Backend/**/*.purs',
25+
options: {
26+
main: 'Backend.Main',
27+
output: 'backend.js'
28+
}
29+
}
1730
},
1831
dest: 'dist',
19-
docs: 'MODULE.md',
20-
options: {
21-
main: 'Todomvc.Main',
22-
output: 'todomvc.js'
23-
}
32+
docs: 'MODULE.md'
2433
},
2534
nstatic: {
2635
root: '.',
@@ -55,9 +64,19 @@ gulp.task('clean', function(){
5564

5665
gulp.task('todomvc', ['clean'], function(){
5766
return (
58-
gulp.src([config.purescript.examples.todomvc].concat(config.purescript.src)).
67+
gulp.src([config.purescript.examples.todomvc.src].concat(config.purescript.src)).
5968
pipe(plumber()).
60-
pipe(purescript.psc(config.purescript.options)).
69+
pipe(purescript.psc(config.purescript.examples.todomvc.options)).
70+
on('error', error).
71+
pipe(gulp.dest(config.purescript.dest))
72+
);
73+
});
74+
75+
gulp.task('backend', ['clean'], function(){
76+
return (
77+
gulp.src([config.purescript.examples.backend.src].concat(config.purescript.src)).
78+
pipe(plumber()).
79+
pipe(purescript.psc(config.purescript.examples.backend.options)).
6180
on('error', error).
6281
pipe(gulp.dest(config.purescript.dest))
6382
);
@@ -96,7 +115,12 @@ gulp.task('watch', function(cb){
96115
});
97116

98117
gulp.task('watch.todomvc', function(cb){
99-
gulp.watch([config.purescript.examples.todomvc].concat(config.purescript.src), ['todomvc']);
118+
gulp.watch([config.purescript.examples.todomvc.src].concat(config.purescript.src), ['todomvc']);
119+
server(cb);
120+
});
121+
122+
gulp.task('watch.backend', function(cb){
123+
gulp.watch([config.purescript.examples.backend.src].concat(config.purescript.src), ['backend']);
100124
server(cb);
101125
});
102126

src/Angular/Http.purs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ module Angular.Http
1919
, put'
2020
) where
2121

22-
import Data.Bifunctor
2322
import Data.Either
2423
import Data.Function
2524
import Data.Maybe
@@ -30,11 +29,11 @@ import qualified Data.DOM.Simple.Ajax as D
3029
import Angular.Cache (Cache())
3130
import qualified Angular.Http.Internal as I
3231
import Angular.Http.Types
33-
import Angular.Promise (Promise())
32+
import Angular.Promise (Promise(), then1')
3433

3534
foreign import data Http :: *
3635

37-
type HttpResponse e r a b c d = HttpEff e (Promise (Response r a b c d) (Response r a b c d))
36+
type HttpResponse e r a b c d = HttpEff e (Promise I.ForeignResponse (Response r a b c d))
3837

3938
type ForeignHttpResponse e = HttpEff e (Promise I.ForeignResponse I.ForeignResponse)
4039

@@ -78,7 +77,7 @@ config = { method: D.GET
7877
, responseType: D.Default }
7978

8079
http :: forall e r a b c d. Config a b c d -> Http -> HttpResponse e r a b c d
81-
http c h = (bimap foreignResponse foreignResponse) <$> (foreignConfig c >>= runFn2 httpFn h)
80+
http c h = (then1' foreignResponse) <$> (foreignConfig c >>= runFn2 httpFn h)
8281

8382
get :: forall e r a b c d. D.Url -> Http -> HttpResponse e r a b c d
8483
get u = runHttpFn' D.GET u config
@@ -120,13 +119,13 @@ runHttpFn' :: forall e r a b c d. D.HttpMethod -> D.Url -> Config a b c d -> Htt
120119
runHttpFn' m u c h = do
121120
conf <- foreignConfig c
122121
res <- runFn4 httpFn' (show m) u conf h
123-
return $ bimap foreignResponse foreignResponse res
122+
return $ then1' foreignResponse res
124123

125124
runHttpFn'' :: forall e r a b c d. D.HttpMethod -> D.Url -> RequestData b -> Config a b c d -> Http -> HttpResponse e r a b c d
126125
runHttpFn'' m u d c h = do
127126
conf <- foreignConfig c
128127
res <- runFn5 httpFn'' (show m) u (writeRequestData d) conf h
129-
return $ bimap foreignResponse foreignResponse res
128+
return $ then1' foreignResponse res
130129

131130
foreignConfig :: forall e a b c d. Config a b c d -> HttpEff e I.ForeignConfig
132131
foreignConfig conf = do

src/Angular/Http/Internal.purs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ module Angular.Http.Internal
3232
) where
3333

3434
import Control.Monad.Eff
35+
import Control.Monad.Error
3536
import Data.Either
3637
import Data.Foldable (for_)
3738
import Data.Function
@@ -47,6 +48,18 @@ foreign import data ForeignConfig :: *
4748

4849
foreign import data ForeignResponse :: *
4950

51+
foreign import unimplementedForeignResponse
52+
"""
53+
function unimplementedForeignResponse(){
54+
return {
55+
};
56+
}
57+
""" :: ForeignResponse
58+
59+
instance errorForeignResponse :: Error ForeignResponse where
60+
noMsg = unimplementedForeignResponse
61+
strMsg = \_ -> unimplementedForeignResponse
62+
5063
setConfigMethod :: forall e. D.HttpMethod -> ForeignConfig -> HttpEff e Unit
5164
setConfigMethod m = runFn3 setConfigPropFn "method" (show m)
5265

0 commit comments

Comments
 (0)