{"ast":null,"code":"import _defineProperty from \"@babel/runtime/helpers/defineProperty\";\nfunction ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }\nfunction _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }\nimport * as React from 'react';\nimport NavigationBuilderContext from \"./NavigationBuilderContext\";\nimport NavigationRouteContext from \"./NavigationRouteContext\";\nvar VISITED_ROUTE_KEYS = Symbol('VISITED_ROUTE_KEYS');\nexport var shouldPreventRemove = function shouldPreventRemove(emitter, beforeRemoveListeners, currentRoutes, nextRoutes, action) {\n var _action$VISITED_ROUTE;\n var nextRouteKeys = nextRoutes.map(function (route) {\n return route.key;\n });\n var removedRoutes = currentRoutes.filter(function (route) {\n return !nextRouteKeys.includes(route.key);\n }).reverse();\n var visitedRouteKeys = (_action$VISITED_ROUTE = action[VISITED_ROUTE_KEYS]) != null ? _action$VISITED_ROUTE : new Set();\n var beforeRemoveAction = _objectSpread(_objectSpread({}, action), {}, _defineProperty({}, VISITED_ROUTE_KEYS, visitedRouteKeys));\n for (var route of removedRoutes) {\n var _beforeRemoveListener;\n if (visitedRouteKeys.has(route.key)) {\n continue;\n }\n var isPrevented = (_beforeRemoveListener = beforeRemoveListeners[route.key]) === null || _beforeRemoveListener === void 0 ? void 0 : _beforeRemoveListener.call(beforeRemoveListeners, beforeRemoveAction);\n if (isPrevented) {\n return true;\n }\n visitedRouteKeys.add(route.key);\n var event = emitter.emit({\n type: 'beforeRemove',\n target: route.key,\n data: {\n action: beforeRemoveAction\n },\n canPreventDefault: true\n });\n if (event.defaultPrevented) {\n return true;\n }\n }\n return false;\n};\nexport default function useOnPreventRemove(_ref) {\n var getState = _ref.getState,\n emitter = _ref.emitter,\n beforeRemoveListeners = _ref.beforeRemoveListeners;\n var _React$useContext = React.useContext(NavigationBuilderContext),\n addKeyedListener = _React$useContext.addKeyedListener;\n var route = React.useContext(NavigationRouteContext);\n var routeKey = route === null || route === void 0 ? void 0 : route.key;\n React.useEffect(function () {\n if (routeKey) {\n return addKeyedListener === null || addKeyedListener === void 0 ? void 0 : addKeyedListener('beforeRemove', routeKey, function (action) {\n var state = getState();\n return shouldPreventRemove(emitter, beforeRemoveListeners, state.routes, [], action);\n });\n }\n }, [addKeyedListener, beforeRemoveListeners, emitter, getState, routeKey]);\n}","map":{"version":3,"sources":["useOnPreventRemove.tsx"],"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"],"mappings":";;;AAIA,OAAO,KAAKA,KAAK,MAAM,OAAO;AAE9B,OAAOC,wBAAwB;AAG/B,OAAOC,sBAAsB;AAU7B,IAAMC,kBAAkB,GAAGC,MAAM,CAAC,oBAAoB,CAAC;AAEvD,OAAO,IAAMC,mBAAmB,GAAG,SAAtBA,mBAAmB,CAC9BC,OAAkD,EAClDC,qBAA4E,EAC5EC,aAAgC,EAChCC,UAA0C,EAC1CC,MAAwB,EACrB;EAAA;EACH,IAAMC,aAAa,GAAGF,UAAU,CAACG,GAAG,CAAEC,UAAAA,KAAK;IAAA,OAAKA,KAAK,CAACC,GAAG;EAAA,EAAC;E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AAO;IACPC,qBAAAA,GACQ,IAAA,CADRA,qBAAAA;EAEA,wBAA6BP,KAAK,CAACmC,UAAU,CAAClC,wBAAwB,CAAC;IAA/DiC,gBAAAA,qBAAAA,gBAAAA;EACR,IAAMrB,KAAK,GAAGb,KAAK,CAACmC,UAAU,CAACjC,sBAAsB,CAAC;EACtD,IAAMkC,QAAQ,GAAGvB,KAAK,KAAA,IAAA,IAALA,KAAK,KAAA,KAAA,CAAA,GAAA,KAAA,CAAA,GAALA,KAAK,CAAEC,GAAG;EAE3Bd,KAAK,CAACqC,SAAS,CAAC,YAAM;IACpB,IAAID,QAAQ,EAAE;MACZ,OAAOF,gBAAgB,KAAA,IAAA,IAAhBA,gBAAgB,KAAA,KAAA,CAAA,GAAA,KAAA,CAAA,GAAhBA,gBAAgB,CAAG,cAAc,EAAEE,QAAQ,EAAG1B,UAAAA,MAAM,EAAK;QAC9D,IAAM4B,KAAK,GAAGL,QAAQ,EAAE;QAExB,OAAO5B,mBAAmB,CACxBC,OAAO,EACPC,qBAAqB,EACrB+B,KAAK,CAACC,MAAM,EACZ,EAAE,EACF7B,MAAM,CACP;MACH,CAAC,CAAC;IACJ;EACF,CAAC,EAAE,CAACwB,gBAAgB,EAAE3B,qBAAqB,EAAED,OAAO,EAAE2B,QAAQ,EAAEG,QAAQ,CAAC,CAAC;AAC5E","sourceRoot":"../../src","sourcesContent":["import * as React from 'react';\nimport NavigationBuilderContext from './NavigationBuilderContext';\nimport NavigationRouteContext from './NavigationRouteContext';\nconst VISITED_ROUTE_KEYS = Symbol('VISITED_ROUTE_KEYS');\nexport const shouldPreventRemove = (emitter, beforeRemoveListeners, currentRoutes, nextRoutes, action) => {\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.filter(route => !nextRouteKeys.includes(route.key)).reverse();\n const visitedRouteKeys =\n // @ts-expect-error: add this property to mark that we've already emitted this action\n action[VISITED_ROUTE_KEYS] ?? new Set();\n const beforeRemoveAction = {\n ...action,\n [VISITED_ROUTE_KEYS]: visitedRouteKeys\n };\n for (const route of removedRoutes) {\n var _beforeRemoveListener;\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 = (_beforeRemoveListener = beforeRemoveListeners[route.key]) === null || _beforeRemoveListener === void 0 ? void 0 : _beforeRemoveListener.call(beforeRemoveListeners, beforeRemoveAction);\n if (isPrevented) {\n return true;\n }\n visitedRouteKeys.add(route.key);\n const event = emitter.emit({\n type: 'beforeRemove',\n target: route.key,\n data: {\n action: beforeRemoveAction\n },\n canPreventDefault: true\n });\n if (event.defaultPrevented) {\n return true;\n }\n }\n return false;\n};\nexport default function useOnPreventRemove(_ref) {\n let {\n getState,\n emitter,\n beforeRemoveListeners\n } = _ref;\n const {\n addKeyedListener\n } = React.useContext(NavigationBuilderContext);\n const route = React.useContext(NavigationRouteContext);\n const routeKey = route === null || route === void 0 ? void 0 : route.key;\n React.useEffect(() => {\n if (routeKey) {\n return addKeyedListener === null || addKeyedListener === void 0 ? void 0 : addKeyedListener('beforeRemove', routeKey, action => {\n const state = getState();\n return shouldPreventRemove(emitter, beforeRemoveListeners, state.routes, [], action);\n });\n }\n }, [addKeyedListener, beforeRemoveListeners, emitter, getState, routeKey]);\n}\n//# sourceMappingURL=useOnPreventRemove.js.map"]},"metadata":{},"sourceType":"module"}