Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add bindings to Promise.allSettled #6137

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#### :bug: Bug Fix

- Make "rescript format" work with node 10 again and set minimum required node version to 10 in package.json. https://github.com/rescript-lang/rescript-compiler/pull/6186
- GenType: add support for custom `@tag` in variant type declaration. https://github.com/rescript-lang/rescript-compiler/pull/6137/files

# 11.0.0-alpha.4

Expand Down
7 changes: 7 additions & 0 deletions jscomp/gentype/Annotation.ml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ let tagIsGenTypeAs s = s = "genType.as" || s = "gentype.as"
let tagIsAs s = s = "bs.as" || s = "as"
let tagIsInt s = s = "bs.int" || s = "int"
let tagIsString s = s = "bs.string" || s = "string"
let tagIsTag s = s = "tag"

let tagIsUnboxed s = s = "unboxed" || s = "ocaml.unboxed"
let tagIsGenTypeImport s = s = "genType.import" || s = "gentype.import"
let tagIsGenTypeOpaque s = s = "genType.opaque" || s = "gentype.opaque"
Expand Down Expand Up @@ -123,6 +125,11 @@ let getAsString attributes =
| Some (_, StringPayload s) -> Some s
| _ -> None

let getTag attributes =
match attributes |> getAttributePayload tagIsTag with
| Some (_, StringPayload s) -> Some s
| _ -> None

