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

Re add getPaused setPaused #156

Merged
merged 1 commit into from
Jan 9, 2024
Merged
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
24 changes: 24 additions & 0 deletions src/components/Router.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ export interface IRouterContext extends IRouterContextStackStates {
routeIndex: number
previousPageIsMount: boolean
unmountPreviousPage: () => void
getPaused: () => boolean
setPaused: (value: boolean) => void
}

// -------------------------------------------------------------------------------- PREPARE / CONTEXT
Expand All @@ -85,6 +87,8 @@ export const RouterContext = createContext<IRouterContext>({
previousPageIsMount: true,
staticLocation: undefined,
unmountPreviousPage: () => {},
getPaused: () => false,
setPaused: (value: boolean) => {},
})
RouterContext.displayName = "RouterContext"

Expand Down Expand Up @@ -245,13 +249,31 @@ function Router(props: {

const currentRouteRef = useRef<TRoute>()

/**
* Enable paused on Router instance
*/
const _waitingUrl = useRef(null)
const _paused = useRef<boolean>(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
* Dispatch new routes via RouterContext
*/

const handleHistory = async (url = ""): Promise<void> => {
if (_paused.current) {
_waitingUrl.current = url
return
}

const matchingRoute = getRouteFromUrl({
pUrl: url,
pRoutes: routes,
Expand Down Expand Up @@ -411,6 +433,8 @@ function Router(props: {
routeIndex,
previousPageIsMount,
unmountPreviousPage,
getPaused,
setPaused,
}}
/>
)
Expand Down
Loading