1 | {"version":3,"names":["React","NavigationBuilderContext","NavigationRouteContext","VISITED_ROUTE_KEYS","Symbol","shouldPreventRemove","emitter","beforeRemoveListeners","currentRoutes","nextRoutes","action","nextRouteKeys","map","route","key","removedRoutes","filter","includes","reverse","visitedRouteKeys","Set","beforeRemoveAction","has","isPrevented","add","event","emit","type","target","data","canPreventDefault","defaultPrevented","useOnPreventRemove","getState","addKeyedListener","useContext","routeKey","useEffect","state","routes"],"sources":["useOnPreventRemove.tsx"],"sourcesContent":["import type {\n NavigationAction,\n NavigationState,\n} from '@react-navigation/routers';\nimport * as React from 'react';\n\nimport NavigationBuilderContext, {\n ChildBeforeRemoveListener,\n} from './NavigationBuilderContext';\nimport NavigationRouteContext from './NavigationRouteContext';\nimport type { EventMapCore } from './types';\nimport type { NavigationEventEmitter } from './useEventEmitter';\n\ntype Options = {\n getState: () => NavigationState;\n emitter: NavigationEventEmitter<EventMapCore<any>>;\n beforeRemoveListeners: Record<string, ChildBeforeRemoveListener | undefined>;\n};\n\nconst VISITED_ROUTE_KEYS = Symbol('VISITED_ROUTE_KEYS');\n\nexport const shouldPreventRemove = (\n emitter: NavigationEventEmitter<EventMapCore<any>>,\n beforeRemoveListeners: Record<string, ChildBeforeRemoveListener | undefined>,\n currentRoutes: { key: string }[],\n nextRoutes: { key?: string | undefined }[],\n action: NavigationAction\n) => {\n const nextRouteKeys = nextRoutes.map((route) => route.key);\n\n // Call these in reverse order so last screens handle the event first\n const removedRoutes = currentRoutes\n .filter((route) => !nextRouteKeys.includes(route.key))\n .reverse();\n\n const visitedRouteKeys: Set<string> =\n // @ts-expect-error: add this property to mark that we've already emitted this action\n action[VISITED_ROUTE_KEYS] ?? new Set<string>();\n\n const beforeRemoveAction = {\n ...action,\n [VISITED_ROUTE_KEYS]: visitedRouteKeys,\n };\n\n for (const route of removedRoutes) {\n if (visitedRouteKeys.has(route.key)) {\n // Skip if we've already emitted this action for this screen\n continue;\n }\n\n // First, we need to check if any child screens want to prevent it\n const isPrevented = beforeRemoveListeners[route.key]?.(beforeRemoveAction);\n\n if (isPrevented) {\n return true;\n }\n\n visitedRouteKeys.add(route.key);\n\n const event = emitter.emit({\n type: 'beforeRemove',\n target: route.key,\n data: { action: beforeRemoveAction },\n canPreventDefault: true,\n });\n\n if (event.defaultPrevented) {\n return true;\n }\n }\n\n return false;\n};\n\nexport default function useOnPreventRemove({\n getState,\n emitter,\n beforeRemoveListeners,\n}: Options) {\n const { addKeyedListener } = React.useContext(NavigationBuilderContext);\n const route = React.useContext(NavigationRouteContext);\n const routeKey = route?.key;\n\n React.useEffect(() => {\n if (routeKey) {\n return addKeyedListener?.('beforeRemove', routeKey, (action) => {\n const state = getState();\n\n return shouldPreventRemove(\n emitter,\n beforeRemoveListeners,\n state.routes,\n [],\n action\n );\n });\n }\n }, [addKeyedListener, beforeRemoveListeners, emitter, getState, routeKey]);\n}\n"],"mappings":"AAIA,OAAO,KAAKA,KAAZ,MAAuB,OAAvB;AAEA,OAAOC,wBAAP,MAEO,4BAFP;AAGA,OAAOC,sBAAP,MAAmC,0BAAnC;AAUA,MAAMC,kBAAkB,GAAGC,MAAM,CAAC,oBAAD,CAAjC;AAEA,OAAO,MAAMC,mBAAmB,GAAG,CACjCC,OADiC,EAEjCC,qBAFiC,EAGjCC,aAHiC,EAIjCC,UAJiC,EAKjCC,MALiC,KAM9B;EAAA;;EACH,MAAMC,aAAa,GAAGF,UAAU,CAACG,GAAX,CAAgBC,KAAD,IAAWA,KAAK,CAACC,GAAhC,CAAtB,CADG,CAGH;;EACA,MAAMC,aAAa,GAAGP,aAAa,CAChCQ,MADmB,CACXH,KAAD,IAAW,CAACF,aAAa,CAACM,QAAd,CAAuBJ,KAAK,CAACC,GAA7B,CADA,EAEnBI,OAFmB,EAAtB;EAIA,MAAMC,gBAA6B,GACjC;EADiC,yBAEjCT,MAAM,CAACP,kBAAD,CAF2B,yEAEH,IAAIiB,GAAJ,EAFhC;EAIA,MAAMC,kBAAkB,GAAG,EACzB,GAAGX,MADs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