Skip to content

Commit b9af379

Browse files
committed
Implementing PromiseEff
This specialization of Promise is meant to address issue #12. Consider the following example. import Debug.Trace import Angular.Promise import Angular.Promise.Eff import qualified Angular.Http as H foo :: forall e f a b. a -> PromiseEff e (trace :: Trace | f) b Unit foo _ = liftPromiseEff' $ do trace "Hello world 2." trace "Hello world 3." bar :: forall e f a b. a -> PromiseEff e (trace :: Trace | f) b Unit bar _ = liftPromiseEff' $ do trace "Hello world 4." trace "Hello world 5." controller http = do promise <- H.get "http://localhost:9501/examples/Todomvc/main.html" http return $ unsafeRunPromiseEff $ promiseEff promise >>= foo >>= bar This results in the trace statements being run once the promise is resolved.
1 parent b0d6373 commit b9af379

File tree

3 files changed

+89
-2
lines changed

3 files changed

+89
-2
lines changed

bower.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
"purescript-tuples": "0.2.0"
2626
},
2727
"devDependencies": {
28-
"angular": "1.2.22",
28+
"angular": "1.2.23",
2929
"todomvc-common": "0.1.9",
3030
"purescript-strings": "0.2.1",
3131
"purescript-arrays": "0.2.0",

src/Angular/Promise.purs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
module Angular.Promise
2-
( Promise()
2+
( Promise(..)
33
, then'
44
, then''
55
, then'''
66
, catch'
77
, finally'
8+
, pureResolve
9+
, pureReject
810
) where
911

1012
import Control.Monad.Eff

src/Angular/Promise/Eff.purs

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
module Angular.Promise.Eff
2+
( PromiseEff(..)
3+
, runPromiseEff
4+
, unsafeRunPromiseEff
5+
, promiseEff
6+
, promiseEff'
7+
, promiseEff''
8+
, liftPromiseEff
9+
, liftPromiseEff'
10+
) where
11+
12+
import Control.Monad.Eff
13+
import Data.Bifunctor
14+
import Data.Function
15+
16+
import Angular.Promise
17+
18+
newtype PromiseEff e f a b = PromiseEff (Promise (Eff e a) (Eff f b))
19+
20+
runPromiseEff :: forall e f a b. PromiseEff e f a b -> Promise (Eff e a) (Eff f b)
21+
runPromiseEff (PromiseEff fa) = fa
22+
23+
unsafeRunPromiseEff :: forall e f a b. PromiseEff e f a b -> Promise a b
24+
unsafeRunPromiseEff (PromiseEff fa) = unsafeRunPromiseEff' fa
25+
26+
instance functorPromiseEff :: Functor (PromiseEff e f a) where
27+
(<$>) k (PromiseEff fa) = PromiseEff $ (\eff -> k <$> eff) <$> fa
28+
29+
instance applyPromise :: Apply (PromiseEff e f a) where
30+
(<*>) (PromiseEff fk) (PromiseEff fa) = PromiseEff $ do
31+
k <- fk
32+
a <- fa
33+
return $ k `ap` a
34+
35+
instance applicativePromiseEff :: Applicative (PromiseEff e f a) where
36+
pure = PromiseEff <<< pure <<< pure
37+
38+
instance bindPromiseEff :: Bind (PromiseEff e f a) where
39+
(>>=) = flip $ runFn2 thenEffFn
40+
41+
instance bifunctorPromise :: Bifunctor (PromiseEff e f) where
42+
bimap f g = runFn3 thenEffFn' (PromiseEff <<< pureResolve <<< pure <<< g)
43+
(PromiseEff <<< pureReject <<< pure <<< f)
44+
45+
promiseEff :: forall e f a b. Promise a b -> PromiseEff e f a b
46+
promiseEff = PromiseEff <<< then'' (pureResolve <<< returnE) (pureReject <<< returnE)
47+
48+
promiseEff' :: forall e f a b. Promise a (Eff f b) -> PromiseEff e f a b
49+
promiseEff' = PromiseEff <<< then'' (pureResolve <<< id) (pureReject <<< returnE)
50+
51+
promiseEff'' :: forall e f a b. Promise (Eff e a) b -> PromiseEff e f a b
52+
promiseEff'' = PromiseEff <<< then'' (pureResolve <<< returnE) (pureReject <<< id)
53+
54+
liftPromiseEff :: forall e f a b. Eff e a -> Eff f b -> PromiseEff e f a b
55+
liftPromiseEff e f = PromiseEff $ then'' (pureResolve <<< id) (\_ -> pureReject e) (pureResolve f)
56+
57+
liftPromiseEff' :: forall e f a b. Eff f b -> PromiseEff e f a b
58+
liftPromiseEff' = promiseEff' <<< return
59+
60+
foreign import thenEffFn
61+
" function thenEffFn(k, fa){ \
62+
\ return fa.then(function(eff){ \
63+
\ return k(eff()); \
64+
\ }); \
65+
\ } "
66+
:: forall e f a b c. Fn2 (b -> PromiseEff e f a c)
67+
(PromiseEff e f a b)
68+
(PromiseEff e f a c)
69+
70+
foreign import thenEffFn'
71+
" function thenEffFn$prime(fa, k, i){ \
72+
\ return fa.then(function(eff){return k(eff());}, \
73+
\ function(eff){return i(eff());}); \
74+
\ } "
75+
:: forall e f a b c d. Fn3 (b -> PromiseEff e f c d)
76+
(a -> PromiseEff e f c d)
77+
(PromiseEff e f a b)
78+
(PromiseEff e f c d)
79+
80+
foreign import unsafeRunPromiseEff'
81+
" function unsafeRunPromiseEff$prime(p) { \
82+
\ return p.then(function(eff){return eff();}, \
83+
\ function(eff){return eff();}); \
84+
\ } "
85+
:: forall e f a b. Promise (Eff e a) (Eff f b) -> Promise a b

0 commit comments

Comments
 (0)