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