From 2d4f46cca35c4a361f2b3966f827bc77271888a6 Mon Sep 17 00:00:00 2001 From: Gabriel Nordeborn Date: Fri, 13 Sep 2024 08:30:51 +0200 Subject: [PATCH] optimize useMakeLinkWithPreservedPath --- .changeset/silent-geese-check.md | 5 +++ ...oot__Todos__ByStatusDecodedExtra_route.res | 13 +------ ...t__Todos__ByStatusDecoded__Child_route.res | 13 +------ ...te__Root__Todos__ByStatusDecoded_route.res | 13 +------ .../Route__Root__Todos__ByStatus_route.res | 13 +------ .../Route__Root__Todos__Single_route.res | 13 +------ .../Route__Root__Todos_route.res | 13 +------ .../Route__Root__Todos__ByStatus_route.res | 13 +------ .../Route__Root__Todos__Single_route.res | 13 +------ .../Route__Root__Todos_route.res | 13 +------ .../cli/RescriptRelayRouterCli__Codegen.res | 13 +------ .../src/RelayRouter__Internal.res | 35 +++++++++++++++++-- .../src/RelayRouter__Internal.resi | 19 ++++++++-- 13 files changed, 65 insertions(+), 124 deletions(-) create mode 100644 .changeset/silent-geese-check.md diff --git a/.changeset/silent-geese-check.md b/.changeset/silent-geese-check.md new file mode 100644 index 0000000..b94276d --- /dev/null +++ b/.changeset/silent-geese-check.md @@ -0,0 +1,5 @@ +--- +"rescript-relay-router": patch +--- + +Optimize useMakeLinkWithPreservedPath diff --git a/examples/client-rendering/src/routes/__generated__/Route__Root__Todos__ByStatusDecodedExtra_route.res b/examples/client-rendering/src/routes/__generated__/Route__Root__Todos__ByStatusDecodedExtra_route.res index f3a82da..8f61efd 100644 --- a/examples/client-rendering/src/routes/__generated__/Route__Root__Todos__ByStatusDecodedExtra_route.res +++ b/examples/client-rendering/src/routes/__generated__/Route__Root__Todos__ByStatusDecodedExtra_route.res @@ -127,18 +127,7 @@ let makeLinkFromQueryParams = (~byStatusDecoded: TodoStatusPathParam.t, queryPar } @live -let useMakeLinkWithPreservedPath = () => { - let location = RelayRouter.Utils.useLocation() - React.useMemo(() => { - (makeNewQueryParams: queryParams => queryParams) => { - let newQueryParams = location.search->parseQueryParams->makeNewQueryParams - open RelayRouter.Bindings - let queryParams = location.search->QueryParams.parse - queryParams->applyQueryParams(~newParams=newQueryParams) - location.pathname ++ queryParams->QueryParams.toString - } - }, [location.search]) -} +let useMakeLinkWithPreservedPath = (): ((queryParams => queryParams) => string) => RelayRouter__Internal.useMakeLinkWithPreservedPath(~parseQueryParams, ~applyQueryParams) @live diff --git a/examples/client-rendering/src/routes/__generated__/Route__Root__Todos__ByStatusDecoded__Child_route.res b/examples/client-rendering/src/routes/__generated__/Route__Root__Todos__ByStatusDecoded__Child_route.res index c3f78ea..c59a7b3 100644 --- a/examples/client-rendering/src/routes/__generated__/Route__Root__Todos__ByStatusDecoded__Child_route.res +++ b/examples/client-rendering/src/routes/__generated__/Route__Root__Todos__ByStatusDecoded__Child_route.res @@ -127,18 +127,7 @@ let makeLinkFromQueryParams = (~byStatusDecoded: TodoStatusPathParam.t, queryPar } @live -let useMakeLinkWithPreservedPath = () => { - let location = RelayRouter.Utils.useLocation() - React.useMemo(() => { - (makeNewQueryParams: queryParams => queryParams) => { - let newQueryParams = location.search->parseQueryParams->makeNewQueryParams - open RelayRouter.Bindings - let queryParams = location.search->QueryParams.parse - queryParams->applyQueryParams(~newParams=newQueryParams) - location.pathname ++ queryParams->QueryParams.toString - } - }, [location.search]) -} +let useMakeLinkWithPreservedPath = (): ((queryParams => queryParams) => string) => RelayRouter__Internal.useMakeLinkWithPreservedPath(~parseQueryParams, ~applyQueryParams) @live diff --git a/examples/client-rendering/src/routes/__generated__/Route__Root__Todos__ByStatusDecoded_route.res b/examples/client-rendering/src/routes/__generated__/Route__Root__Todos__ByStatusDecoded_route.res index 8c169c3..2588c03 100644 --- a/examples/client-rendering/src/routes/__generated__/Route__Root__Todos__ByStatusDecoded_route.res +++ b/examples/client-rendering/src/routes/__generated__/Route__Root__Todos__ByStatusDecoded_route.res @@ -135,18 +135,7 @@ let makeLinkFromQueryParams = (~byStatusDecoded: TodoStatusPathParam.t, queryPar } @live -let useMakeLinkWithPreservedPath = () => { - let location = RelayRouter.Utils.useLocation() - React.useMemo(() => { - (makeNewQueryParams: queryParams => queryParams) => { - let newQueryParams = location.search->parseQueryParams->makeNewQueryParams - open RelayRouter.Bindings - let queryParams = location.search->QueryParams.parse - queryParams->applyQueryParams(~newParams=newQueryParams) - location.pathname ++ queryParams->QueryParams.toString - } - }, [location.search]) -} +let useMakeLinkWithPreservedPath = (): ((queryParams => queryParams) => string) => RelayRouter__Internal.useMakeLinkWithPreservedPath(~parseQueryParams, ~applyQueryParams) @live diff --git a/examples/client-rendering/src/routes/__generated__/Route__Root__Todos__ByStatus_route.res b/examples/client-rendering/src/routes/__generated__/Route__Root__Todos__ByStatus_route.res index a71380f..9e5916e 100644 --- a/examples/client-rendering/src/routes/__generated__/Route__Root__Todos__ByStatus_route.res +++ b/examples/client-rendering/src/routes/__generated__/Route__Root__Todos__ByStatus_route.res @@ -127,18 +127,7 @@ let makeLinkFromQueryParams = (~byStatus: [#"completed" | #"not-completed"], que } @live -let useMakeLinkWithPreservedPath = () => { - let location = RelayRouter.Utils.useLocation() - React.useMemo(() => { - (makeNewQueryParams: queryParams => queryParams) => { - let newQueryParams = location.search->parseQueryParams->makeNewQueryParams - open RelayRouter.Bindings - let queryParams = location.search->QueryParams.parse - queryParams->applyQueryParams(~newParams=newQueryParams) - location.pathname ++ queryParams->QueryParams.toString - } - }, [location.search]) -} +let useMakeLinkWithPreservedPath = (): ((queryParams => queryParams) => string) => RelayRouter__Internal.useMakeLinkWithPreservedPath(~parseQueryParams, ~applyQueryParams) @live diff --git a/examples/client-rendering/src/routes/__generated__/Route__Root__Todos__Single_route.res b/examples/client-rendering/src/routes/__generated__/Route__Root__Todos__Single_route.res index 70e28a8..b10a20b 100644 --- a/examples/client-rendering/src/routes/__generated__/Route__Root__Todos__Single_route.res +++ b/examples/client-rendering/src/routes/__generated__/Route__Root__Todos__Single_route.res @@ -145,18 +145,7 @@ let makeLinkFromQueryParams = (~todoId: string, queryParams: queryParams) => { } @live -let useMakeLinkWithPreservedPath = () => { - let location = RelayRouter.Utils.useLocation() - React.useMemo(() => { - (makeNewQueryParams: queryParams => queryParams) => { - let newQueryParams = location.search->parseQueryParams->makeNewQueryParams - open RelayRouter.Bindings - let queryParams = location.search->QueryParams.parse - queryParams->applyQueryParams(~newParams=newQueryParams) - location.pathname ++ queryParams->QueryParams.toString - } - }, [location.search]) -} +let useMakeLinkWithPreservedPath = (): ((queryParams => queryParams) => string) => RelayRouter__Internal.useMakeLinkWithPreservedPath(~parseQueryParams, ~applyQueryParams) @live diff --git a/examples/client-rendering/src/routes/__generated__/Route__Root__Todos_route.res b/examples/client-rendering/src/routes/__generated__/Route__Root__Todos_route.res index 7bf7108..f061150 100644 --- a/examples/client-rendering/src/routes/__generated__/Route__Root__Todos_route.res +++ b/examples/client-rendering/src/routes/__generated__/Route__Root__Todos_route.res @@ -129,18 +129,7 @@ let makeLinkFromQueryParams = (queryParams: queryParams) => { } @live -let useMakeLinkWithPreservedPath = () => { - let location = RelayRouter.Utils.useLocation() - React.useMemo(() => { - (makeNewQueryParams: queryParams => queryParams) => { - let newQueryParams = location.search->parseQueryParams->makeNewQueryParams - open RelayRouter.Bindings - let queryParams = location.search->QueryParams.parse - queryParams->applyQueryParams(~newParams=newQueryParams) - location.pathname ++ queryParams->QueryParams.toString - } - }, [location.search]) -} +let useMakeLinkWithPreservedPath = (): ((queryParams => queryParams) => string) => RelayRouter__Internal.useMakeLinkWithPreservedPath(~parseQueryParams, ~applyQueryParams) @live diff --git a/examples/express/src/routes/__generated__/Route__Root__Todos__ByStatus_route.res b/examples/express/src/routes/__generated__/Route__Root__Todos__ByStatus_route.res index 01fcf28..4f1cb0b 100644 --- a/examples/express/src/routes/__generated__/Route__Root__Todos__ByStatus_route.res +++ b/examples/express/src/routes/__generated__/Route__Root__Todos__ByStatus_route.res @@ -117,18 +117,7 @@ let makeLinkFromQueryParams = (~byStatus: [#"completed" | #"notCompleted"], quer } @live -let useMakeLinkWithPreservedPath = () => { - let location = RelayRouter.Utils.useLocation() - React.useMemo(() => { - (makeNewQueryParams: queryParams => queryParams) => { - let newQueryParams = location.search->parseQueryParams->makeNewQueryParams - open RelayRouter.Bindings - let queryParams = location.search->QueryParams.parse - queryParams->applyQueryParams(~newParams=newQueryParams) - location.pathname ++ queryParams->QueryParams.toString - } - }, [location.search]) -} +let useMakeLinkWithPreservedPath = (): ((queryParams => queryParams) => string) => RelayRouter__Internal.useMakeLinkWithPreservedPath(~parseQueryParams, ~applyQueryParams) @live diff --git a/examples/express/src/routes/__generated__/Route__Root__Todos__Single_route.res b/examples/express/src/routes/__generated__/Route__Root__Todos__Single_route.res index bd2905e..fa573fd 100644 --- a/examples/express/src/routes/__generated__/Route__Root__Todos__Single_route.res +++ b/examples/express/src/routes/__generated__/Route__Root__Todos__Single_route.res @@ -135,18 +135,7 @@ let makeLinkFromQueryParams = (~todoId: string, queryParams: queryParams) => { } @live -let useMakeLinkWithPreservedPath = () => { - let location = RelayRouter.Utils.useLocation() - React.useMemo(() => { - (makeNewQueryParams: queryParams => queryParams) => { - let newQueryParams = location.search->parseQueryParams->makeNewQueryParams - open RelayRouter.Bindings - let queryParams = location.search->QueryParams.parse - queryParams->applyQueryParams(~newParams=newQueryParams) - location.pathname ++ queryParams->QueryParams.toString - } - }, [location.search]) -} +let useMakeLinkWithPreservedPath = (): ((queryParams => queryParams) => string) => RelayRouter__Internal.useMakeLinkWithPreservedPath(~parseQueryParams, ~applyQueryParams) @live diff --git a/examples/express/src/routes/__generated__/Route__Root__Todos_route.res b/examples/express/src/routes/__generated__/Route__Root__Todos_route.res index 3ec25b2..26c54ae 100644 --- a/examples/express/src/routes/__generated__/Route__Root__Todos_route.res +++ b/examples/express/src/routes/__generated__/Route__Root__Todos_route.res @@ -118,18 +118,7 @@ let makeLinkFromQueryParams = (queryParams: queryParams) => { } @live -let useMakeLinkWithPreservedPath = () => { - let location = RelayRouter.Utils.useLocation() - React.useMemo(() => { - (makeNewQueryParams: queryParams => queryParams) => { - let newQueryParams = location.search->parseQueryParams->makeNewQueryParams - open RelayRouter.Bindings - let queryParams = location.search->QueryParams.parse - queryParams->applyQueryParams(~newParams=newQueryParams) - location.pathname ++ queryParams->QueryParams.toString - } - }, [location.search]) -} +let useMakeLinkWithPreservedPath = (): ((queryParams => queryParams) => string) => RelayRouter__Internal.useMakeLinkWithPreservedPath(~parseQueryParams, ~applyQueryParams) @live diff --git a/packages/rescript-relay-router/cli/RescriptRelayRouterCli__Codegen.res b/packages/rescript-relay-router/cli/RescriptRelayRouterCli__Codegen.res index 83c506b..16ce1a4 100644 --- a/packages/rescript-relay-router/cli/RescriptRelayRouterCli__Codegen.res +++ b/packages/rescript-relay-router/cli/RescriptRelayRouterCli__Codegen.res @@ -151,18 +151,7 @@ let makeLinkFromQueryParams = (`->addToStr ` @live -let useMakeLinkWithPreservedPath = () => { - let location = RelayRouter.Utils.useLocation() - React.useMemo(() => { - (makeNewQueryParams: queryParams => queryParams) => { - let newQueryParams = location.search->parseQueryParams->makeNewQueryParams - open RelayRouter.Bindings - let queryParams = location.search->QueryParams.parse - queryParams->applyQueryParams(~newParams=newQueryParams) - location.pathname ++ queryParams->QueryParams.toString - } - }, [location.search]) -} +let useMakeLinkWithPreservedPath = (): ((queryParams => queryParams) => string) => RelayRouter__Internal.useMakeLinkWithPreservedPath(~parseQueryParams, ~applyQueryParams) `->addToStr } diff --git a/packages/rescript-relay-router/src/RelayRouter__Internal.res b/packages/rescript-relay-router/src/RelayRouter__Internal.res index beb41b4..3e53b64 100644 --- a/packages/rescript-relay-router/src/RelayRouter__Internal.res +++ b/packages/rescript-relay-router/src/RelayRouter__Internal.res @@ -16,9 +16,15 @@ type setQueryParamsFn<'queryParams> = ( ~shallow: bool=?, ) => unit +type parseQueryParamsFn<'queryParams> = string => 'queryParams +type applyQueryParamsFn<'queryParams> = ( + RelayRouter__Bindings.QueryParams.t, + ~newParams: 'queryParams, +) => unit + let useSetQueryParams = ( - ~parseQueryParams: string => 'queryParams, - ~applyQueryParams: (RelayRouter__Bindings.QueryParams.t, ~newParams: 'queryParams) => unit, + ~parseQueryParams: parseQueryParamsFn<'queryParams>, + ~applyQueryParams: applyQueryParamsFn<'queryParams>, ): setQueryParamsFn<'queryParams> => { let router = RelayRouter__Context.useRouterContext() @@ -75,6 +81,31 @@ let useSetQueryParams = ( }, (parseQueryParams, applyQueryParams, router)) } +type makeNewQueryParamsMakerFn<'queryParams> = 'queryParams => 'queryParams + +type makeNewQueryParamsFn<'queryParams> = makeNewQueryParamsMakerFn<'queryParams> => string + +let useMakeLinkWithPreservedPath = ( + ~parseQueryParams: parseQueryParamsFn<'queryParams>, + ~applyQueryParams: applyQueryParamsFn<'queryParams>, +): makeNewQueryParamsFn<'queryParams> => { + let router = RelayRouter__Context.useRouterContext() + React.useMemo3(() => { + (makeNewQueryParams: 'queryParams => 'queryParams) => { + let location = router.history->RelayRouter__History.getLocation + let newQueryParams = + location.search + ->parseQueryParams + ->makeNewQueryParams + + open RelayRouter__Bindings + let queryParams = location.search->QueryParams.parse + queryParams->applyQueryParams(~newParams=newQueryParams) + location.pathname ++ queryParams->QueryParams.toString + } + }, (router, parseQueryParams, applyQueryParams)) +} + type pathMatch = {params: Js.Dict.t} @module("./vendor/react-router.js") @return(nullable) diff --git a/packages/rescript-relay-router/src/RelayRouter__Internal.resi b/packages/rescript-relay-router/src/RelayRouter__Internal.resi index f0af7cb..fa399c6 100644 --- a/packages/rescript-relay-router/src/RelayRouter__Internal.resi +++ b/packages/rescript-relay-router/src/RelayRouter__Internal.resi @@ -6,11 +6,26 @@ type setQueryParamsFn<'queryParams> = ( ~shallow: bool=?, ) => unit +type parseQueryParamsFn<'queryParams> = string => 'queryParams +type applyQueryParamsFn<'queryParams> = ( + RelayRouter__Bindings.QueryParams.t, + ~newParams: 'queryParams, +) => unit + let useSetQueryParams: ( - ~parseQueryParams: string => 'queryParams, - ~applyQueryParams: (RelayRouter__Bindings.QueryParams.t, ~newParams: 'queryParams) => unit, + ~parseQueryParams: parseQueryParamsFn<'queryParams>, + ~applyQueryParams: applyQueryParamsFn<'queryParams>, ) => setQueryParamsFn<'queryParams> +type makeNewQueryParamsMakerFn<'queryParams> = 'queryParams => 'queryParams + +type makeNewQueryParamsFn<'queryParams> = makeNewQueryParamsMakerFn<'queryParams> => string + +let useMakeLinkWithPreservedPath: ( + ~parseQueryParams: parseQueryParamsFn<'queryParams>, + ~applyQueryParams: applyQueryParamsFn<'queryParams>, +) => makeNewQueryParamsFn<'queryParams> + type pathMatch = {params: Js.Dict.t} @module("./vendor/react-router.js") @return(nullable)