|
1 | 1 | 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 |
10 | 11 | ) where |
11 | 12 |
|
12 | 13 | import Control.Monad.Eff |
13 | | -import Data.Bifunctor |
14 | 14 | import Data.Function |
15 | 15 |
|
16 | 16 | foreign import data Promise :: * -> * -> * |
17 | 17 |
|
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