Skip to content
This repository was archived by the owner on Aug 4, 2023. It is now read-only.

Commit be724ec

Browse files
committed
Initial commit
0 parents  commit be724ec

File tree

10 files changed

+224
-0
lines changed

10 files changed

+224
-0
lines changed

.gitignore

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/bower_components/
2+
/node_modules/
3+
/.pulp-cache/
4+
/output/
5+
/generated-docs/
6+
/.psc-package/
7+
/.psc*
8+
/.purs*
9+
/.psa*

LICENSE

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2020 Awake Security
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# purescript-web-promise

bower.json

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
{
2+
"name": "purescript-web-encoding",
3+
"homepage": "https://github.com/purescript-web/purescript-web-promise",
4+
"license": "MIT",
5+
"repository": {
6+
"type": "git",
7+
"url": "git://github.com/purescript-web/purescript-web-promise.git"
8+
},
9+
"ignore": [
10+
"**/.*",
11+
"bower_components",
12+
"node_modules",
13+
"output",
14+
"bower.json",
15+
"package.json"
16+
],
17+
"ignore": [
18+
"**/.*",
19+
"node_modules",
20+
"bower_components",
21+
"output"
22+
],
23+
"dependencies": {
24+
"purescript-prelude": "^4.0.0",
25+
"purescript-effect": "^2.0.0",
26+
"purescript-maybe": "^4.0.0",
27+
"purescript-functions": "^4.0.0",
28+
"purescript-exceptions": "^4.0.0"
29+
}
30+
}

src/Web/Promise.purs

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
module Web.Promise
2+
( module Web.Promise
3+
, module Web.Promise.Internal
4+
, module Web.Promise.Rejection
5+
) where
6+
7+
import Prelude
8+
9+
import Effect (Effect)
10+
import Effect.Uncurried (mkEffectFn1, mkEffectFn2, runEffectFn1, runEffectFn2)
11+
import Web.Promise.Internal (Promise, reject, resolve)
12+
import Web.Promise.Internal as P
13+
import Web.Promise.Rejection (Rejection)
14+
15+
type Executor a = (a -> Effect Unit) -> (Rejection -> Effect Unit) -> Effect Unit
16+
17+
new :: forall a. Executor a -> Effect (Promise a)
18+
new k = runEffectFn1 P.new $ mkEffectFn2 \onResolve onReject ->
19+
k (runEffectFn1 onResolve) (runEffectFn1 onReject)
20+
21+
then_ :: forall a b. (a -> Effect (Promise b)) -> Promise a -> Effect (Promise b)
22+
then_ k p = runEffectFn2 P.then_ (mkEffectFn1 k) p
23+
24+
catch :: forall a b. (Rejection -> Effect (Promise b)) -> Promise a -> Effect (Promise b)
25+
catch k p = runEffectFn2 P.catch (mkEffectFn1 k) p
26+
27+
finally :: forall a. (Effect (Promise Unit)) -> Promise a -> Effect (Promise a)
28+
finally = runEffectFn2 P.finally
29+
30+
all :: forall a. Array (Promise a) -> Effect (Promise (Array a))
31+
all = runEffectFn1 P.all
32+
33+
race :: forall a. Array (Promise a) -> Effect (Promise a)
34+
race = runEffectFn1 P.race

src/Web/Promise/Internal.js

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
exports.new = function(k) {
2+
return new Promise(k);
3+
};
4+
5+
exports.then_ = function(k, p) {
6+
return p.then(k);
7+
};
8+
9+
exports.catch = function(k, p) {
10+
return p.catch(k);
11+
};
12+
13+
exports.finally = function(k, p) {
14+
return p.finally(k);
15+
};
16+
17+
exports.resolve = Promise.resolve;
18+
exports.reject = Promise.reject;
19+
exports.all = Promise.all;
20+
exports.race = Promise.race;

