improved typesafty

This commit is contained in:
hariroshan 2023-02-25 17:45:48 +05:30
parent 528c3c6e8c
commit c75bd77e06
3 changed files with 45 additions and 31 deletions

View File

@ -2,22 +2,30 @@ module Frame = {
%%private(
@module("@nativescript/core") @new
external new: unit => Types.nativeObject = "Frame"
let mapTransition = transition =>
transition
->Js.Nullable.toOption
->Belt.Option.map(transition =>
transition->NativescriptCore.AnimationCurve.convertTransitionCurve
)
->Js.Nullable.fromOption
let setNullableProperty = (
current: Types.htmlElement,
config: Types.navigationConfig,
key: string,
fx: Types.navigationOptions => Js.Nullable.t<'a>,
fx: Types.navigationOptions<Types.clean> => Js.Nullable.t<'a>,
) =>
current.navigationOptions
->Js.Nullable.toOption
->Belt.Option.map(x => {
->Belt.Option.map((x: Types.navigationOptions<Types.raw>): Types.navigationOptions<
Types.clean,
> => {
...x,
transition: x.transition
->Js.Nullable.toOption
->Belt.Option.map(transition =>
transition->NativescriptCore.AnimationCurve.convertTransitionCurve->Js.Nullable.return
)
->Belt.Option.getWithDefault(x.transition),
transition: x.transition->mapTransition,
transitioniOS: x.transitioniOS->mapTransition,
transitionAndroid: x.transitionAndroid->mapTransition,
})
->Belt.Option.flatMap(x => fx(x)->Js.Nullable.toOption)
->Belt.Option.forEach(value => {
@ -79,24 +87,24 @@ module Frame = {
let config: Types.navigationConfig = {
create: _ => pageData,
}
current->setNullableProperty(config, "animated", (x: Types.navigationOptions) =>
x.animated
)
current->setNullableProperty(config, "transition", (x: Types.navigationOptions) =>
x.transition
)
current->setNullableProperty(config, "transitioniOS", (x: Types.navigationOptions) =>
x.transitioniOS
)
current->setNullableProperty(config, "animated", (
x: Types.navigationOptions<Types.clean>,
) => x.animated)
current->setNullableProperty(config, "transition", (
x: Types.navigationOptions<Types.clean>,
) => x.transition)
current->setNullableProperty(config, "transitioniOS", (
x: Types.navigationOptions<Types.clean>,
) => x.transitioniOS)
current->setNullableProperty(config, "transitionAndroid", (
x: Types.navigationOptions,
x: Types.navigationOptions<Types.clean>,
) => x.transitionAndroid)
current->setNullableProperty(config, "backstackVisible", (x: Types.navigationOptions) =>
x.backstackVisible
)
current->setNullableProperty(config, "clearHistory", (x: Types.navigationOptions) =>
x.clearHistory
)
current->setNullableProperty(config, "backstackVisible", (
x: Types.navigationOptions<Types.clean>,
) => x.backstackVisible)
current->setNullableProperty(config, "clearHistory", (
x: Types.navigationOptions<Types.clean>,
) => x.clearHistory)
data->Types.navigate(config)
}
})

View File

@ -2,6 +2,7 @@ type rootLayout
module AnimationCurve = {
type animationCurve = {cubicBezier: (. float, float, float, float) => string}
%%private(
let cubicBezierParamsRE = %re(`/cubicBezier\(([0-9]+\.?[0-9]*),([0-9]+\.?[0-9]*),([0-9]+\.?[0-9]*),([0-9]+\.?[0-9]*)\)/g`)
@ -29,7 +30,9 @@ module AnimationCurve = {
external animationCurve: animationCurve = "AnimationCurve"
)
let convertTransitionCurve = (transition: Types.transition): Types.transition => {
let convertTransitionCurve = (transition: Types.transition<Types.raw>): Types.transition<
Types.clean,
> => {
{
...transition,
curve: switch transition.curve->Js.Nullable.toOption {

View File

@ -20,7 +20,10 @@ type constructor = {
name: string,
}
type transition = {
type raw
type clean
type transition<'a> = {
/**
* Can be one of the built-in transitions:
* - curl (same as curlUp) (iOS only)
@ -43,11 +46,11 @@ type transition = {
curve: Js.Nullable.t<string>,
}
type navigationOptions = {
type navigationOptions<'kind> = {
animated: Js.Nullable.t<bool>,
transition: Js.Nullable.t<transition>,
transitioniOS: Js.Nullable.t<transition>,
transitionAndroid: Js.Nullable.t<transition>,
transition: Js.Nullable.t<transition<'kind>>,
transitioniOS: Js.Nullable.t<transition<'kind>>,
transitionAndroid: Js.Nullable.t<transition<'kind>>,
backstackVisible: Js.Nullable.t<bool>,
clearHistory: Js.Nullable.t<bool>,
}
@ -111,7 +114,7 @@ and htmlElement = {
data: Js.Nullable.t<nativeObject>,
children: array<htmlElement>,
items: array<string>,
navigationOptions: Js.Nullable.t<navigationOptions>,
navigationOptions: Js.Nullable.t<navigationOptions<raw>>,
modalPage: Js.Nullable.t<bool>,
}
and frameMethods = {pageAdded: (. htmlElement) => unit}