@react-navigation/core 6.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +38 -0
- package/lib/commonjs/BaseNavigationContainer.js +393 -0
- package/lib/commonjs/BaseNavigationContainer.js.map +1 -0
- package/lib/commonjs/CurrentRenderContext.js +21 -0
- package/lib/commonjs/CurrentRenderContext.js.map +1 -0
- package/lib/commonjs/EnsureSingleNavigator.js +53 -0
- package/lib/commonjs/EnsureSingleNavigator.js.map +1 -0
- package/lib/commonjs/Group.js +15 -0
- package/lib/commonjs/Group.js.map +1 -0
- package/lib/commonjs/NavigationBuilderContext.js +23 -0
- package/lib/commonjs/NavigationBuilderContext.js.map +1 -0
- package/lib/commonjs/NavigationContainerRefContext.js +20 -0
- package/lib/commonjs/NavigationContainerRefContext.js.map +1 -0
- package/lib/commonjs/NavigationContext.js +20 -0
- package/lib/commonjs/NavigationContext.js.map +1 -0
- package/lib/commonjs/NavigationHelpersContext.js +21 -0
- package/lib/commonjs/NavigationHelpersContext.js.map +1 -0
- package/lib/commonjs/NavigationRouteContext.js +20 -0
- package/lib/commonjs/NavigationRouteContext.js.map +1 -0
- package/lib/commonjs/NavigationStateContext.js +42 -0
- package/lib/commonjs/NavigationStateContext.js.map +1 -0
- package/lib/commonjs/SceneView.js +97 -0
- package/lib/commonjs/SceneView.js.map +1 -0
- package/lib/commonjs/Screen.js +15 -0
- package/lib/commonjs/Screen.js.map +1 -0
- package/lib/commonjs/StaticContainer.js +43 -0
- package/lib/commonjs/StaticContainer.js.map +1 -0
- package/lib/commonjs/UnhandledActionContext.js +17 -0
- package/lib/commonjs/UnhandledActionContext.js.map +1 -0
- package/lib/commonjs/checkDuplicateRouteNames.js +31 -0
- package/lib/commonjs/checkDuplicateRouteNames.js.map +1 -0
- package/lib/commonjs/checkSerializable.js +59 -0
- package/lib/commonjs/checkSerializable.js.map +1 -0
- package/lib/commonjs/createNavigationContainerRef.js +82 -0
- package/lib/commonjs/createNavigationContainerRef.js.map +1 -0
- package/lib/commonjs/createNavigatorFactory.js +34 -0
- package/lib/commonjs/createNavigatorFactory.js.map +1 -0
- package/lib/commonjs/findFocusedRoute.js +22 -0
- package/lib/commonjs/findFocusedRoute.js.map +1 -0
- package/lib/commonjs/fromEntries.js +19 -0
- package/lib/commonjs/fromEntries.js.map +1 -0
- package/lib/commonjs/getActionFromState.js +99 -0
- package/lib/commonjs/getActionFromState.js.map +1 -0
- package/lib/commonjs/getFocusedRouteNameFromRoute.js +23 -0
- package/lib/commonjs/getFocusedRouteNameFromRoute.js.map +1 -0
- package/lib/commonjs/getPathFromState.js +238 -0
- package/lib/commonjs/getPathFromState.js.map +1 -0
- package/lib/commonjs/getStateFromPath.js +450 -0
- package/lib/commonjs/getStateFromPath.js.map +1 -0
- package/lib/commonjs/index.js +227 -0
- package/lib/commonjs/index.js.map +1 -0
- package/lib/commonjs/isArrayEqual.js +15 -0
- package/lib/commonjs/isArrayEqual.js.map +1 -0
- package/lib/commonjs/types.js +18 -0
- package/lib/commonjs/types.js.map +1 -0
- package/lib/commonjs/useChildListeners.js +38 -0
- package/lib/commonjs/useChildListeners.js.map +1 -0
- package/lib/commonjs/useComponent.js +36 -0
- package/lib/commonjs/useComponent.js.map +1 -0
- package/lib/commonjs/useCurrentRender.js +33 -0
- package/lib/commonjs/useCurrentRender.js.map +1 -0
- package/lib/commonjs/useDescriptors.js +141 -0
- package/lib/commonjs/useDescriptors.js.map +1 -0
- package/lib/commonjs/useEventEmitter.js +118 -0
- package/lib/commonjs/useEventEmitter.js.map +1 -0
- package/lib/commonjs/useFocusEffect.js +97 -0
- package/lib/commonjs/useFocusEffect.js.map +1 -0
- package/lib/commonjs/useFocusEvents.js +77 -0
- package/lib/commonjs/useFocusEvents.js.map +1 -0
- package/lib/commonjs/useFocusedListenersChildrenAdapter.js +57 -0
- package/lib/commonjs/useFocusedListenersChildrenAdapter.js.map +1 -0
- package/lib/commonjs/useIsFocused.js +47 -0
- package/lib/commonjs/useIsFocused.js.map +1 -0
- package/lib/commonjs/useKeyedChildListeners.js +35 -0
- package/lib/commonjs/useKeyedChildListeners.js.map +1 -0
- package/lib/commonjs/useNavigation.js +36 -0
- package/lib/commonjs/useNavigation.js.map +1 -0
- package/lib/commonjs/useNavigationBuilder.js +480 -0
- package/lib/commonjs/useNavigationBuilder.js.map +1 -0
- package/lib/commonjs/useNavigationCache.js +122 -0
- package/lib/commonjs/useNavigationCache.js.map +1 -0
- package/lib/commonjs/useNavigationContainerRef.js +27 -0
- package/lib/commonjs/useNavigationContainerRef.js.map +1 -0
- package/lib/commonjs/useNavigationHelpers.js +77 -0
- package/lib/commonjs/useNavigationHelpers.js.map +1 -0
- package/lib/commonjs/useNavigationState.js +41 -0
- package/lib/commonjs/useNavigationState.js.map +1 -0
- package/lib/commonjs/useOnAction.js +118 -0
- package/lib/commonjs/useOnAction.js.map +1 -0
- package/lib/commonjs/useOnGetState.js +60 -0
- package/lib/commonjs/useOnGetState.js.map +1 -0
- package/lib/commonjs/useOnPreventRemove.js +89 -0
- package/lib/commonjs/useOnPreventRemove.js.map +1 -0
- package/lib/commonjs/useOnRouteFocus.js +45 -0
- package/lib/commonjs/useOnRouteFocus.js.map +1 -0
- package/lib/commonjs/useOptionsGetters.js +99 -0
- package/lib/commonjs/useOptionsGetters.js.map +1 -0
- package/lib/commonjs/useRegisterNavigator.js +40 -0
- package/lib/commonjs/useRegisterNavigator.js.map +1 -0
- package/lib/commonjs/useRoute.js +32 -0
- package/lib/commonjs/useRoute.js.map +1 -0
- package/lib/commonjs/useRouteCache.js +61 -0
- package/lib/commonjs/useRouteCache.js.map +1 -0
- package/lib/commonjs/useScheduleUpdate.js +44 -0
- package/lib/commonjs/useScheduleUpdate.js.map +1 -0
- package/lib/commonjs/useSyncState.js +76 -0
- package/lib/commonjs/useSyncState.js.map +1 -0
- package/lib/commonjs/validatePathConfig.js +31 -0
- package/lib/commonjs/validatePathConfig.js.map +1 -0
- package/lib/module/BaseNavigationContainer.js +360 -0
- package/lib/module/BaseNavigationContainer.js.map +1 -0
- package/lib/module/CurrentRenderContext.js +9 -0
- package/lib/module/CurrentRenderContext.js.map +1 -0
- package/lib/module/EnsureSingleNavigator.js +38 -0
- package/lib/module/EnsureSingleNavigator.js.map +1 -0
- package/lib/module/Group.js +8 -0
- package/lib/module/Group.js.map +1 -0
- package/lib/module/NavigationBuilderContext.js +11 -0
- package/lib/module/NavigationBuilderContext.js.map +1 -0
- package/lib/module/NavigationContainerRefContext.js +8 -0
- package/lib/module/NavigationContainerRefContext.js.map +1 -0
- package/lib/module/NavigationContext.js +8 -0
- package/lib/module/NavigationContext.js.map +1 -0
- package/lib/module/NavigationHelpersContext.js +9 -0
- package/lib/module/NavigationHelpersContext.js.map +1 -0
- package/lib/module/NavigationRouteContext.js +8 -0
- package/lib/module/NavigationRouteContext.js.map +1 -0
- package/lib/module/NavigationStateContext.js +27 -0
- package/lib/module/NavigationStateContext.js.map +1 -0
- package/lib/module/SceneView.js +80 -0
- package/lib/module/SceneView.js.map +1 -0
- package/lib/module/Screen.js +8 -0
- package/lib/module/Screen.js.map +1 -0
- package/lib/module/StaticContainer.js +30 -0
- package/lib/module/StaticContainer.js.map +1 -0
- package/lib/module/UnhandledActionContext.js +4 -0
- package/lib/module/UnhandledActionContext.js.map +1 -0
- package/lib/module/checkDuplicateRouteNames.js +24 -0
- package/lib/module/checkDuplicateRouteNames.js.map +1 -0
- package/lib/module/checkSerializable.js +52 -0
- package/lib/module/checkSerializable.js.map +1 -0
- package/lib/module/createNavigationContainerRef.js +71 -0
- package/lib/module/createNavigationContainerRef.js.map +1 -0
- package/lib/module/createNavigatorFactory.js +24 -0
- package/lib/module/createNavigatorFactory.js.map +1 -0
- package/lib/module/findFocusedRoute.js +15 -0
- package/lib/module/findFocusedRoute.js.map +1 -0
- package/lib/module/fromEntries.js +12 -0
- package/lib/module/fromEntries.js.map +1 -0
- package/lib/module/getActionFromState.js +92 -0
- package/lib/module/getActionFromState.js.map +1 -0
- package/lib/module/getFocusedRouteNameFromRoute.js +15 -0
- package/lib/module/getFocusedRouteNameFromRoute.js.map +1 -0
- package/lib/module/getPathFromState.js +223 -0
- package/lib/module/getPathFromState.js.map +1 -0
- package/lib/module/getStateFromPath.js +434 -0
- package/lib/module/getStateFromPath.js.map +1 -0
- package/lib/module/index.js +24 -0
- package/lib/module/index.js.map +1 -0
- package/lib/module/isArrayEqual.js +8 -0
- package/lib/module/isArrayEqual.js.map +1 -0
- package/lib/module/types.js +9 -0
- package/lib/module/types.js.map +1 -0
- package/lib/module/useChildListeners.js +27 -0
- package/lib/module/useChildListeners.js.map +1 -0
- package/lib/module/useComponent.js +24 -0
- package/lib/module/useComponent.js.map +1 -0
- package/lib/module/useCurrentRender.js +19 -0
- package/lib/module/useCurrentRender.js.map +1 -0
- package/lib/module/useDescriptors.js +122 -0
- package/lib/module/useDescriptors.js.map +1 -0
- package/lib/module/useEventEmitter.js +107 -0
- package/lib/module/useEventEmitter.js.map +1 -0
- package/lib/module/useFocusEffect.js +83 -0
- package/lib/module/useFocusEffect.js.map +1 -0
- package/lib/module/useFocusEvents.js +63 -0
- package/lib/module/useFocusEvents.js.map +1 -0
- package/lib/module/useFocusedListenersChildrenAdapter.js +43 -0
- package/lib/module/useFocusedListenersChildrenAdapter.js.map +1 -0
- package/lib/module/useIsFocused.js +34 -0
- package/lib/module/useIsFocused.js.map +1 -0
- package/lib/module/useKeyedChildListeners.js +24 -0
- package/lib/module/useKeyedChildListeners.js.map +1 -0
- package/lib/module/useNavigation.js +21 -0
- package/lib/module/useNavigation.js.map +1 -0
- package/lib/module/useNavigationBuilder.js +443 -0
- package/lib/module/useNavigationBuilder.js.map +1 -0
- package/lib/module/useNavigationCache.js +107 -0
- package/lib/module/useNavigationCache.js.map +1 -0
- package/lib/module/useNavigationContainerRef.js +12 -0
- package/lib/module/useNavigationContainerRef.js.map +1 -0
- package/lib/module/useNavigationHelpers.js +59 -0
- package/lib/module/useNavigationHelpers.js.map +1 -0
- package/lib/module/useNavigationState.js +27 -0
- package/lib/module/useNavigationState.js.map +1 -0
- package/lib/module/useOnAction.js +103 -0
- package/lib/module/useOnAction.js.map +1 -0
- package/lib/module/useOnGetState.js +43 -0
- package/lib/module/useOnGetState.js.map +1 -0
- package/lib/module/useOnPreventRemove.js +68 -0
- package/lib/module/useOnPreventRemove.js.map +1 -0
- package/lib/module/useOnRouteFocus.js +31 -0
- package/lib/module/useOnRouteFocus.js.map +1 -0
- package/lib/module/useOptionsGetters.js +83 -0
- package/lib/module/useOptionsGetters.js.map +1 -0
- package/lib/module/useRegisterNavigator.js +27 -0
- package/lib/module/useRegisterNavigator.js.map +1 -0
- package/lib/module/useRoute.js +18 -0
- package/lib/module/useRoute.js.map +1 -0
- package/lib/module/useRouteCache.js +47 -0
- package/lib/module/useRouteCache.js.map +1 -0
- package/lib/module/useScheduleUpdate.js +29 -0
- package/lib/module/useScheduleUpdate.js.map +1 -0
- package/lib/module/useSyncState.js +64 -0
- package/lib/module/useSyncState.js.map +1 -0
- package/lib/module/validatePathConfig.js +24 -0
- package/lib/module/validatePathConfig.js.map +1 -0
- package/lib/typescript/src/BaseNavigationContainer.d.ts +14 -0
- package/lib/typescript/src/CurrentRenderContext.d.ts +9 -0
- package/lib/typescript/src/EnsureSingleNavigator.d.ts +13 -0
- package/lib/typescript/src/Group.d.ts +6 -0
- package/lib/typescript/src/NavigationBuilderContext.d.ts +34 -0
- package/lib/typescript/src/NavigationContainerRefContext.d.ts +8 -0
- package/lib/typescript/src/NavigationContext.d.ts +24 -0
- package/lib/typescript/src/NavigationHelpersContext.d.ts +9 -0
- package/lib/typescript/src/NavigationRouteContext.d.ts +7 -0
- package/lib/typescript/src/NavigationStateContext.d.ts +45 -0
- package/lib/typescript/src/SceneView.d.ts +21 -0
- package/lib/typescript/src/Screen.d.ts +6 -0
- package/lib/typescript/src/StaticContainer.d.ts +7 -0
- package/lib/typescript/src/UnhandledActionContext.d.ts +4 -0
- package/lib/typescript/src/checkDuplicateRouteNames.d.ts +2 -0
- package/lib/typescript/src/checkSerializable.d.ts +9 -0
- package/lib/typescript/src/createNavigationContainerRef.d.ts +3 -0
- package/lib/typescript/src/createNavigatorFactory.d.ts +11 -0
- package/lib/typescript/src/findFocusedRoute.d.ts +22 -0
- package/lib/typescript/src/fromEntries.d.ts +1 -0
- package/lib/typescript/src/getActionFromState.d.ts +16 -0
- package/lib/typescript/src/getFocusedRouteNameFromRoute.d.ts +2 -0
- package/lib/typescript/src/getPathFromState.d.ts +38 -0
- package/lib/typescript/src/getStateFromPath.d.ts +32 -0
- package/lib/typescript/src/index.d.ts +23 -0
- package/lib/typescript/src/isArrayEqual.d.ts +5 -0
- package/lib/typescript/src/types.d.ts +485 -0
- package/lib/typescript/src/useChildListeners.d.ts +11 -0
- package/lib/typescript/src/useComponent.d.ts +2 -0
- package/lib/typescript/src/useCurrentRender.d.ts +13 -0
- package/lib/typescript/src/useDescriptors.d.ts +89 -0
- package/lib/typescript/src/useEventEmitter.d.ts +8 -0
- package/lib/typescript/src/useFocusEffect.d.ts +10 -0
- package/lib/typescript/src/useFocusEvents.d.ts +12 -0
- package/lib/typescript/src/useFocusedListenersChildrenAdapter.d.ts +12 -0
- package/lib/typescript/src/useIsFocused.d.ts +5 -0
- package/lib/typescript/src/useKeyedChildListeners.d.ts +11 -0
- package/lib/typescript/src/useNavigation.d.ts +7 -0
- package/lib/typescript/src/useNavigationBuilder.d.ts +223 -0
- package/lib/typescript/src/useNavigationCache.d.ts +19 -0
- package/lib/typescript/src/useNavigationContainerRef.d.ts +2 -0
- package/lib/typescript/src/useNavigationHelpers.d.ts +174 -0
- package/lib/typescript/src/useNavigationState.d.ts +9 -0
- package/lib/typescript/src/useOnAction.d.ts +25 -0
- package/lib/typescript/src/useOnGetState.d.ts +8 -0
- package/lib/typescript/src/useOnPreventRemove.d.ts +16 -0
- package/lib/typescript/src/useOnRouteFocus.d.ts +14 -0
- package/lib/typescript/src/useOptionsGetters.d.ts +12 -0
- package/lib/typescript/src/useRegisterNavigator.d.ts +5 -0
- package/lib/typescript/src/useRoute.d.ts +8 -0
- package/lib/typescript/src/useRouteCache.d.ts +13 -0
- package/lib/typescript/src/useScheduleUpdate.d.ts +13 -0
- package/lib/typescript/src/useSyncState.d.ts +4 -0
- package/lib/typescript/src/validatePathConfig.d.ts +1 -0
- package/package.json +73 -0
- package/src/BaseNavigationContainer.tsx +453 -0
- package/src/CurrentRenderContext.tsx +10 -0
- package/src/EnsureSingleNavigator.tsx +53 -0
- package/src/Group.tsx +14 -0
- package/src/NavigationBuilderContext.tsx +70 -0
- package/src/NavigationContainerRefContext.tsx +14 -0
- package/src/NavigationContext.tsx +12 -0
- package/src/NavigationHelpersContext.tsx +13 -0
- package/src/NavigationRouteContext.tsx +10 -0
- package/src/NavigationStateContext.tsx +39 -0
- package/src/SceneView.tsx +134 -0
- package/src/Screen.tsx +17 -0
- package/src/StaticContainer.tsx +29 -0
- package/src/UnhandledActionContext.tsx +9 -0
- package/src/checkDuplicateRouteNames.tsx +33 -0
- package/src/checkSerializable.tsx +74 -0
- package/src/createNavigationContainerRef.tsx +97 -0
- package/src/createNavigatorFactory.tsx +40 -0
- package/src/findFocusedRoute.tsx +13 -0
- package/src/fromEntries.tsx +13 -0
- package/src/getActionFromState.tsx +154 -0
- package/src/getFocusedRouteNameFromRoute.tsx +28 -0
- package/src/getPathFromState.tsx +297 -0
- package/src/getStateFromPath.tsx +575 -0
- package/src/index.tsx +23 -0
- package/src/isArrayEqual.tsx +7 -0
- package/src/types.tsx +645 -0
- package/src/useChildListeners.tsx +37 -0
- package/src/useComponent.tsx +30 -0
- package/src/useCurrentRender.tsx +35 -0
- package/src/useDescriptors.tsx +229 -0
- package/src/useEventEmitter.tsx +130 -0
- package/src/useFocusEffect.tsx +112 -0
- package/src/useFocusEvents.tsx +73 -0
- package/src/useFocusedListenersChildrenAdapter.tsx +47 -0
- package/src/useIsFocused.tsx +43 -0
- package/src/useKeyedChildListeners.tsx +40 -0
- package/src/useNavigation.tsx +26 -0
- package/src/useNavigationBuilder.tsx +640 -0
- package/src/useNavigationCache.tsx +159 -0
- package/src/useNavigationContainerRef.tsx +17 -0
- package/src/useNavigationHelpers.tsx +98 -0
- package/src/useNavigationState.tsx +41 -0
- package/src/useOnAction.tsx +166 -0
- package/src/useOnGetState.tsx +47 -0
- package/src/useOnPreventRemove.tsx +99 -0
- package/src/useOnRouteFocus.tsx +47 -0
- package/src/useOptionsGetters.tsx +98 -0
- package/src/useRegisterNavigator.tsx +29 -0
- package/src/useRoute.tsx +22 -0
- package/src/useRouteCache.tsx +55 -0
- package/src/useScheduleUpdate.tsx +32 -0
- package/src/useSyncState.tsx +74 -0
- package/src/validatePathConfig.tsx +32 -0
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.default = useNavigationCache;
|
|
7
|
+
|
|
8
|
+
var _routers = require("@react-navigation/routers");
|
|
9
|
+
|
|
10
|
+
var React = _interopRequireWildcard(require("react"));
|
|
11
|
+
|
|
12
|
+
var _NavigationBuilderContext = _interopRequireDefault(require("./NavigationBuilderContext"));
|
|
13
|
+
|
|
14
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
15
|
+
|
|
16
|
+
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
|
|
17
|
+
|
|
18
|
+
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Hook to cache navigation objects for each screen in the navigator.
|
|
22
|
+
* It's important to cache them to make sure navigation objects don't change between renders.
|
|
23
|
+
* This lets us apply optimizations like `React.memo` to minimize re-rendering screens.
|
|
24
|
+
*/
|
|
25
|
+
function useNavigationCache({
|
|
26
|
+
state,
|
|
27
|
+
getState,
|
|
28
|
+
navigation,
|
|
29
|
+
setOptions,
|
|
30
|
+
router,
|
|
31
|
+
emitter
|
|
32
|
+
}) {
|
|
33
|
+
const {
|
|
34
|
+
stackRef
|
|
35
|
+
} = React.useContext(_NavigationBuilderContext.default); // Cache object which holds navigation objects for each screen
|
|
36
|
+
// We use `React.useMemo` instead of `React.useRef` coz we want to invalidate it when deps change
|
|
37
|
+
// In reality, these deps will rarely change, if ever
|
|
38
|
+
|
|
39
|
+
const cache = React.useMemo(() => ({
|
|
40
|
+
current: {}
|
|
41
|
+
}), // eslint-disable-next-line react-hooks/exhaustive-deps
|
|
42
|
+
[getState, navigation, setOptions, router, emitter]);
|
|
43
|
+
const actions = { ...router.actionCreators,
|
|
44
|
+
..._routers.CommonActions
|
|
45
|
+
};
|
|
46
|
+
cache.current = state.routes.reduce((acc, route) => {
|
|
47
|
+
const previous = cache.current[route.key];
|
|
48
|
+
|
|
49
|
+
if (previous) {
|
|
50
|
+
// If a cached navigation object already exists, reuse it
|
|
51
|
+
acc[route.key] = previous;
|
|
52
|
+
} else {
|
|
53
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
54
|
+
const {
|
|
55
|
+
emit,
|
|
56
|
+
...rest
|
|
57
|
+
} = navigation;
|
|
58
|
+
|
|
59
|
+
const dispatch = thunk => {
|
|
60
|
+
const action = typeof thunk === 'function' ? thunk(getState()) : thunk;
|
|
61
|
+
|
|
62
|
+
if (action != null) {
|
|
63
|
+
navigation.dispatch({
|
|
64
|
+
source: route.key,
|
|
65
|
+
...action
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
const withStack = callback => {
|
|
71
|
+
let isStackSet = false;
|
|
72
|
+
|
|
73
|
+
try {
|
|
74
|
+
if (process.env.NODE_ENV !== 'production' && stackRef && !stackRef.current) {
|
|
75
|
+
// Capture the stack trace for devtools
|
|
76
|
+
stackRef.current = new Error().stack;
|
|
77
|
+
isStackSet = true;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
callback();
|
|
81
|
+
} finally {
|
|
82
|
+
if (isStackSet && stackRef) {
|
|
83
|
+
stackRef.current = undefined;
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
};
|
|
87
|
+
|
|
88
|
+
const helpers = Object.keys(actions).reduce((acc, name) => {
|
|
89
|
+
acc[name] = (...args) => withStack(() => // @ts-expect-error: name is a valid key, but TypeScript is dumb
|
|
90
|
+
dispatch(actions[name](...args)));
|
|
91
|
+
|
|
92
|
+
return acc;
|
|
93
|
+
}, {});
|
|
94
|
+
acc[route.key] = { ...rest,
|
|
95
|
+
...helpers,
|
|
96
|
+
// FIXME: too much work to fix the types for now
|
|
97
|
+
...emitter.create(route.key),
|
|
98
|
+
dispatch: thunk => withStack(() => dispatch(thunk)),
|
|
99
|
+
setOptions: options => setOptions(o => ({ ...o,
|
|
100
|
+
[route.key]: { ...o[route.key],
|
|
101
|
+
...options
|
|
102
|
+
}
|
|
103
|
+
})),
|
|
104
|
+
isFocused: () => {
|
|
105
|
+
const state = getState();
|
|
106
|
+
|
|
107
|
+
if (state.routes[state.index].key !== route.key) {
|
|
108
|
+
return false;
|
|
109
|
+
} // If the current screen is focused, we also need to check if parent navigator is focused
|
|
110
|
+
// This makes sure that we return the focus state in the whole tree, not just this navigator
|
|
111
|
+
|
|
112
|
+
|
|
113
|
+
return navigation ? navigation.isFocused() : true;
|
|
114
|
+
}
|
|
115
|
+
};
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
return acc;
|
|
119
|
+
}, {});
|
|
120
|
+
return cache.current;
|
|
121
|
+
}
|
|
122
|
+
//# sourceMappingURL=useNavigationCache.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["useNavigationCache.tsx"],"names":["useNavigationCache","state","getState","navigation","setOptions","router","emitter","stackRef","React","useContext","NavigationBuilderContext","cache","useMemo","current","actions","actionCreators","CommonActions","routes","reduce","acc","route","previous","key","emit","rest","dispatch","thunk","action","source","withStack","callback","isStackSet","process","env","NODE_ENV","Error","stack","undefined","helpers","Object","keys","name","args","create","options","o","isFocused","index"],"mappings":"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","sourcesContent":["import {\n CommonActions,\n NavigationAction,\n NavigationState,\n ParamListBase,\n Router,\n} from '@react-navigation/routers';\nimport * as React from 'react';\n\nimport NavigationBuilderContext from './NavigationBuilderContext';\nimport type { NavigationHelpers, NavigationProp } from './types';\nimport type { NavigationEventEmitter } from './useEventEmitter';\n\ntype Options<\n State extends NavigationState,\n EventMap extends Record<string, any>\n> = {\n state: State;\n getState: () => State;\n navigation: NavigationHelpers<ParamListBase> &\n Partial<NavigationProp<ParamListBase, string, any, any, any>>;\n setOptions: (\n cb: (options: Record<string, object>) => Record<string, object>\n ) => void;\n router: Router<State, NavigationAction>;\n emitter: NavigationEventEmitter<EventMap>;\n};\n\ntype NavigationCache<\n State extends NavigationState,\n ScreenOptions extends {},\n EventMap extends Record<string, any>\n> = Record<\n string,\n NavigationProp<ParamListBase, string, State, ScreenOptions, EventMap>\n>;\n\n/**\n * Hook to cache navigation objects for each screen in the navigator.\n * It's important to cache them to make sure navigation objects don't change between renders.\n * This lets us apply optimizations like `React.memo` to minimize re-rendering screens.\n */\nexport default function useNavigationCache<\n State extends NavigationState,\n ScreenOptions extends {},\n EventMap extends Record<string, any>\n>({\n state,\n getState,\n navigation,\n setOptions,\n router,\n emitter,\n}: Options<State, EventMap>) {\n const { stackRef } = React.useContext(NavigationBuilderContext);\n\n // Cache object which holds navigation objects for each screen\n // We use `React.useMemo` instead of `React.useRef` coz we want to invalidate it when deps change\n // In reality, these deps will rarely change, if ever\n const cache = React.useMemo(\n () => ({ current: {} as NavigationCache<State, ScreenOptions, EventMap> }),\n // eslint-disable-next-line react-hooks/exhaustive-deps\n [getState, navigation, setOptions, router, emitter]\n );\n\n const actions = {\n ...router.actionCreators,\n ...CommonActions,\n };\n\n cache.current = state.routes.reduce<\n NavigationCache<State, ScreenOptions, EventMap>\n >((acc, route) => {\n const previous = cache.current[route.key];\n\n type Thunk =\n | NavigationAction\n | ((state: State) => NavigationAction | null | undefined);\n\n if (previous) {\n // If a cached navigation object already exists, reuse it\n acc[route.key] = previous;\n } else {\n // eslint-disable-next-line @typescript-eslint/no-unused-vars\n const { emit, ...rest } = navigation;\n\n const dispatch = (thunk: Thunk) => {\n const action = typeof thunk === 'function' ? thunk(getState()) : thunk;\n\n if (action != null) {\n navigation.dispatch({ source: route.key, ...action });\n }\n };\n\n const withStack = (callback: () => void) => {\n let isStackSet = false;\n\n try {\n if (\n process.env.NODE_ENV !== 'production' &&\n stackRef &&\n !stackRef.current\n ) {\n // Capture the stack trace for devtools\n stackRef.current = new Error().stack;\n isStackSet = true;\n }\n\n callback();\n } finally {\n if (isStackSet && stackRef) {\n stackRef.current = undefined;\n }\n }\n };\n\n const helpers = Object.keys(actions).reduce<Record<string, () => void>>(\n (acc, name) => {\n acc[name] = (...args: any) =>\n withStack(() =>\n // @ts-expect-error: name is a valid key, but TypeScript is dumb\n dispatch(actions[name](...args))\n );\n\n return acc;\n },\n {}\n );\n\n acc[route.key] = {\n ...rest,\n ...helpers,\n // FIXME: too much work to fix the types for now\n ...(emitter.create(route.key) as any),\n dispatch: (thunk: Thunk) => withStack(() => dispatch(thunk)),\n setOptions: (options: object) =>\n setOptions((o) => ({\n ...o,\n [route.key]: { ...o[route.key], ...options },\n })),\n isFocused: () => {\n const state = getState();\n\n if (state.routes[state.index].key !== route.key) {\n return false;\n }\n\n // If the current screen is focused, we also need to check if parent navigator is focused\n // This makes sure that we return the focus state in the whole tree, not just this navigator\n return navigation ? navigation.isFocused() : true;\n },\n };\n }\n\n return acc;\n }, {});\n\n return cache.current;\n}\n"]}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.default = useNavigationContainerRef;
|
|
7
|
+
|
|
8
|
+
var React = _interopRequireWildcard(require("react"));
|
|
9
|
+
|
|
10
|
+
var _createNavigationContainerRef = _interopRequireDefault(require("./createNavigationContainerRef"));
|
|
11
|
+
|
|
12
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
13
|
+
|
|
14
|
+
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
|
|
15
|
+
|
|
16
|
+
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
|
|
17
|
+
|
|
18
|
+
function useNavigationContainerRef() {
|
|
19
|
+
const navigation = React.useRef(null);
|
|
20
|
+
|
|
21
|
+
if (navigation.current == null) {
|
|
22
|
+
navigation.current = (0, _createNavigationContainerRef.default)();
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
return navigation.current;
|
|
26
|
+
}
|
|
27
|
+
//# sourceMappingURL=useNavigationContainerRef.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["useNavigationContainerRef.tsx"],"names":["useNavigationContainerRef","navigation","React","useRef","current"],"mappings":";;;;;;;AAAA;;AAEA;;;;;;;;AAGe,SAASA,yBAAT,GAEmC;AAChD,QAAMC,UAAU,GACdC,KAAK,CAACC,MAAN,CAAkE,IAAlE,CADF;;AAGA,MAAIF,UAAU,CAACG,OAAX,IAAsB,IAA1B,EAAgC;AAC9BH,IAAAA,UAAU,CAACG,OAAX,GAAqB,4CAArB;AACD;;AAED,SAAOH,UAAU,CAACG,OAAlB;AACD","sourcesContent":["import * as React from 'react';\n\nimport createNavigationContainerRef from './createNavigationContainerRef';\nimport type { NavigationContainerRefWithCurrent } from './types';\n\nexport default function useNavigationContainerRef<\n ParamList extends {} = ReactNavigation.RootParamList\n>(): NavigationContainerRefWithCurrent<ParamList> {\n const navigation =\n React.useRef<NavigationContainerRefWithCurrent<ParamList> | null>(null);\n\n if (navigation.current == null) {\n navigation.current = createNavigationContainerRef<ParamList>();\n }\n\n return navigation.current;\n}\n"]}
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.default = useNavigationHelpers;
|
|
7
|
+
|
|
8
|
+
var _routers = require("@react-navigation/routers");
|
|
9
|
+
|
|
10
|
+
var React = _interopRequireWildcard(require("react"));
|
|
11
|
+
|
|
12
|
+
var _NavigationContext = _interopRequireDefault(require("./NavigationContext"));
|
|
13
|
+
|
|
14
|
+
var _types = require("./types");
|
|
15
|
+
|
|
16
|
+
var _UnhandledActionContext = _interopRequireDefault(require("./UnhandledActionContext"));
|
|
17
|
+
|
|
18
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
19
|
+
|
|
20
|
+
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
|
|
21
|
+
|
|
22
|
+
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
|
|
23
|
+
|
|
24
|
+
// This is to make TypeScript compiler happy
|
|
25
|
+
// eslint-disable-next-line babel/no-unused-expressions
|
|
26
|
+
_types.PrivateValueStore;
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Navigation object with helper methods to be used by a navigator.
|
|
30
|
+
* This object includes methods for common actions as well as methods the parent screen's navigation object.
|
|
31
|
+
*/
|
|
32
|
+
function useNavigationHelpers({
|
|
33
|
+
onAction,
|
|
34
|
+
getState,
|
|
35
|
+
emitter,
|
|
36
|
+
router
|
|
37
|
+
}) {
|
|
38
|
+
const onUnhandledAction = React.useContext(_UnhandledActionContext.default);
|
|
39
|
+
const parentNavigationHelpers = React.useContext(_NavigationContext.default);
|
|
40
|
+
return React.useMemo(() => {
|
|
41
|
+
const dispatch = op => {
|
|
42
|
+
const action = typeof op === 'function' ? op(getState()) : op;
|
|
43
|
+
const handled = onAction(action);
|
|
44
|
+
|
|
45
|
+
if (!handled) {
|
|
46
|
+
onUnhandledAction === null || onUnhandledAction === void 0 ? void 0 : onUnhandledAction(action);
|
|
47
|
+
}
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
const actions = { ...router.actionCreators,
|
|
51
|
+
..._routers.CommonActions
|
|
52
|
+
};
|
|
53
|
+
const helpers = Object.keys(actions).reduce((acc, name) => {
|
|
54
|
+
// @ts-expect-error: name is a valid key, but TypeScript is dumb
|
|
55
|
+
acc[name] = (...args) => dispatch(actions[name](...args));
|
|
56
|
+
|
|
57
|
+
return acc;
|
|
58
|
+
}, {});
|
|
59
|
+
return { ...parentNavigationHelpers,
|
|
60
|
+
...helpers,
|
|
61
|
+
dispatch,
|
|
62
|
+
emit: emitter.emit,
|
|
63
|
+
isFocused: parentNavigationHelpers ? parentNavigationHelpers.isFocused : () => true,
|
|
64
|
+
canGoBack: () => {
|
|
65
|
+
const state = getState();
|
|
66
|
+
return router.getStateForAction(state, _routers.CommonActions.goBack(), {
|
|
67
|
+
routeNames: state.routeNames,
|
|
68
|
+
routeParamList: {},
|
|
69
|
+
routeGetIdList: {}
|
|
70
|
+
}) !== null || (parentNavigationHelpers === null || parentNavigationHelpers === void 0 ? void 0 : parentNavigationHelpers.canGoBack()) || false;
|
|
71
|
+
},
|
|
72
|
+
getParent: () => parentNavigationHelpers,
|
|
73
|
+
getState
|
|
74
|
+
};
|
|
75
|
+
}, [emitter.emit, getState, onAction, onUnhandledAction, parentNavigationHelpers, router]);
|
|
76
|
+
}
|
|
77
|
+
//# sourceMappingURL=useNavigationHelpers.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["useNavigationHelpers.tsx"],"names":["PrivateValueStore","useNavigationHelpers","onAction","getState","emitter","router","onUnhandledAction","React","useContext","UnhandledActionContext","parentNavigationHelpers","NavigationContext","useMemo","dispatch","op","action","handled","actions","actionCreators","CommonActions","helpers","Object","keys","reduce","acc","name","args","emit","isFocused","canGoBack","state","getStateForAction","goBack","routeNames","routeParamList","routeGetIdList","getParent"],"mappings":";;;;;;;AAAA;;AAOA;;AAEA;;AACA;;AACA;;;;;;;;AAGA;AACA;AACAA;;AASA;AACA;AACA;AACA;AACe,SAASC,oBAAT,CAKb;AAAEC,EAAAA,QAAF;AAAYC,EAAAA,QAAZ;AAAsBC,EAAAA,OAAtB;AAA+BC,EAAAA;AAA/B,CALa,EAKoD;AACjE,QAAMC,iBAAiB,GAAGC,KAAK,CAACC,UAAN,CAAiBC,+BAAjB,CAA1B;AACA,QAAMC,uBAAuB,GAAGH,KAAK,CAACC,UAAN,CAAiBG,0BAAjB,CAAhC;AAEA,SAAOJ,KAAK,CAACK,OAAN,CAAc,MAAM;AACzB,UAAMC,QAAQ,GAAIC,EAAD,IAA6C;AAC5D,YAAMC,MAAM,GAAG,OAAOD,EAAP,KAAc,UAAd,GAA2BA,EAAE,CAACX,QAAQ,EAAT,CAA7B,GAA4CW,EAA3D;AAEA,YAAME,OAAO,GAAGd,QAAQ,CAACa,MAAD,CAAxB;;AAEA,UAAI,CAACC,OAAL,EAAc;AACZV,QAAAA,iBAAiB,SAAjB,IAAAA,iBAAiB,WAAjB,YAAAA,iBAAiB,CAAGS,MAAH,CAAjB;AACD;AACF,KARD;;AAUA,UAAME,OAAO,GAAG,EACd,GAAGZ,MAAM,CAACa,cADI;AAEd,SAAGC;AAFW,KAAhB;AAKA,UAAMC,OAAO,GAAGC,MAAM,CAACC,IAAP,CAAYL,OAAZ,EAAqBM,MAArB,CACd,CAACC,GAAD,EAAMC,IAAN,KAAe;AACb;AACAD,MAAAA,GAAG,CAACC,IAAD,CAAH,GAAY,CAAC,GAAGC,IAAJ,KAAkBb,QAAQ,CAACI,OAAO,CAACQ,IAAD,CAAP,CAAc,GAAGC,IAAjB,CAAD,CAAtC;;AACA,aAAOF,GAAP;AACD,KALa,EAMd,EANc,CAAhB;AASA,WAAO,EACL,GAAGd,uBADE;AAEL,SAAGU,OAFE;AAGLP,MAAAA,QAHK;AAILc,MAAAA,IAAI,EAAEvB,OAAO,CAACuB,IAJT;AAKLC,MAAAA,SAAS,EAAElB,uBAAuB,GAC9BA,uBAAuB,CAACkB,SADM,GAE9B,MAAM,IAPL;AAQLC,MAAAA,SAAS,EAAE,MAAM;AACf,cAAMC,KAAK,GAAG3B,QAAQ,EAAtB;AAEA,eACEE,MAAM,CAAC0B,iBAAP,CAAyBD,KAAzB,EAAgCX,uBAAca,MAAd,EAAhC,EAAkE;AAChEC,UAAAA,UAAU,EAAEH,KAAK,CAACG,UAD8C;AAEhEC,UAAAA,cAAc,EAAE,EAFgD;AAGhEC,UAAAA,cAAc,EAAE;AAHgD,SAAlE,MAIO,IAJP,KAKAzB,uBALA,aAKAA,uBALA,uBAKAA,uBAAuB,CAAEmB,SAAzB,EALA,KAMA,KAPF;AASD,OApBI;AAqBLO,MAAAA,SAAS,EAAE,MAAM1B,uBArBZ;AAsBLP,MAAAA;AAtBK,KAAP;AA0BD,GAnDM,EAmDJ,CACDC,OAAO,CAACuB,IADP,EAEDxB,QAFC,EAGDD,QAHC,EAIDI,iBAJC,EAKDI,uBALC,EAMDL,MANC,CAnDI,CAAP;AA2DD","sourcesContent":["import {\n CommonActions,\n NavigationAction,\n NavigationState,\n ParamListBase,\n Router,\n} from '@react-navigation/routers';\nimport * as React from 'react';\n\nimport NavigationContext from './NavigationContext';\nimport { NavigationHelpers, NavigationProp, PrivateValueStore } from './types';\nimport UnhandledActionContext from './UnhandledActionContext';\nimport type { NavigationEventEmitter } from './useEventEmitter';\n\n// This is to make TypeScript compiler happy\n// eslint-disable-next-line babel/no-unused-expressions\nPrivateValueStore;\n\ntype Options<State extends NavigationState, Action extends NavigationAction> = {\n onAction: (action: NavigationAction) => boolean;\n getState: () => State;\n emitter: NavigationEventEmitter<any>;\n router: Router<State, Action>;\n};\n\n/**\n * Navigation object with helper methods to be used by a navigator.\n * This object includes methods for common actions as well as methods the parent screen's navigation object.\n */\nexport default function useNavigationHelpers<\n State extends NavigationState,\n ActionHelpers extends Record<string, () => void>,\n Action extends NavigationAction,\n EventMap extends Record<string, any>\n>({ onAction, getState, emitter, router }: Options<State, Action>) {\n const onUnhandledAction = React.useContext(UnhandledActionContext);\n const parentNavigationHelpers = React.useContext(NavigationContext);\n\n return React.useMemo(() => {\n const dispatch = (op: Action | ((state: State) => Action)) => {\n const action = typeof op === 'function' ? op(getState()) : op;\n\n const handled = onAction(action);\n\n if (!handled) {\n onUnhandledAction?.(action);\n }\n };\n\n const actions = {\n ...router.actionCreators,\n ...CommonActions,\n };\n\n const helpers = Object.keys(actions).reduce<Record<string, () => void>>(\n (acc, name) => {\n // @ts-expect-error: name is a valid key, but TypeScript is dumb\n acc[name] = (...args: any) => dispatch(actions[name](...args));\n return acc;\n },\n {}\n );\n\n return {\n ...parentNavigationHelpers,\n ...helpers,\n dispatch,\n emit: emitter.emit,\n isFocused: parentNavigationHelpers\n ? parentNavigationHelpers.isFocused\n : () => true,\n canGoBack: () => {\n const state = getState();\n\n return (\n router.getStateForAction(state, CommonActions.goBack() as Action, {\n routeNames: state.routeNames,\n routeParamList: {},\n routeGetIdList: {},\n }) !== null ||\n parentNavigationHelpers?.canGoBack() ||\n false\n );\n },\n getParent: () => parentNavigationHelpers as any,\n getState,\n } as NavigationHelpers<ParamListBase, EventMap> &\n (NavigationProp<ParamListBase, string, any, any, any> | undefined) &\n ActionHelpers;\n }, [\n emitter.emit,\n getState,\n onAction,\n onUnhandledAction,\n parentNavigationHelpers,\n router,\n ]);\n}\n"]}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.default = useNavigationState;
|
|
7
|
+
|
|
8
|
+
var React = _interopRequireWildcard(require("react"));
|
|
9
|
+
|
|
10
|
+
var _useNavigation = _interopRequireDefault(require("./useNavigation"));
|
|
11
|
+
|
|
12
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
13
|
+
|
|
14
|
+
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
|
|
15
|
+
|
|
16
|
+
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Hook to get a value from the current navigation state using a selector.
|
|
20
|
+
*
|
|
21
|
+
* @param selector Selector function to get a value from the state.
|
|
22
|
+
*/
|
|
23
|
+
function useNavigationState(selector) {
|
|
24
|
+
const navigation = (0, _useNavigation.default)(); // We don't care about the state value, we run the selector again at the end
|
|
25
|
+
// The state is only to make sure that there's a re-render when we have a new value
|
|
26
|
+
|
|
27
|
+
const [, setResult] = React.useState(() => selector(navigation.getState())); // We store the selector in a ref to avoid re-subscribing listeners every render
|
|
28
|
+
|
|
29
|
+
const selectorRef = React.useRef(selector);
|
|
30
|
+
React.useEffect(() => {
|
|
31
|
+
selectorRef.current = selector;
|
|
32
|
+
});
|
|
33
|
+
React.useEffect(() => {
|
|
34
|
+
const unsubscribe = navigation.addListener('state', e => {
|
|
35
|
+
setResult(selectorRef.current(e.data.state));
|
|
36
|
+
});
|
|
37
|
+
return unsubscribe;
|
|
38
|
+
}, [navigation]);
|
|
39
|
+
return selector(navigation.getState());
|
|
40
|
+
}
|
|
41
|
+
//# sourceMappingURL=useNavigationState.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["useNavigationState.tsx"],"names":["useNavigationState","selector","navigation","setResult","React","useState","getState","selectorRef","useRef","useEffect","current","unsubscribe","addListener","e","data","state"],"mappings":";;;;;;;AACA;;AAGA;;;;;;;;AAMA;AACA;AACA;AACA;AACA;AACe,SAASA,kBAAT,CACbC,QADa,EAEV;AACH,QAAMC,UAAU,GAAG,6BAAnB,CADG,CAGH;AACA;;AACA,QAAM,GAAGC,SAAH,IAAgBC,KAAK,CAACC,QAAN,CAAe,MAAMJ,QAAQ,CAACC,UAAU,CAACI,QAAX,EAAD,CAA7B,CAAtB,CALG,CAOH;;AACA,QAAMC,WAAW,GAAGH,KAAK,CAACI,MAAN,CAAaP,QAAb,CAApB;AAEAG,EAAAA,KAAK,CAACK,SAAN,CAAgB,MAAM;AACpBF,IAAAA,WAAW,CAACG,OAAZ,GAAsBT,QAAtB;AACD,GAFD;AAIAG,EAAAA,KAAK,CAACK,SAAN,CAAgB,MAAM;AACpB,UAAME,WAAW,GAAGT,UAAU,CAACU,WAAX,CAAuB,OAAvB,EAAiCC,CAAD,IAAO;AACzDV,MAAAA,SAAS,CAACI,WAAW,CAACG,OAAZ,CAAoBG,CAAC,CAACC,IAAF,CAAOC,KAA3B,CAAD,CAAT;AACD,KAFmB,CAApB;AAIA,WAAOJ,WAAP;AACD,GAND,EAMG,CAACT,UAAD,CANH;AAQA,SAAOD,QAAQ,CAACC,UAAU,CAACI,QAAX,EAAD,CAAf;AACD","sourcesContent":["import type { NavigationState, ParamListBase } from '@react-navigation/routers';\nimport * as React from 'react';\n\nimport type { NavigationProp } from './types';\nimport useNavigation from './useNavigation';\n\ntype Selector<ParamList extends ParamListBase, T> = (\n state: NavigationState<ParamList>\n) => T;\n\n/**\n * Hook to get a value from the current navigation state using a selector.\n *\n * @param selector Selector function to get a value from the state.\n */\nexport default function useNavigationState<ParamList extends ParamListBase, T>(\n selector: Selector<ParamList, T>\n): T {\n const navigation = useNavigation<NavigationProp<ParamList>>();\n\n // We don't care about the state value, we run the selector again at the end\n // The state is only to make sure that there's a re-render when we have a new value\n const [, setResult] = React.useState(() => selector(navigation.getState()));\n\n // We store the selector in a ref to avoid re-subscribing listeners every render\n const selectorRef = React.useRef(selector);\n\n React.useEffect(() => {\n selectorRef.current = selector;\n });\n\n React.useEffect(() => {\n const unsubscribe = navigation.addListener('state', (e) => {\n setResult(selectorRef.current(e.data.state));\n });\n\n return unsubscribe;\n }, [navigation]);\n\n return selector(navigation.getState());\n}\n"]}
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.default = useOnAction;
|
|
7
|
+
|
|
8
|
+
var React = _interopRequireWildcard(require("react"));
|
|
9
|
+
|
|
10
|
+
var _NavigationBuilderContext = _interopRequireDefault(require("./NavigationBuilderContext"));
|
|
11
|
+
|
|
12
|
+
var _useOnPreventRemove = _interopRequireWildcard(require("./useOnPreventRemove"));
|
|
13
|
+
|
|
14
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
15
|
+
|
|
16
|
+
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
|
|
17
|
+
|
|
18
|
+
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Hook to handle actions for a navigator, including state updates and bubbling.
|
|
22
|
+
*
|
|
23
|
+
* Bubbling an action is achieved in 2 ways:
|
|
24
|
+
* 1. To bubble action to parent, we expose the action handler in context and then access the parent context
|
|
25
|
+
* 2. To bubble action to child, child adds event listeners subscribing to actions from parent
|
|
26
|
+
*
|
|
27
|
+
* When the action handler handles as action, it returns `true`, otherwise `false`.
|
|
28
|
+
*/
|
|
29
|
+
function useOnAction({
|
|
30
|
+
router,
|
|
31
|
+
getState,
|
|
32
|
+
setState,
|
|
33
|
+
key,
|
|
34
|
+
actionListeners,
|
|
35
|
+
beforeRemoveListeners,
|
|
36
|
+
routerConfigOptions,
|
|
37
|
+
emitter
|
|
38
|
+
}) {
|
|
39
|
+
const {
|
|
40
|
+
onAction: onActionParent,
|
|
41
|
+
onRouteFocus: onRouteFocusParent,
|
|
42
|
+
addListener: addListenerParent,
|
|
43
|
+
onDispatchAction
|
|
44
|
+
} = React.useContext(_NavigationBuilderContext.default);
|
|
45
|
+
const routerConfigOptionsRef = React.useRef(routerConfigOptions);
|
|
46
|
+
React.useEffect(() => {
|
|
47
|
+
routerConfigOptionsRef.current = routerConfigOptions;
|
|
48
|
+
});
|
|
49
|
+
const onAction = React.useCallback((action, visitedNavigators = new Set()) => {
|
|
50
|
+
const state = getState(); // Since actions can bubble both up and down, they could come to the same navigator again
|
|
51
|
+
// We keep track of navigators which have already tried to handle the action and return if it's already visited
|
|
52
|
+
|
|
53
|
+
if (visitedNavigators.has(state.key)) {
|
|
54
|
+
return false;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
visitedNavigators.add(state.key);
|
|
58
|
+
|
|
59
|
+
if (typeof action.target !== 'string' || action.target === state.key) {
|
|
60
|
+
let result = router.getStateForAction(state, action, routerConfigOptionsRef.current); // If a target is specified and set to current navigator, the action shouldn't bubble
|
|
61
|
+
// So instead of `null`, we use the state object for such cases to signal that action was handled
|
|
62
|
+
|
|
63
|
+
result = result === null && action.target === state.key ? state : result;
|
|
64
|
+
|
|
65
|
+
if (result !== null) {
|
|
66
|
+
onDispatchAction(action, state === result);
|
|
67
|
+
|
|
68
|
+
if (state !== result) {
|
|
69
|
+
const isPrevented = (0, _useOnPreventRemove.shouldPreventRemove)(emitter, beforeRemoveListeners, state.routes, result.routes, action);
|
|
70
|
+
|
|
71
|
+
if (isPrevented) {
|
|
72
|
+
return true;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
setState(result);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
if (onRouteFocusParent !== undefined) {
|
|
79
|
+
// Some actions such as `NAVIGATE` also want to bring the navigated route to focus in the whole tree
|
|
80
|
+
// This means we need to focus all of the parent navigators of this navigator as well
|
|
81
|
+
const shouldFocus = router.shouldActionChangeFocus(action);
|
|
82
|
+
|
|
83
|
+
if (shouldFocus && key !== undefined) {
|
|
84
|
+
onRouteFocusParent(key);
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
return true;
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
if (onActionParent !== undefined) {
|
|
93
|
+
// Bubble action to the parent if the current navigator didn't handle it
|
|
94
|
+
if (onActionParent(action, visitedNavigators)) {
|
|
95
|
+
return true;
|
|
96
|
+
}
|
|
97
|
+
} // If the action wasn't handled by current navigator or a parent navigator, let children handle it
|
|
98
|
+
|
|
99
|
+
|
|
100
|
+
for (let i = actionListeners.length - 1; i >= 0; i--) {
|
|
101
|
+
const listener = actionListeners[i];
|
|
102
|
+
|
|
103
|
+
if (listener(action, visitedNavigators)) {
|
|
104
|
+
return true;
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
return false;
|
|
109
|
+
}, [actionListeners, beforeRemoveListeners, emitter, getState, key, onActionParent, onDispatchAction, onRouteFocusParent, router, setState]);
|
|
110
|
+
(0, _useOnPreventRemove.default)({
|
|
111
|
+
getState,
|
|
112
|
+
emitter,
|
|
113
|
+
beforeRemoveListeners
|
|
114
|
+
});
|
|
115
|
+
React.useEffect(() => addListenerParent === null || addListenerParent === void 0 ? void 0 : addListenerParent('action', onAction), [addListenerParent, onAction]);
|
|
116
|
+
return onAction;
|
|
117
|
+
}
|
|
118
|
+
//# sourceMappingURL=useOnAction.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["useOnAction.tsx"],"names":["useOnAction","router","getState","setState","key","actionListeners","beforeRemoveListeners","routerConfigOptions","emitter","onAction","onActionParent","onRouteFocus","onRouteFocusParent","addListener","addListenerParent","onDispatchAction","React","useContext","NavigationBuilderContext","routerConfigOptionsRef","useRef","useEffect","current","useCallback","action","visitedNavigators","Set","state","has","add","target","result","getStateForAction","isPrevented","routes","undefined","shouldFocus","shouldActionChangeFocus","i","length","listener"],"mappings":"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","sourcesContent":["import type {\n NavigationAction,\n NavigationState,\n PartialState,\n Router,\n RouterConfigOptions,\n} from '@react-navigation/routers';\nimport * as React from 'react';\n\nimport NavigationBuilderContext, {\n ChildActionListener,\n ChildBeforeRemoveListener,\n} from './NavigationBuilderContext';\nimport type { EventMapCore } from './types';\nimport type { NavigationEventEmitter } from './useEventEmitter';\nimport useOnPreventRemove, { shouldPreventRemove } from './useOnPreventRemove';\n\ntype Options = {\n router: Router<NavigationState, NavigationAction>;\n key?: string;\n getState: () => NavigationState;\n setState: (state: NavigationState | PartialState<NavigationState>) => void;\n actionListeners: ChildActionListener[];\n beforeRemoveListeners: Record<string, ChildBeforeRemoveListener | undefined>;\n routerConfigOptions: RouterConfigOptions;\n emitter: NavigationEventEmitter<EventMapCore<any>>;\n};\n\n/**\n * Hook to handle actions for a navigator, including state updates and bubbling.\n *\n * Bubbling an action is achieved in 2 ways:\n * 1. To bubble action to parent, we expose the action handler in context and then access the parent context\n * 2. To bubble action to child, child adds event listeners subscribing to actions from parent\n *\n * When the action handler handles as action, it returns `true`, otherwise `false`.\n */\nexport default function useOnAction({\n router,\n getState,\n setState,\n key,\n actionListeners,\n beforeRemoveListeners,\n routerConfigOptions,\n emitter,\n}: Options) {\n const {\n onAction: onActionParent,\n onRouteFocus: onRouteFocusParent,\n addListener: addListenerParent,\n onDispatchAction,\n } = React.useContext(NavigationBuilderContext);\n\n const routerConfigOptionsRef =\n React.useRef<RouterConfigOptions>(routerConfigOptions);\n\n React.useEffect(() => {\n routerConfigOptionsRef.current = routerConfigOptions;\n });\n\n const onAction = React.useCallback(\n (\n action: NavigationAction,\n visitedNavigators: Set<string> = new Set<string>()\n ) => {\n const state = getState();\n\n // Since actions can bubble both up and down, they could come to the same navigator again\n // We keep track of navigators which have already tried to handle the action and return if it's already visited\n if (visitedNavigators.has(state.key)) {\n return false;\n }\n\n visitedNavigators.add(state.key);\n\n if (typeof action.target !== 'string' || action.target === state.key) {\n let result = router.getStateForAction(\n state,\n action,\n routerConfigOptionsRef.current\n );\n\n // If a target is specified and set to current navigator, the action shouldn't bubble\n // So instead of `null`, we use the state object for such cases to signal that action was handled\n result =\n result === null && action.target === state.key ? state : result;\n\n if (result !== null) {\n onDispatchAction(action, state === result);\n\n if (state !== result) {\n const isPrevented = shouldPreventRemove(\n emitter,\n beforeRemoveListeners,\n state.routes,\n result.routes,\n action\n );\n\n if (isPrevented) {\n return true;\n }\n\n setState(result);\n }\n\n if (onRouteFocusParent !== undefined) {\n // Some actions such as `NAVIGATE` also want to bring the navigated route to focus in the whole tree\n // This means we need to focus all of the parent navigators of this navigator as well\n const shouldFocus = router.shouldActionChangeFocus(action);\n\n if (shouldFocus && key !== undefined) {\n onRouteFocusParent(key);\n }\n }\n\n return true;\n }\n }\n\n if (onActionParent !== undefined) {\n // Bubble action to the parent if the current navigator didn't handle it\n if (onActionParent(action, visitedNavigators)) {\n return true;\n }\n }\n\n // If the action wasn't handled by current navigator or a parent navigator, let children handle it\n for (let i = actionListeners.length - 1; i >= 0; i--) {\n const listener = actionListeners[i];\n\n if (listener(action, visitedNavigators)) {\n return true;\n }\n }\n\n return false;\n },\n [\n actionListeners,\n beforeRemoveListeners,\n emitter,\n getState,\n key,\n onActionParent,\n onDispatchAction,\n onRouteFocusParent,\n router,\n setState,\n ]\n );\n\n useOnPreventRemove({\n getState,\n emitter,\n beforeRemoveListeners,\n });\n\n React.useEffect(\n () => addListenerParent?.('action', onAction),\n [addListenerParent, onAction]\n );\n\n return onAction;\n}\n"]}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.default = useOnGetState;
|
|
7
|
+
|
|
8
|
+
var React = _interopRequireWildcard(require("react"));
|
|
9
|
+
|
|
10
|
+
var _isArrayEqual = _interopRequireDefault(require("./isArrayEqual"));
|
|
11
|
+
|
|
12
|
+
var _NavigationBuilderContext = _interopRequireDefault(require("./NavigationBuilderContext"));
|
|
13
|
+
|
|
14
|
+
var _NavigationRouteContext = _interopRequireDefault(require("./NavigationRouteContext"));
|
|
15
|
+
|
|
16
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
17
|
+
|
|
18
|
+
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
|
|
19
|
+
|
|
20
|
+
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
|
|
21
|
+
|
|
22
|
+
function useOnGetState({
|
|
23
|
+
getState,
|
|
24
|
+
getStateListeners
|
|
25
|
+
}) {
|
|
26
|
+
const {
|
|
27
|
+
addKeyedListener
|
|
28
|
+
} = React.useContext(_NavigationBuilderContext.default);
|
|
29
|
+
const route = React.useContext(_NavigationRouteContext.default);
|
|
30
|
+
const key = route ? route.key : 'root';
|
|
31
|
+
const getRehydratedState = React.useCallback(() => {
|
|
32
|
+
const state = getState(); // Avoid returning new route objects if we don't need to
|
|
33
|
+
|
|
34
|
+
const routes = state.routes.map(route => {
|
|
35
|
+
var _getStateListeners$ro;
|
|
36
|
+
|
|
37
|
+
const childState = (_getStateListeners$ro = getStateListeners[route.key]) === null || _getStateListeners$ro === void 0 ? void 0 : _getStateListeners$ro.call(getStateListeners);
|
|
38
|
+
|
|
39
|
+
if (route.state === childState) {
|
|
40
|
+
return route;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
return { ...route,
|
|
44
|
+
state: childState
|
|
45
|
+
};
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
if ((0, _isArrayEqual.default)(state.routes, routes)) {
|
|
49
|
+
return state;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
return { ...state,
|
|
53
|
+
routes
|
|
54
|
+
};
|
|
55
|
+
}, [getState, getStateListeners]);
|
|
56
|
+
React.useEffect(() => {
|
|
57
|
+
return addKeyedListener === null || addKeyedListener === void 0 ? void 0 : addKeyedListener('getState', key, getRehydratedState);
|
|
58
|
+
}, [addKeyedListener, getRehydratedState, key]);
|
|
59
|
+
}
|
|
60
|
+
//# sourceMappingURL=useOnGetState.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["useOnGetState.tsx"],"names":["useOnGetState","getState","getStateListeners","addKeyedListener","React","useContext","NavigationBuilderContext","route","NavigationRouteContext","key","getRehydratedState","useCallback","state","routes","map","childState","useEffect"],"mappings":";;;;;;;AACA;;AAEA;;AACA;;AAGA;;;;;;;;AAOe,SAASA,aAAT,CAAuB;AACpCC,EAAAA,QADoC;AAEpCC,EAAAA;AAFoC,CAAvB,EAGH;AACV,QAAM;AAAEC,IAAAA;AAAF,MAAuBC,KAAK,CAACC,UAAN,CAAiBC,iCAAjB,CAA7B;AACA,QAAMC,KAAK,GAAGH,KAAK,CAACC,UAAN,CAAiBG,+BAAjB,CAAd;AACA,QAAMC,GAAG,GAAGF,KAAK,GAAGA,KAAK,CAACE,GAAT,GAAe,MAAhC;AAEA,QAAMC,kBAAkB,GAAGN,KAAK,CAACO,WAAN,CAAkB,MAAM;AACjD,UAAMC,KAAK,GAAGX,QAAQ,EAAtB,CADiD,CAGjD;;AACA,UAAMY,MAAM,GAAGD,KAAK,CAACC,MAAN,CAAaC,GAAb,CAAkBP,KAAD,IAAW;AAAA;;AACzC,YAAMQ,UAAU,4BAAGb,iBAAiB,CAACK,KAAK,CAACE,GAAP,CAApB,0DAAG,2BAAAP,iBAAiB,CAApC;;AAEA,UAAIK,KAAK,CAACK,KAAN,KAAgBG,UAApB,EAAgC;AAC9B,eAAOR,KAAP;AACD;;AAED,aAAO,EAAE,GAAGA,KAAL;AAAYK,QAAAA,KAAK,EAAEG;AAAnB,OAAP;AACD,KARc,CAAf;;AAUA,QAAI,2BAAaH,KAAK,CAACC,MAAnB,EAA2BA,MAA3B,CAAJ,EAAwC;AACtC,aAAOD,KAAP;AACD;;AAED,WAAO,EAAE,GAAGA,KAAL;AAAYC,MAAAA;AAAZ,KAAP;AACD,GAnB0B,EAmBxB,CAACZ,QAAD,EAAWC,iBAAX,CAnBwB,CAA3B;AAqBAE,EAAAA,KAAK,CAACY,SAAN,CAAgB,MAAM;AACpB,WAAOb,gBAAP,aAAOA,gBAAP,uBAAOA,gBAAgB,CAAG,UAAH,EAAeM,GAAf,EAAoBC,kBAApB,CAAvB;AACD,GAFD,EAEG,CAACP,gBAAD,EAAmBO,kBAAnB,EAAuCD,GAAvC,CAFH;AAGD","sourcesContent":["import type { NavigationState } from '@react-navigation/routers';\nimport * as React from 'react';\n\nimport isArrayEqual from './isArrayEqual';\nimport NavigationBuilderContext, {\n GetStateListener,\n} from './NavigationBuilderContext';\nimport NavigationRouteContext from './NavigationRouteContext';\n\ntype Options = {\n getState: () => NavigationState;\n getStateListeners: Record<string, GetStateListener | undefined>;\n};\n\nexport default function useOnGetState({\n getState,\n getStateListeners,\n}: Options) {\n const { addKeyedListener } = React.useContext(NavigationBuilderContext);\n const route = React.useContext(NavigationRouteContext);\n const key = route ? route.key : 'root';\n\n const getRehydratedState = React.useCallback(() => {\n const state = getState();\n\n // Avoid returning new route objects if we don't need to\n const routes = state.routes.map((route) => {\n const childState = getStateListeners[route.key]?.();\n\n if (route.state === childState) {\n return route;\n }\n\n return { ...route, state: childState };\n });\n\n if (isArrayEqual(state.routes, routes)) {\n return state;\n }\n\n return { ...state, routes };\n }, [getState, getStateListeners]);\n\n React.useEffect(() => {\n return addKeyedListener?.('getState', key, getRehydratedState);\n }, [addKeyedListener, getRehydratedState, key]);\n}\n"]}
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.default = useOnPreventRemove;
|
|
7
|
+
exports.shouldPreventRemove = void 0;
|
|
8
|
+
|
|
9
|
+
var React = _interopRequireWildcard(require("react"));
|
|
10
|
+
|
|
11
|
+
var _NavigationBuilderContext = _interopRequireDefault(require("./NavigationBuilderContext"));
|
|
12
|
+
|
|
13
|
+
var _NavigationRouteContext = _interopRequireDefault(require("./NavigationRouteContext"));
|
|
14
|
+
|
|
15
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
16
|
+
|
|
17
|
+
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
|
|
18
|
+
|
|
19
|
+
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
|
|
20
|
+
|
|
21
|
+
const VISITED_ROUTE_KEYS = Symbol('VISITED_ROUTE_KEYS');
|
|
22
|
+
|
|
23
|
+
const shouldPreventRemove = (emitter, beforeRemoveListeners, currentRoutes, nextRoutes, action) => {
|
|
24
|
+
var _action$VISITED_ROUTE;
|
|
25
|
+
|
|
26
|
+
const nextRouteKeys = nextRoutes.map(route => route.key); // Call these in reverse order so last screens handle the event first
|
|
27
|
+
|
|
28
|
+
const removedRoutes = currentRoutes.filter(route => !nextRouteKeys.includes(route.key)).reverse();
|
|
29
|
+
const visitedRouteKeys = // @ts-expect-error: add this property to mark that we've already emitted this action
|
|
30
|
+
(_action$VISITED_ROUTE = action[VISITED_ROUTE_KEYS]) !== null && _action$VISITED_ROUTE !== void 0 ? _action$VISITED_ROUTE : new Set();
|
|
31
|
+
const beforeRemoveAction = { ...action,
|
|
32
|
+
[VISITED_ROUTE_KEYS]: visitedRouteKeys
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
for (const route of removedRoutes) {
|
|
36
|
+
var _beforeRemoveListener;
|
|
37
|
+
|
|
38
|
+
if (visitedRouteKeys.has(route.key)) {
|
|
39
|
+
// Skip if we've already emitted this action for this screen
|
|
40
|
+
continue;
|
|
41
|
+
} // First, we need to check if any child screens want to prevent it
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
const isPrevented = (_beforeRemoveListener = beforeRemoveListeners[route.key]) === null || _beforeRemoveListener === void 0 ? void 0 : _beforeRemoveListener.call(beforeRemoveListeners, beforeRemoveAction);
|
|
45
|
+
|
|
46
|
+
if (isPrevented) {
|
|
47
|
+
return true;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
visitedRouteKeys.add(route.key);
|
|
51
|
+
const event = emitter.emit({
|
|
52
|
+
type: 'beforeRemove',
|
|
53
|
+
target: route.key,
|
|
54
|
+
data: {
|
|
55
|
+
action: beforeRemoveAction
|
|
56
|
+
},
|
|
57
|
+
canPreventDefault: true
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
if (event.defaultPrevented) {
|
|
61
|
+
return true;
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
return false;
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
exports.shouldPreventRemove = shouldPreventRemove;
|
|
69
|
+
|
|
70
|
+
function useOnPreventRemove({
|
|
71
|
+
getState,
|
|
72
|
+
emitter,
|
|
73
|
+
beforeRemoveListeners
|
|
74
|
+
}) {
|
|
75
|
+
const {
|
|
76
|
+
addKeyedListener
|
|
77
|
+
} = React.useContext(_NavigationBuilderContext.default);
|
|
78
|
+
const route = React.useContext(_NavigationRouteContext.default);
|
|
79
|
+
const routeKey = route === null || route === void 0 ? void 0 : route.key;
|
|
80
|
+
React.useEffect(() => {
|
|
81
|
+
if (routeKey) {
|
|
82
|
+
return addKeyedListener === null || addKeyedListener === void 0 ? void 0 : addKeyedListener('beforeRemove', routeKey, action => {
|
|
83
|
+
const state = getState();
|
|
84
|
+
return shouldPreventRemove(emitter, beforeRemoveListeners, state.routes, [], action);
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
}, [addKeyedListener, beforeRemoveListeners, emitter, getState, routeKey]);
|
|
88
|
+
}
|
|
89
|
+
//# sourceMappingURL=useOnPreventRemove.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["useOnPreventRemove.tsx"],"names":["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","React","useContext","NavigationBuilderContext","NavigationRouteContext","routeKey","useEffect","state","routes"],"mappings":";;;;;;;;AAIA;;AAEA;;AAGA;;;;;;;;AAUA,MAAMA,kBAAkB,GAAGC,MAAM,CAAC,oBAAD,CAAjC;;AAEO,MAAMC,mBAAmB,GAAG,CACjCC,OADiC,EAEjCC,qBAFiC,EAGjCC,aAHiC,EAIjCC,UAJiC,EAKjCC,MALiC,KAM9B;AAAA;;AACH,QAAMC,aAAa,GAAGF,UAAU,CAACG,GAAX,CAAgBC,KAAD,IAAWA,KAAK,CAACC,GAAhC,CAAtB,CADG,CAGH;;AACA,QAAMC,aAAa,GAAGP,aAAa,CAChCQ,MADmB,CACXH,KAAD,IAAW,CAACF,aAAa,CAACM,QAAd,CAAuBJ,KAAK,CAACC,GAA7B,CADA,EAEnBI,OAFmB,EAAtB;AAIA,QAAMC,gBAA6B,GACjC;AADiC,2BAEjCT,MAAM,CAACP,kBAAD,CAF2B,yEAEH,IAAIiB,GAAJ,EAFhC;AAIA,QAAMC,kBAAkB,GAAG,EACzB,GAAGX,MADsB;AAEzB,KAACP,kBAAD,GAAsBgB;AAFG,GAA3B;;AAKA,OAAK,MAAMN,KAAX,IAAoBE,aAApB,EAAmC;AAAA;;AACjC,QAAII,gBAAgB,CAACG,GAAjB,CAAqBT,KAAK,CAACC,GAA3B,CAAJ,EAAqC;AACnC;AACA;AACD,KAJgC,CAMjC;;;AACA,UAAMS,WAAW,4BAAGhB,qBAAqB,CAACM,KAAK,CAACC,GAAP,CAAxB,0DAAG,2BAAAP,qBAAqB,EAAcc,kBAAd,CAAzC;;AAEA,QAAIE,WAAJ,EAAiB;AACf,aAAO,IAAP;AACD;;AAEDJ,IAAAA,gBAAgB,CAACK,GAAjB,CAAqBX,KAAK,CAACC,GAA3B;AAEA,UAAMW,KAAK,GAAGnB,OAAO,CAACoB,IAAR,CAAa;AACzBC,MAAAA,IAAI,EAAE,cADmB;AAEzBC,MAAAA,MAAM,EAAEf,KAAK,CAACC,GAFW;AAGzBe,MAAAA,IAAI,EAAE;AAAEnB,QAAAA,MAAM,EAAEW;AAAV,OAHmB;AAIzBS,MAAAA,iBAAiB,EAAE;AAJM,KAAb,CAAd;;AAOA,QAAIL,KAAK,CAACM,gBAAV,EAA4B;AAC1B,aAAO,IAAP;AACD;AACF;;AAED,SAAO,KAAP;AACD,CAnDM;;;;AAqDQ,SAASC,kBAAT,CAA4B;AACzCC,EAAAA,QADyC;AAEzC3B,EAAAA,OAFyC;AAGzCC,EAAAA;AAHyC,CAA5B,EAIH;AACV,QAAM;AAAE2B,IAAAA;AAAF,MAAuBC,KAAK,CAACC,UAAN,CAAiBC,iCAAjB,CAA7B;AACA,QAAMxB,KAAK,GAAGsB,KAAK,CAACC,UAAN,CAAiBE,+BAAjB,CAAd;AACA,QAAMC,QAAQ,GAAG1B,KAAH,aAAGA,KAAH,uBAAGA,KAAK,CAAEC,GAAxB;AAEAqB,EAAAA,KAAK,CAACK,SAAN,CAAgB,MAAM;AACpB,QAAID,QAAJ,EAAc;AACZ,aAAOL,gBAAP,aAAOA,gBAAP,uBAAOA,gBAAgB,CAAG,cAAH,EAAmBK,QAAnB,EAA8B7B,MAAD,IAAY;AAC9D,cAAM+B,KAAK,GAAGR,QAAQ,EAAtB;AAEA,eAAO5B,mBAAmB,CACxBC,OADwB,EAExBC,qBAFwB,EAGxBkC,KAAK,CAACC,MAHkB,EAIxB,EAJwB,EAKxBhC,MALwB,CAA1B;AAOD,OAVsB,CAAvB;AAWD;AACF,GAdD,EAcG,CAACwB,gBAAD,EAAmB3B,qBAAnB,EAA0CD,OAA1C,EAAmD2B,QAAnD,EAA6DM,QAA7D,CAdH;AAeD","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"]}
|