src/Web/Promise/Internal.purs

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
module Web.Promise.Internal where
2+
3+
import Prelude
4+
5+
import Effect (Effect)
6+
import Effect.Uncurried (EffectFn1, EffectFn2)
7+
import Web.Promise.Rejection (Rejection)
8+
9+
foreign import data Promise :: Type -> Type
10+
11+
foreign import new :: forall a. EffectFn1 (EffectFn2 (EffectFn1 a Unit) (EffectFn1 Rejection Unit) Unit) (Promise a)
12+
13+
foreign import then_ :: forall a b. EffectFn2 (EffectFn1 a (Promise b)) (Promise a) (Promise b)
14+
15+
foreign import catch :: forall a b. EffectFn2 (EffectFn1 Rejection (Promise b)) (Promise a) (Promise b)
16+
17+
foreign import finally :: forall a. EffectFn2 (Effect (Promise Unit)) (Promise a) (Promise a)
18+
19+
foreign import resolve :: forall a. a -> Promise a
20+
21+
foreign import reject :: forall a. Rejection -> Promise a
22+
23+
foreign import all :: forall a. EffectFn1 (Array (Promise a)) (Promise (Array a))
24+
25+
foreign import race :: forall a. EffectFn1 (Array (Promise a)) (Promise a)

src/Web/Promise/Lazy.purs

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
module Web.Promise.Lazy where
2+
3+
import Prelude
4+
5+
import Data.Newtype (class Newtype)
6+
import Data.Traversable (traverse)
7+
import Effect (Effect)
8+
import Effect.Class (class MonadEffect)
9+
import Effect.Uncurried (mkEffectFn1, runEffectFn1, runEffectFn2)
10+
import Web.Promise (Rejection)
11+
import Web.Promise.Internal as P
12+
13+
-- | A pure `Promise` that has not been executed yet. This type can be used
14+
-- | with `do` syntax.
15+
newtype LazyPromise a = LazyPromise (Effect (P.Promise a))
16+
17+
derive instance newtypeLazyPromise :: Newtype (LazyPromise a) _
18+
19+
instance functorLazyPromise :: Functor LazyPromise where
20+
map = liftM1
21+
22+
instance applyLazyPromise :: Apply LazyPromise where
23+
apply = ap
24+
25+
instance applicativeLazyPromise :: Applicative LazyPromise where
26+
pure = LazyPromise <<< pure <<< P.resolve
27+
28+
instance bindLazyPromise :: Bind LazyPromise where
29+
bind (LazyPromise p) k = LazyPromise do
30+
p' <- p
31+
runEffectFn2 P.then_ (mkEffectFn1 \a -> let (LazyPromise b) = k a in b) p'
32+
33+
instance monadLazyPromise :: Monad LazyPromise
34+
35+
instance monadEffectLazyPromise :: MonadEffect LazyPromise where
36+
liftEffect = LazyPromise <<< map P.resolve
37+
38+
catch :: forall a b. (Rejection -> LazyPromise b) -> LazyPromise a -> LazyPromise b
39+
catch k (LazyPromise p) = LazyPromise do
40+
p' <- p
41+
runEffectFn2 P.catch (mkEffectFn1 \a -> let (LazyPromise b) = k a in b) p'
42+
43+
finally :: forall a. LazyPromise Unit -> LazyPromise a -> LazyPromise a
44+
finally (LazyPromise p1) (LazyPromise p2) = LazyPromise do
45+
p2' <- p2
46+
runEffectFn2 P.finally p1 p2'
47+
48+
all :: forall a. Array (LazyPromise a) -> LazyPromise (Array a)
49+
all as = LazyPromise do
50+
as' <- traverse (\(LazyPromise a) -> a) as
51+
runEffectFn1 P.all as'
52+
53+
race :: forall a. Array (LazyPromise a) -> LazyPromise a
54+
race as = LazyPromise do
55+
as' <- traverse (\(LazyPromise a) -> a) as
56+
runEffectFn1 P.race as'

src/Web/Promise/Rejection.js

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
exports.fromError = function(a) {
2+
return a;
3+
};
4+
5+
exports._toError = function(just, nothing, rej) {
6+
if (rej instanceof Error) {
7+
return just(ref);
8+
}
9+
return nothing;
10+
};

src/Web/Promise/Rejection.purs

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
module Web.Promise.Rejection
2+
( Rejection
3+
, fromError
4+
, toError
5+
) where
6+
7+
import Data.Function.Uncurried (Fn3, runFn3)
8+
import Data.Maybe (Maybe(..))
9+
import Effect.Exception (Error)
10+
11+
foreign import data Rejection :: Type
12+
13+
foreign import fromError :: Error -> Rejection
14+
15+
foreign import _toError :: Fn3 (forall a. a -> Maybe a) (forall a. Maybe a) Rejection (Maybe Error)
16+
17+
toError :: Rejection -> Maybe Error
18+
toError = runFn3 _toError Just Nothing

0 commit comments

Comments
 (0)