Skip to content

Commit 6e5b5eb

Browse files
committed
msglist [nfc]: Reorganize makeOnShouldStartLoadWithRequest for clarity
Fewer layers of nested function expressions; stop having a conditional to sometimes return one function expression and sometimes another. The cost (such as it is) is that we always make the local variable `loaded_once`, even on Android where we won't need it. I rather think we can afford one extra Boolean variable, one per message-list screen.
1 parent 4d9a54a commit 6e5b5eb

File tree

1 file changed

+20
-22
lines changed

1 file changed

+20
-22
lines changed

src/webview/SinglePageWebView.js

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -24,30 +24,11 @@ import type { ElementConfigFull } from '../reactUtils';
2424
function makeOnShouldStartLoadWithRequest(
2525
baseUrl: URL,
2626
): React.ElementConfig<typeof WebView>['onShouldStartLoadWithRequest'] {
27-
// Inner closure to actually test the URL.
28-
const urlTester: (url: string) => boolean = (() => {
29-
// On Android this function is documented to be skipped on first load:
30-
// therefore, simply never return true.
31-
if (Platform.OS === 'android') {
32-
return (url: string) => false;
33-
}
27+
let loaded_once = false;
3428

35-
// Otherwise (for iOS), return a closure that evaluates to `true` _exactly
36-
// once_, and even then only if the URL looks like what we're expecting.
37-
let loaded_once = false;
38-
return (url: string) => {
39-
const parsedUrl = tryParseUrl(url);
40-
if (!loaded_once && parsedUrl && parsedUrl.toString() === baseUrl.toString()) {
41-
loaded_once = true;
42-
return true;
43-
}
44-
return false;
45-
};
46-
})();
47-
48-
// Outer closure to perform logging.
4929
return event => {
50-
const ok = urlTester(event.url);
30+
// eslint-disable-next-line no-use-before-define
31+
const ok = urlIsOk(event.url);
5132
if (!ok) {
5233
logging.warn('webview: rejected navigation event', {
5334
navigation_event: { ...event },
@@ -56,6 +37,23 @@ function makeOnShouldStartLoadWithRequest(
5637
}
5738
return ok;
5839
};
40+
41+
function urlIsOk(url: string): boolean {
42+
// On Android the onShouldStartLoadWithRequest prop is documented to be
43+
// skipped on first load; therefore, simply never return true.
44+
if (Platform.OS === 'android') {
45+
return false;
46+
}
47+
48+
// Otherwise (for iOS), return `true` only if the URL looks like what
49+
// we're expecting, and only the first such time.
50+
const parsedUrl = tryParseUrl(url);
51+
if (!loaded_once && parsedUrl && parsedUrl.toString() === baseUrl.toString()) {
52+
loaded_once = true;
53+
return true;
54+
}
55+
return false;
56+
}
5957
}
6058

6159
/**

0 commit comments

Comments
 (0)