1 | import * as React from 'react';
|
2 | import isArrayEqual from './isArrayEqual';
|
3 | import NavigationBuilderContext from './NavigationBuilderContext';
|
4 | import NavigationRouteContext from './NavigationRouteContext';
|
5 | export default function useOnGetState(_ref) {
|
6 | let {
|
7 | getState,
|
8 | getStateListeners
|
9 | } = _ref;
|
10 | const {
|
11 | addKeyedListener
|
12 | } = React.useContext(NavigationBuilderContext);
|
13 | const route = React.useContext(NavigationRouteContext);
|
14 | const key = route ? route.key : 'root';
|
15 | const getRehydratedState = React.useCallback(() => {
|
16 | const state = getState();
|
17 |
|
18 | const routes = state.routes.map(route => {
|
19 | var _getStateListeners$ro;
|
20 |
|
21 | const childState = (_getStateListeners$ro = getStateListeners[route.key]) === null || _getStateListeners$ro === void 0 ? void 0 : _getStateListeners$ro.call(getStateListeners);
|
22 |
|
23 | if (route.state === childState) {
|
24 | return route;
|
25 | }
|
26 |
|
27 | return { ...route,
|
28 | state: childState
|
29 | };
|
30 | });
|
31 |
|
32 | if (isArrayEqual(state.routes, routes)) {
|
33 | return state;
|
34 | }
|
35 |
|
36 | return { ...state,
|
37 | routes
|
38 | };
|
39 | }, [getState, getStateListeners]);
|
40 | React.useEffect(() => {
|
41 | return addKeyedListener === null || addKeyedListener === void 0 ? void 0 : addKeyedListener('getState', key, getRehydratedState);
|
42 | }, [addKeyedListener, getRehydratedState, key]);
|
43 | }
|
44 |
|
\ | No newline at end of file |