-
-
Notifications
You must be signed in to change notification settings - Fork 5k
/
createCompatNavigationProp.tsx
220 lines (198 loc) · 6.26 KB
/
createCompatNavigationProp.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
import type {
NavigationState,
PartialState,
ParamListBase,
NavigationProp,
RouteProp,
} from '@react-navigation/native';
import * as helpers from './helpers';
import type { CompatNavigationProp } from './types';
type EventName =
| 'action'
| 'willFocus'
| 'willBlur'
| 'didFocus'
| 'didBlur'
| 'refocus';
export default function createCompatNavigationProp<
NavigationPropType extends NavigationProp<ParamListBase>,
ParamList extends ParamListBase = NavigationPropType extends NavigationProp<
infer P,
any,
any,
any,
any
>
? P
: ParamListBase
>(
navigation: NavigationPropType,
state:
| (RouteProp<ParamList, keyof ParamList> & {
state?: NavigationState | PartialState<NavigationState>;
})
| NavigationState
| PartialState<NavigationState>,
context: Record<string, any>,
isFirstRouteInParent?: boolean
): CompatNavigationProp<NavigationPropType> {
context.parent = context.parent || {};
context.subscriptions = context.subscriptions || {
didFocus: new Map<() => void, () => void>(),
didBlur: new Map<() => void, () => void>(),
refocus: new Map<() => void, () => void>(),
};
return {
...navigation,
...Object.entries(helpers).reduce<{
[key: string]: (...args: any[]) => void;
}>((acc, [name, method]: [string, Function]) => {
if (name in navigation) {
acc[name] = (...args: any[]) => navigation.dispatch(method(...args));
}
return acc;
}, {}),
original: navigation,
addListener(type: EventName, callback: () => void) {
let unsubscribe: () => void;
switch (type) {
case 'willFocus':
unsubscribe = navigation.addListener('focus', callback);
break;
case 'willBlur':
unsubscribe = navigation.addListener('blur', callback);
break;
case 'didFocus': {
const listener = () => {
if (navigation.isFocused()) {
callback();
}
};
// @ts-expect-error: this event may not exist in this navigator
unsubscribe = navigation.addListener('transitionEnd', listener);
context.subscriptions.didFocus.set(callback, unsubscribe);
break;
}
case 'didBlur': {
const listener = () => {
if (!navigation.isFocused()) {
callback();
}
};
// @ts-expect-error: this event may not exist in this navigator
unsubscribe = navigation.addListener('transitionEnd', listener);
context.subscriptions.didBlur.set(callback, unsubscribe);
break;
}
case 'refocus': {
const listener = () => {
if (navigation.isFocused()) {
callback();
}
};
// @ts-expect-error: this event may not exist in this navigator
unsubscribe = navigation.addListener('tabPress', listener);
context.subscriptions.refocus.set(callback, unsubscribe);
break;
}
case 'action':
throw new Error("Listening to 'action' events is not supported.");
default:
unsubscribe = navigation.addListener(type, callback);
}
const subscription = () => unsubscribe();
subscription.remove = unsubscribe;
return subscription;
},
removeListener(type: EventName, callback: () => void) {
context.subscriptions = context.subscriptions || {};
switch (type) {
case 'willFocus':
navigation.removeListener('focus', callback);
break;
case 'willBlur':
navigation.removeListener('blur', callback);
break;
case 'didFocus': {
const unsubscribe = context.subscriptions.didFocus.get(callback);
unsubscribe?.();
break;
}
case 'didBlur': {
const unsubscribe = context.subscriptions.didBlur.get(callback);
unsubscribe?.();
break;
}
case 'refocus': {
const unsubscribe = context.subscriptions.refocus.get(callback);
unsubscribe?.();
break;
}
case 'action':
throw new Error("Listening to 'action' events is not supported.");
default:
navigation.removeListener(type, callback);
}
},
state: {
key: state.key,
// @ts-expect-error
routeName: state.name,
// @ts-expect-error
params: state.params ?? {},
get index() {
// @ts-expect-error
if (state.index !== undefined) {
// @ts-expect-error
return state.index;
}
console.warn(
"Looks like you are using 'navigation.state.index' in your code. Accessing child navigation state for a route is not safe and won't work correctly. You should refactor it not to access the 'index' property in the child navigation state."
);
// @ts-expect-error
return state.state?.index;
},
get routes() {
// @ts-expect-error
if (state.routes !== undefined) {
// @ts-expect-error
return state.routes;
}
console.warn(
"Looks like you are using 'navigation.state.routes' in your code. Accessing child navigation state for a route is not safe and won't work correctly. You should refactor it not to access the 'routes' property in the child navigation state."
);
// @ts-expect-error
return state.state?.routes;
},
},
getParam<T extends keyof ParamList>(
paramName: T,
defaultValue: ParamList[T]
): ParamList[T] {
// @ts-expect-error
const params = state.params;
if (params && paramName in params) {
return params[paramName];
}
return defaultValue;
},
isFirstRouteInParent(): boolean {
if (typeof isFirstRouteInParent === 'boolean') {
return isFirstRouteInParent;
}
const { routes } = navigation.dangerouslyGetState();
return routes[0].key === state.key;
},
dangerouslyGetParent() {
const parent = navigation.dangerouslyGetParent();
if (parent) {
return createCompatNavigationProp(
parent,
navigation.dangerouslyGetState(),
context.parent
);
}
return undefined;
},
} as any;
}