let getAsInt attributes =
match attributes |> getAttributePayload tagIsAs with
| Some (_, IntPayload s) -> (
Expand Down
6 changes: 3 additions & 3 deletions jscomp/gentype/EmitType.ml
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ let rec renderType ~(config : Config.t) ?(indent = None) ~typeNameIsInterface
|> String.concat ", ")
^ "]"
| TypeVar s -> s
| Variant {inherits; noPayloads; payloads; polymorphic; unboxed} ->
| Variant {inherits; noPayloads; payloads; polymorphic; unboxed; customTag} ->
let inheritsRendered =
inherits
|> List.map (fun type_ ->
Expand All @@ -196,15 +196,15 @@ let rec renderType ~(config : Config.t) ?(indent = None) ~typeNameIsInterface
in
let tagField =
case |> labelJSToString
|> field ~name:(Runtime.jsVariantTag ~polymorphic:false)
|> field ~name:(Runtime.jsVariantTag ~customTag)
in
match (unboxed, type_) with
| true, type_ -> type_ |> render
| false, type_ when polymorphic ->
(* poly variant *)
[
case |> labelJSToString
|> field ~name:(Runtime.jsVariantTag ~polymorphic);
|> field ~name:Runtime.jsPolymorphicVariantTag;
type_ |> render
|> field ~name:(Runtime.jsVariantValue ~polymorphic);
]
Expand Down
6 changes: 4 additions & 2 deletions jscomp/gentype/GenTypeCommon.ml
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ and variant = {
payloads: payload list;
polymorphic: bool; (* If true, this is a polymorphic variant *)
unboxed: bool;
customTag: string option;
}

and payload = {case: case; t: type_}
Expand Down Expand Up @@ -158,8 +159,9 @@ let rec depToResolvedName (dep : dep) =
| Internal resolvedName -> resolvedName
| Dot (p, s) -> ResolvedName.dot s (p |> depToResolvedName)

let createVariant ~inherits ~noPayloads ~payloads ~polymorphic ~unboxed =
Variant {inherits; noPayloads; payloads; polymorphic; unboxed}
let createVariant ~inherits ~noPayloads ~payloads ~polymorphic ~unboxed
~customTag =
Variant {inherits; noPayloads; payloads; polymorphic; unboxed; customTag}

let ident ?(builtin = true) ?(typeArgs = []) name =
Ident {builtin; name; typeArgs}
Expand Down
9 changes: 5 additions & 4 deletions jscomp/gentype/Runtime.ml
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,11 @@ let rec emitModuleAccessPath ~config moduleAccessPath =
| Dot (p, moduleItem) ->
p |> emitModuleAccessPath ~config |> EmitText.fieldAccess ~label:moduleItem

let jsVariantTag ~polymorphic =
match polymorphic with
| true -> "NAME"
| false -> "TAG"
let jsVariantTag ~customTag =
match customTag with
| None -> "TAG"
| Some tag -> tag
let jsPolymorphicVariantTag = "NAME"

let jsVariantPayloadTag ~n = "_" ^ string_of_int n

Expand Down
3 changes: 2 additions & 1 deletion jscomp/gentype/Runtime.mli
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ val newModuleItem : name:string -> moduleItem
val newRecordValue : unboxed:bool -> recordGen -> recordValue
val recordGen : unit -> recordGen
val recordValueToString : recordValue -> string
val jsVariantTag : polymorphic:bool -> string
val jsVariantTag : customTag:string option -> string
val jsPolymorphicVariantTag : string
val jsVariantPayloadTag : n:int -> string
val jsVariantValue : polymorphic:bool -> string
2 changes: 1 addition & 1 deletion jscomp/gentype/TranslateCoreType.ml
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ and translateCoreType_ ~config ~typeVarsGen
let inherits = inheritsTranslations |> List.map (fun {type_} -> type_) in
let type_ =
createVariant ~noPayloads ~payloads ~inherits ~polymorphic:true
~unboxed:false
~unboxed:false ~customTag:None
in
let dependencies =
(inheritsTranslations
Expand Down
5 changes: 3 additions & 2 deletions jscomp/gentype/TranslateTypeDeclarations.ml
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ let traslateDeclarationKind ~config ~loc ~outputFileRelative ~resolver
let unboxedAnnotation =
typeAttributes |> Annotation.hasAttribute Annotation.tagIsUnboxed
in
let customTag = typeAttributes |> Annotation.getTag in
let returnTypeDeclaration (typeDeclaration : CodeItem.typeDeclaration) =
match opaque = Some true with
| true -> [{typeDeclaration with importTypes = []}]
Expand Down Expand Up @@ -197,7 +198,7 @@ let traslateDeclarationKind ~config ~loc ~outputFileRelative ~resolver
else variant.payloads
in
createVariant ~inherits:variant.inherits ~noPayloads ~payloads
~polymorphic:true ~unboxed:false
~polymorphic:true ~unboxed:false ~customTag:None
| _ -> translation.type_
in
{translation with type_} |> handleGeneralDeclaration
Expand Down Expand Up @@ -289,7 +290,7 @@ let traslateDeclarationKind ~config ~loc ~outputFileRelative ~resolver
in
let variantTyp =
createVariant ~inherits:[] ~noPayloads ~payloads ~polymorphic:false
~unboxed:unboxedAnnotation
~unboxed:unboxedAnnotation ~customTag
in
let resolvedTypeName = typeName |> TypeEnv.addModulePath ~typeEnv in
let exportFromTypeDeclaration =
Expand Down
6 changes: 3 additions & 3 deletions jscomp/gentype/TranslateTypeExprFromTypes.ml
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ let translateConstr ~config ~paramsTranslation ~(path : Path.t) ~typeEnv =
case 0 "Ok" paramTranslation1.type_;
case 1 "Error" paramTranslation2.type_;
]
~polymorphic:false ~unboxed:false
~polymorphic:false ~unboxed:false ~customTag:None
in
{
dependencies =
Expand Down Expand Up @@ -382,7 +382,7 @@ and translateTypeExprFromTypes_ ~config ~typeVarsGen ~typeEnv
in
let type_ =
createVariant ~inherits:[] ~noPayloads ~payloads:[] ~polymorphic:true
~unboxed:false
~unboxed:false ~customTag:None
in
{dependencies = []; type_}
| {noPayloads = []; payloads = [(_label, t)]; unknowns = []} ->
Expand Down Expand Up @@ -411,7 +411,7 @@ and translateTypeExprFromTypes_ ~config ~typeVarsGen ~typeEnv
in
let type_ =
createVariant ~inherits:[] ~noPayloads ~payloads ~polymorphic:true
~unboxed:false
~unboxed:false ~customTag:None
in
let dependencies =
payloadTranslations
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@ export type fromPayload = { readonly x: number; readonly s: string };
// tslint:disable-next-line:interface-over-type-literal
export type toPayload = { readonly result: string };

// tslint:disable-next-line:interface-over-type-literal
export type settledResult<a> =
{ status: "fulfilled"; readonly value: a }
| { status: "rejected"; readonly reason: unknown };

// tslint:disable-next-line:interface-over-type-literal
export type settled = settledResult<string>;

export const convert: (_1:Promise<fromPayload>) => Promise<toPayload> = TestPromiseBS.convert;

export const barx: (_1:{ readonly x?: Promise<(undefined | string)> }, _2:void) => boolean = TestPromiseBS.barx;
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,11 @@ type fromPayload = {
@genType let convert = Js.Promise.then_(({s}) => Js.Promise.resolve({result: s}))

@genType let barx = (~x=Js.Promise.resolve(Some("a")), ()) => x == x

@genType
@tag("status")
type settledResult<+'a> =
| @as("fulfilled") Fulfilled({value: 'a}) | @as("rejected") Rejected({reason: unknown})

@genType
type settled = settledResult<string>
51 changes: 51 additions & 0 deletions jscomp/others/js_promise2.res
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
type t<+'a> = promise<'a>
type error

@tag("status")
type settledResult<+'a> =
| @as("fulfilled") Fulfilled({value: 'a}) | @as("rejected") Rejected({reason: unknown})

/** Type-safe t-first then */
let then: (promise<'a>, 'a => promise<'b>) => promise<'b> = %raw(`
function(p, cont) {
Expand Down Expand Up @@ -52,6 +56,53 @@ external all6: (
(promise<'a0>, promise<'a1>, promise<'a2>, promise<'a3>, promise<'a4>, promise<'a5>)
) => promise<('a0, 'a1, 'a2, 'a3, 'a4, 'a5)> = "all"

@val @scope("Promise")
external allSettled: array<promise<'a>> => promise<array<settledResult<'a>>> = "allSettled"

@val @scope("Promise")
external allSettled2: ((promise<'a0>, promise<'a1>)) => promise<(
settledResult<'a0>,
settledResult<'a1>,
)> = "allSettled"

@val @scope("Promise")
external allSettled3: ((promise<'a0>, promise<'a1>, promise<'a2>)) => promise<(
settledResult<'a0>,
settledResult<'a1>,
settledResult<'a2>,
)> = "allSettled"

@val @scope("Promise")
external allSettled4: ((promise<'a0>, promise<'a1>, promise<'a2>, promise<'a3>)) => promise<(
settledResult<'a0>,
settledResult<'a1>,
settledResult<'a2>,
settledResult<'a3>,
)> = "allSettled"

@val @scope("Promise")
external allSettled5: (
(promise<'a0>, promise<'a1>, promise<'a2>, promise<'a3>, promise<'a4>)
) => promise<(
settledResult<'a0>,
settledResult<'a1>,
settledResult<'a2>,
settledResult<'a3>,
settledResult<'a4>,
)> = "allSettled"

@val @scope("Promise")
external allSettled6: (
(promise<'a0>, promise<'a1>, promise<'a2>, promise<'a3>, promise<'a4>, promise<'a5>)
) => promise<(
settledResult<'a0>,
settledResult<'a1>,
settledResult<'a2>,
settledResult<'a3>,
settledResult<'a4>,
settledResult<'a5>,
)> = "allSettled"

@val @scope("Promise") external race: array<promise<'a>> => promise<'a> = "race"

external unsafe_async: 'a => promise<'a> = "%identity"
Expand Down
6 changes: 3 additions & 3 deletions jscomp/runtime/release.ninja
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ o runtime/caml_bytes.cmj : cc_cmi runtime/caml_bytes.ml | runtime/caml_bytes.cmi
o runtime/caml_bytes.cmi : cc runtime/caml_bytes.mli | runtime/bs_stdlib_mini.cmi runtime/js.cmi runtime/js.cmj
o runtime/caml_float.cmj : cc_cmi runtime/caml_float.ml | runtime/caml_float.cmi runtime/caml_float_extern.cmj
o runtime/caml_float.cmi : cc runtime/caml_float.mli | runtime/bs_stdlib_mini.cmi runtime/js.cmi runtime/js.cmj
o runtime/caml_format.cmj : cc_cmi runtime/caml_format.ml | runtime/caml.cmj runtime/caml_float.cmj runtime/caml_float_extern.cmj runtime/caml_format.cmi runtime/caml_int64.cmj runtime/caml_int64_extern.cmj runtime/caml_nativeint_extern.cmj runtime/caml_string_extern.cmj
o runtime/caml_format.cmj : cc_cmi runtime/caml_format.ml | runtime/caml_float.cmj runtime/caml_float_extern.cmj runtime/caml_format.cmi runtime/caml_int64.cmj runtime/caml_int64_extern.cmj runtime/caml_nativeint_extern.cmj runtime/caml_string_extern.cmj
o runtime/caml_format.cmi : cc runtime/caml_format.mli | runtime/bs_stdlib_mini.cmi runtime/js.cmi runtime/js.cmj
o runtime/caml_hash.cmj : cc_cmi runtime/caml_hash.ml | runtime/caml_hash.cmi runtime/caml_hash_primitive.cmj runtime/caml_nativeint_extern.cmj runtime/js.cmj
o runtime/caml_hash.cmi : cc runtime/caml_hash.mli | runtime/bs_stdlib_mini.cmi runtime/js.cmi runtime/js.cmj
Expand All @@ -37,7 +37,7 @@ o runtime/caml_md5.cmj : cc_cmi runtime/caml_md5.ml | runtime/caml_array_extern.
o runtime/caml_md5.cmi : cc runtime/caml_md5.mli | runtime/bs_stdlib_mini.cmi runtime/js.cmi runtime/js.cmj
o runtime/caml_module.cmj : cc_cmi runtime/caml_module.ml | runtime/caml_array_extern.cmj runtime/caml_module.cmi runtime/caml_obj.cmj
o runtime/caml_module.cmi : cc runtime/caml_module.mli | runtime/bs_stdlib_mini.cmi runtime/js.cmi runtime/js.cmj
o runtime/caml_obj.cmj : cc_cmi runtime/caml_obj.res | runtime/caml.cmj runtime/caml_array_extern.cmj runtime/caml_obj.cmi runtime/caml_option.cmj runtime/js.cmj
o runtime/caml_obj.cmj : cc_cmi runtime/caml_obj.res | runtime/caml_array_extern.cmj runtime/caml_obj.cmi runtime/caml_option.cmj runtime/js.cmj
o runtime/caml_obj.cmi : cc runtime/caml_obj.resi | runtime/js.cmj
o runtime/caml_option.cmj : cc_cmi runtime/caml_option.ml | runtime/caml_option.cmi runtime/caml_undefined_extern.cmj runtime/js.cmj
o runtime/caml_option.cmi : cc runtime/caml_option.mli | runtime/bs_stdlib_mini.cmi runtime/caml_undefined_extern.cmj runtime/js.cmi runtime/js.cmj
Expand All @@ -54,7 +54,7 @@ o runtime/caml_exceptions.cmi runtime/caml_exceptions.cmj : cc runtime/caml_exce
o runtime/caml_external_polyfill.cmi runtime/caml_external_polyfill.cmj : cc runtime/caml_external_polyfill.ml | runtime/bs_stdlib_mini.cmi runtime/js.cmi runtime/js.cmj
o runtime/caml_float_extern.cmi runtime/caml_float_extern.cmj : cc runtime/caml_float_extern.ml | runtime/bs_stdlib_mini.cmi runtime/js.cmi runtime/js.cmj
o runtime/caml_int64_extern.cmi runtime/caml_int64_extern.cmj : cc runtime/caml_int64_extern.ml | runtime/bs_stdlib_mini.cmi runtime/js.cmi runtime/js.cmj
o runtime/caml_js_exceptions.cmi runtime/caml_js_exceptions.cmj : cc runtime/caml_js_exceptions.ml | runtime/bs_stdlib_mini.cmi runtime/caml_exceptions.cmj runtime/caml_option.cmj runtime/js.cmi runtime/js.cmj
o runtime/caml_js_exceptions.cmi runtime/caml_js_exceptions.cmj : cc runtime/caml_js_exceptions.ml | runtime/bs_stdlib_mini.cmi runtime/caml_exceptions.cmj runtime/js.cmi runtime/js.cmj
o runtime/caml_nativeint_extern.cmi runtime/caml_nativeint_extern.cmj : cc runtime/caml_nativeint_extern.ml | runtime/bs_stdlib_mini.cmi runtime/js.cmi runtime/js.cmj
o runtime/caml_string_extern.cmi runtime/caml_string_extern.cmj : cc runtime/caml_string_extern.ml | runtime/bs_stdlib_mini.cmi runtime/js.cmi runtime/js.cmj
o runtime/caml_undefined_extern.cmi runtime/caml_undefined_extern.cmj : cc runtime/caml_undefined_extern.ml | runtime/bs_stdlib_mini.cmi runtime/js.cmi runtime/js.cmj
Expand Down
3 changes: 2 additions & 1 deletion jscomp/test/build.ninja

Large diffs are not rendered by default.

35 changes: 35 additions & 0 deletions jscomp/test/test_promise.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions jscomp/test/test_promise.res
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
let p1 = Js.Promise2.make((~resolve, ~reject) => resolve(1))
let p2 = Js.Promise2.make((~resolve, ~reject) => resolve("foo"))

switch await Js.Promise2.allSettled2((p1, p2)) {
| (Fulfilled({value: v1}), Fulfilled({value: v2})) => Js.log2(v1, v2)
| (Rejected({reason}), Fulfilled(_)) => Js.log2("first rejected", reason)
| (Fulfilled(_), Rejected({reason})) => Js.log2("second rejected", reason)
| _ => Js.log("both rejected")
}