Skip to content

Commit a0766e3

Browse files
committed
Vanilla promise
Reverting to a direct API implementation for promises.
1 parent cf2be69 commit a0766e3

File tree

5 files changed

+86
-245
lines changed

5 files changed

+86
-245
lines changed

MODULE.md

100755100644
Lines changed: 9 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -682,36 +682,23 @@
682682
data Promise :: * -> * -> *
683683

684684

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)
694-
695-
instance functorPromise :: Functor (Promise a)
696-
697-
instance monadPromise :: Monad (Promise a)
698-
699-
700685
### Values
701686

702-
catch' :: forall a b c d. (a -> Promise c d) -> Promise a b -> Promise c d
687+
catch :: forall a b c d. (a -> Promise c d) -> Promise a b -> Promise c d
703688

704-
finally' :: forall e r a b. Eff e r -> Promise a b -> Promise a b
689+
finally :: forall e r a b. Eff e r -> Promise a b -> Promise a b
705690

706-
pureReject :: forall a b. a -> Promise a b
691+
then1 :: forall a b c. (b -> Promise a c) -> Promise a b -> Promise a c
707692

708-
pureResolve :: forall a b. b -> Promise a b
693+
then1' :: forall a b c. (b -> c) -> Promise a b -> Promise a c
709694

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

712-
then'' :: forall a b c d. (b -> Promise c d) -> (a -> Promise c d) -> Promise a b -> Promise c d
697+
then2' :: forall a b c d. (b -> d) -> (a -> c) -> Promise a b -> Promise c d
713698

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
699+
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
700+
701+
then3' :: forall e s t a b c d. (b -> d) -> (a -> c) -> (s -> Eff e t) -> Promise a b -> Promise c d
715702

716703

717704
## Module Angular.Q
@@ -1025,42 +1012,4 @@
10251012
writeRequestData :: forall a. RequestData a -> ForeignRequestData
10261013

10271014

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-
10661015

bower.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
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",

src/Angular/Http.purs

Lines changed: 4 additions & 5 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,7 +29,7 @@ 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(), then2')
3433

3534
foreign import data Http :: *
3635

