From ad5193bdc173bd7182c42b2e2474fb7f0900313e Mon Sep 17 00:00:00 2001 From: Willy Brauner Date: Tue, 9 Jan 2024 11:50:48 +0100 Subject: [PATCH] re add getPaused setPaused (#156) --- src/components/Router.tsx | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/src/components/Router.tsx b/src/components/Router.tsx index 7b131a1..3b50c82 100644 --- a/src/components/Router.tsx +++ b/src/components/Router.tsx @@ -61,6 +61,8 @@ export interface IRouterContext extends IRouterContextStackStates { routeIndex: number previousPageIsMount: boolean unmountPreviousPage: () => void + getPaused: () => boolean + setPaused: (value: boolean) => void } // -------------------------------------------------------------------------------- PREPARE / CONTEXT @@ -85,6 +87,8 @@ export const RouterContext = createContext({ previousPageIsMount: true, staticLocation: undefined, unmountPreviousPage: () => {}, + getPaused: () => false, + setPaused: (value: boolean) => {}, }) RouterContext.displayName = "RouterContext" @@ -245,6 +249,19 @@ function Router(props: { const currentRouteRef = useRef() + /** + * Enable paused on Router instance + */ + const _waitingUrl = useRef(null) + const _paused = useRef(false) + const getPaused = () => _paused.current + const setPaused = (value: boolean) => { + _paused.current = value + if (!value && _waitingUrl.current) { + handleHistory(_waitingUrl.current) + _waitingUrl.current = null + } + } /** * Handle history * Update routes when history change @@ -252,6 +269,11 @@ function Router(props: { */ const handleHistory = async (url = ""): Promise => { + if (_paused.current) { + _waitingUrl.current = url + return + } + const matchingRoute = getRouteFromUrl({ pUrl: url, pRoutes: routes, @@ -411,6 +433,8 @@ function Router(props: { routeIndex, previousPageIsMount, unmountPreviousPage, + getPaused, + setPaused, }} /> )