Skip to content

Commit 46813b8

Browse files
committed
fix: disable ios view recycling
1 parent f1110e7 commit 46813b8

File tree

1 file changed

+55
-13
lines changed

1 file changed

+55
-13
lines changed

scripts/nitrogen-patch.js

Lines changed: 55 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,44 @@
11
/**
2-
* Recursively patches all generated Android files:
2+
* Recursively patches all generated Nitro files (Android & iOS):
3+
*
4+
* ANDROID
35
* - Replaces 'com.margelo.nitro.rngooglemapsplus' -> 'com.rngooglemapsplus'
46
* - Replaces 'com/margelo/nitro/rngooglemapsplus' -> 'com/rngooglemapsplus'
57
* - Removes 'margelo/nitro/' in RNGoogleMapsPlusOnLoad.cpp
6-
* - Inserts `prepareToRecycleView()` under `onDropViewInstance()` if missing
8+
* - Inserts `prepareToRecycleView()`
9+
* nitrogen/generated/android/kotlin/com/margelo/nitro/rngooglemapsplus/views/HybridRNGoogleMapsPlusViewManager.kt
10+
*
11+
* iOS
12+
* - Inserts `+ (BOOL)shouldBeRecycled`
13+
* nitrogen/generated/ios/c++/views/HybridRNGoogleMapsPlusViewComponent.mm
714
*/
815
import { fileURLToPath } from 'url';
916
import { basename } from 'path';
1017
import path from 'node:path';
1118
import { readdir, readFile, writeFile } from 'node:fs/promises';
1219

13-
const ROOT_DIR = path.join(process.cwd(), 'nitrogen', 'generated', 'android');
14-
console.log(ROOT_DIR);
15-
const ANDROID_ONLOAD_FILE = path.join(ROOT_DIR, 'RNGoogleMapsPlusOnLoad.cpp');
20+
const ROOT_ANDROID = path.join(
21+
process.cwd(),
22+
'nitrogen',
23+
'generated',
24+
'android'
25+
);
26+
const ROOT_IOS = path.join(process.cwd(), 'nitrogen', 'generated', 'ios');
27+
const ANDROID_ONLOAD_FILE = path.join(
28+
ROOT_ANDROID,
29+
'RNGoogleMapsPlusOnLoad.cpp'
30+
);
1631

1732
const HYBRID_VIEW_MANAGER = path.join(
18-
ROOT_DIR,
33+
ROOT_ANDROID,
1934
'kotlin/com/margelo/nitro/rngooglemapsplus/views/HybridRNGoogleMapsPlusViewManager.kt'
2035
);
2136

37+
const HYBRID_VIEW_COMPONENT_IOS = path.join(
38+
ROOT_IOS,
39+
'c++/views/HybridRNGoogleMapsPlusViewComponent.mm'
40+
);
41+
2242
const REPLACEMENTS = [
2343
{
2444
regex: /com\.margelo\.nitro\.rngooglemapsplus/g,
@@ -33,14 +53,21 @@ const REPLACEMENTS = [
3353
const __filename = fileURLToPath(import.meta.url);
3454
const filename = basename(__filename);
3555

36-
const RECYCLE_METHOD = `
56+
const RECYCLE_METHOD_ANDROID = `
3757
/// added by ${filename}
3858
override fun prepareToRecycleView(reactContext: ThemedReactContext, view: View): View? {
3959
return null
4060
}
4161
`;
4262

43-
// Patch-Routine
63+
const RECYCLE_METHOD_IOS = `
64+
/// added by ${filename}
65+
+ (BOOL)shouldBeRecycled
66+
{
67+
return NO;
68+
}
69+
`;
70+
4471
async function processFile(filePath) {
4572
let content = await readFile(filePath, 'utf8');
4673
let updated = content;
@@ -53,16 +80,30 @@ async function processFile(filePath) {
5380
updated = updated.replace(/margelo\/nitro\//g, '');
5481
}
5582

56-
console.log(filePath);
5783
if (path.resolve(filePath) === path.resolve(HYBRID_VIEW_MANAGER)) {
5884
if (!/override fun prepareToRecycleView/.test(updated)) {
5985
const pattern =
6086
/(override fun onDropViewInstance\(view: View\)\s*\{[^}]+\}\s*)/m;
6187

6288
if (pattern.test(updated)) {
63-
updated = updated.replace(pattern, `$1${RECYCLE_METHOD}\n`);
89+
updated = updated.replace(pattern, `$1${RECYCLE_METHOD_ANDROID}\n`);
90+
} else {
91+
throw new Error(
92+
`Pattern for "onDropViewInstance" not found in ${filePath}`
93+
);
94+
}
95+
}
96+
}
97+
98+
if (path.resolve(filePath) === path.resolve(HYBRID_VIEW_COMPONENT_IOS)) {
99+
if (!/\+\s*\(BOOL\)\s*shouldBeRecycled/.test(updated)) {
100+
const pattern =
101+
/(- \(instancetype\)\s*init\s*\{(?:[^{}]|\{[^{}]*\})*\})/m;
102+
103+
if (pattern.test(updated)) {
104+
updated = updated.replace(pattern, `$1\n${RECYCLE_METHOD_IOS}`);
64105
} else {
65-
updated = updated.replace(/}\s*$/m, `${RECYCLE_METHOD}\n}\n`);
106+
throw new Error(`Pattern for "init" not found in ${filePath}`);
66107
}
67108
}
68109
}
@@ -87,8 +128,9 @@ async function start(dir) {
87128

88129
(async () => {
89130
try {
90-
await start(ROOT_DIR);
91-
console.log('All occurrences patched successfully.');
131+
await start(ROOT_ANDROID);
132+
await start(ROOT_IOS);
133+
console.log('All Nitrogen files patched successfully.');
92134
} catch (err) {
93135
console.error('Error while processing files:', err);
94136
process.exit(1);

0 commit comments

Comments
 (0)