@@ -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 = (then2' foreignResponse 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 $ then2' foreignResponse 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 $ then2' foreignResponse 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/Promise.purs

Lines changed: 73 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -1,102 +1,81 @@
11
module Angular.Promise
2-
( Promise(..)
3-
, then'
4-
, then''
5-
, then'''
6-
, catch'
7-
, finally'
8-
, pureResolve
9-
, pureReject
2+
( Promise()
3+
, then1
4+
, then1'
5+
, then2
6+
, then2'
7+
, then3
8+
, then3'
9+
, catch
10+
, finally
1011
) where
1112

1213
import Control.Monad.Eff
13-
import Data.Bifunctor
1414
import Data.Function
1515

1616
foreign import data Promise :: * -> * -> *
1717

18-
instance functorPromise :: Functor (Promise a) where
19-
(<$>) = liftA1
20-
21-
instance applyPromise :: Apply (Promise a) where
22-
(<*>) = ap
23-
24-
instance applicativePromise :: Applicative (Promise a) where
25-
pure = pureResolve
26-
27-
instance bindPromise :: Bind (Promise a) where
28-
(>>=) = flip then'
29-
30-
instance monadPromise :: Monad (Promise a)
31-
32-
instance bifunctorPromise :: Bifunctor Promise where
33-
bimap f g = then'' (pureResolve <<< g) (pureReject <<< f)
34-
35-
foreign import pureResolve
36-
" function pureResolve(a){ \
37-
\ var $q = angular.injector(['ng']).get('$q') \
38-
\ , dfd = $q.defer() \
39-
\ ; \
40-
\ dfd.resolve(a); \
41-
\ return dfd.promise; \
42-
\ } "
43-
:: forall a b. b -> Promise a b
44-
45-
foreign import pureReject
46-
" function pureReject(a){ \
47-
\ var $q = angular.injector(['ng']).get('$q') \
48-
\ , dfd = $q.defer() \
49-
\ ; \
50-
\ dfd.reject(a); \
51-
\ return dfd.promise; \
52-
\ } "
53-
:: forall a b. a -> Promise a b
54-
55-
foreign import thenFn'
56-
" function thenFn$prime(f, fa){ \
57-
\ return fa['then'](f); \
58-
\ } "
59-
:: forall a b c d. Fn2 (b -> Promise c d) (Promise a b) (Promise c d)
60-
61-
then' :: forall a b c. (b -> Promise a c) -> Promise a b -> Promise a c
62-
then' = runFn2 thenFn'
63-
64-
foreign import thenFn''
65-
" function thenFn$prime$prime(f, g, fa){ \
66-
\ return fa['then'](f, g); \
67-
\ } "
68-
:: forall a b c d. Fn3 (b -> Promise c d) (a -> Promise c d) (Promise a b) (Promise c d)
69-
70-
then'' :: forall a b c d. (b -> Promise c d) -> (a -> Promise c d) -> Promise a b -> Promise c d
71-
then'' = runFn3 thenFn''
72-
73-
foreign import thenFn'''
74-
" function thenFn$prime$prime$prime(f, g, h, fa){ \
75-
\ return fa['then'](f, g, function(a){return h(a)();}); \
76-
\ } "
77-
:: forall e s t a b c d. Fn4 (b -> Promise c d)
78-
(a -> Promise c d)
79-
(s -> Eff e t)
80-
(Promise a b)
81-
(Promise c d)
82-
83-
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
84-
then''' = runFn4 thenFn'''
85-
86-
foreign import catchFn'
87-
" function catchFn$prime(f, fa){ \
88-
\ return fa['catch'](f); \
89-
\ } "
90-
:: forall a b c d. Fn2 (a -> Promise c d) (Promise a b) (Promise c d)
91-
92-
catch' :: forall a b c d. (a -> Promise c d) -> Promise a b -> Promise c d
93-
catch' = runFn2 catchFn'
94-
95-
foreign import finallyFn'
96-
" function finallyFn$prime(f, fa){ \
97-
\ return fa['finally'](f); \
98-
\ } "
99-
:: forall e r a b. Fn2 (Eff e r) (Promise a b) (Promise a b)
100-
101-
finally' :: forall e r a b. Eff e r -> Promise a b -> Promise a b
102-
finally' = runFn2 finallyFn'
18+
foreign import then1Fn
19+
"""
20+
function then1Fn(f, fa){
21+
return fa['then'](f);
22+
}
23+
""" :: forall a b c d. Fn2 (b -> c) (Promise a b) (Promise a d)
24+
25+
then1 :: forall a b c. (b -> Promise a c) -> Promise a b -> Promise a c
26+
then1 = runFn2 then1Fn
27+
28+
then1' :: forall a b c. (b -> c) -> Promise a b -> Promise a c
29+
then1' = runFn2 then1Fn
30+
31+
foreign import then2Fn
32+
"""
33+
function then2Fn(f, g, fa){
34+
return fa['then'](f, g);
35+
}
36+
""" :: forall s t a b c d. Fn3 (b -> d) (a -> c) (Promise a b) (Promise s t)
37+
38+
then2 :: forall a b c d. (b -> Promise c d) -> (a -> Promise c d) -> Promise a b -> Promise c d
39+
then2 = runFn3 then2Fn
40+
41+
then2' :: forall a b c d. (b -> d) -> (a -> c) -> Promise a b -> Promise c d
42+
then2' = runFn3 then2Fn
43+
44+
foreign import then3Fn
45+
"""
46+
function then3Fn(f, g, h, fa){
47+
return fa['then'](f, g, function(a){
48+
return h(a)();
49+
});
50+
}
51+
""" :: forall e q r s t a b c d. Fn4 (b -> d)
52+
(a -> c)
53+
(s -> Eff e t)
54+
(Promise a b)
55+
(Promise q r)
56+
57+
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
58+
then3 = runFn4 then3Fn
59+
60+
then3' :: forall e s t a b c d. (b -> d) -> (a -> c) -> (s -> Eff e t) -> Promise a b -> Promise c d
61+
then3' = runFn4 then3Fn
62+
63+
foreign import catchFn
64+
"""
65+
function catchFn(f, fa){ \
66+
return fa['catch'](f); \
67+
}
68+
""" :: forall a b c d. Fn2 (a -> Promise c d) (Promise a b) (Promise c d)
69+
70+
catch :: forall a b c d. (a -> Promise c d) -> Promise a b -> Promise c d
71+
catch = runFn2 catchFn
72+
73+
foreign import finallyFn
74+
"""
75+
function finallyFn(f, fa){
76+
return fa['finally'](f);
77+
}
78+
""" :: forall e r a b. Fn2 (Eff e r) (Promise a b) (Promise a b)
79+
80+
finally :: forall e r a b. Eff e r -> Promise a b -> Promise a b
81+
finally = runFn2 finallyFn

0 commit comments

Comments
 (0)