From c75bd77e06b81d2018949b90230bb044aeab2d34 Mon Sep 17 00:00:00 2001 From: hariroshan Date: Sat, 25 Feb 2023 17:45:48 +0530 Subject: [PATCH] improved typesafty --- elm-native-js/src/Native/Native.res | 56 +++++++++++-------- elm-native-js/src/Native/NativescriptCore.res | 5 +- elm-native-js/src/Native/Types.res | 15 +++-- 3 files changed, 45 insertions(+), 31 deletions(-) diff --git a/elm-native-js/src/Native/Native.res b/elm-native-js/src/Native/Native.res index 3ef350a..c93dd4e 100644 --- a/elm-native-js/src/Native/Native.res +++ b/elm-native-js/src/Native/Native.res @@ -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 => Js.Nullable.t<'a>, ) => current.navigationOptions ->Js.Nullable.toOption - ->Belt.Option.map(x => { + ->Belt.Option.map((x: Types.navigationOptions): 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, + ) => x.animated) + current->setNullableProperty(config, "transition", ( + x: Types.navigationOptions, + ) => x.transition) + current->setNullableProperty(config, "transitioniOS", ( + x: Types.navigationOptions, + ) => x.transitioniOS) current->setNullableProperty(config, "transitionAndroid", ( - x: Types.navigationOptions, + x: Types.navigationOptions, ) => 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, + ) => x.backstackVisible) + current->setNullableProperty(config, "clearHistory", ( + x: Types.navigationOptions, + ) => x.clearHistory) data->Types.navigate(config) } }) diff --git a/elm-native-js/src/Native/NativescriptCore.res b/elm-native-js/src/Native/NativescriptCore.res index 8427062..8d6cc62 100644 --- a/elm-native-js/src/Native/NativescriptCore.res +++ b/elm-native-js/src/Native/NativescriptCore.res @@ -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.transition< + Types.clean, + > => { { ...transition, curve: switch transition.curve->Js.Nullable.toOption { diff --git a/elm-native-js/src/Native/Types.res b/elm-native-js/src/Native/Types.res index 9cc3d23..1665289 100644 --- a/elm-native-js/src/Native/Types.res +++ b/elm-native-js/src/Native/Types.res @@ -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, } -type navigationOptions = { +type navigationOptions<'kind> = { animated: Js.Nullable.t, - transition: Js.Nullable.t, - transitioniOS: Js.Nullable.t, - transitionAndroid: Js.Nullable.t, + transition: Js.Nullable.t>, + transitioniOS: Js.Nullable.t>, + transitionAndroid: Js.Nullable.t>, backstackVisible: Js.Nullable.t, clearHistory: Js.Nullable.t, } @@ -111,7 +114,7 @@ and htmlElement = { data: Js.Nullable.t, children: array, items: array, - navigationOptions: Js.Nullable.t, + navigationOptions: Js.Nullable.t>, modalPage: Js.Nullable.t, } and frameMethods = {pageAdded: (. htmlElement) => unit}