Skip to content

Commit a5aa180

Browse files
committed
feat: migragte JSBundleLoader from java to kt
1 parent 0e11e6a commit a5aa180

2 files changed

Lines changed: 108 additions & 99 deletions

File tree

‎packages/react-native/ReactAndroid/src/main/java/com/facebook/react/bridge/JSBundleLoader.java‎

Lines changed: 0 additions & 99 deletions
This file was deleted.
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
/*
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
package com.facebook.react.bridge
8+
9+
import android.content.Context
10+
import com.facebook.infer.annotation.Nullsafe
11+
import com.facebook.react.common.DebugServerException
12+
import java.util.Objects
13+
14+
/** A class that stores JS bundle information and allows a [JSBundleLoaderDelegate]. */
15+
@Nullsafe(Nullsafe.Mode.LOCAL)
16+
public abstract class JSBundleLoader {
17+
18+
/** Loads the script, returning the URL of the source it loaded. */
19+
public abstract fun loadScript(delegate: JSBundleLoaderDelegate): String
20+
21+
public companion object {
22+
/**
23+
* This loader is recommended one for release version of your app. In that case local JS
24+
* executor should be used. JS bundle will be read from assets in native code to save on
25+
* passing large strings from java to native memory.
26+
*/
27+
@JvmStatic
28+
public fun createAssetLoader(
29+
context: Context,
30+
assetUrl: String,
31+
loadSynchronously: Boolean
32+
): JSBundleLoader {
33+
return object : JSBundleLoader() {
34+
override fun loadScript(delegate: JSBundleLoaderDelegate): String {
35+
delegate.loadScriptFromAssets(context.assets, assetUrl, loadSynchronously)
36+
return assetUrl
37+
}
38+
}
39+
}
40+
41+
/**
42+
* This loader loads bundle from file system. The bundle will be read in native code to save on
43+
* passing large strings from java to native memory.
44+
*/
45+
@JvmStatic
46+
public fun createFileLoader(fileName: String): JSBundleLoader {
47+
return createFileLoader(fileName, fileName, false)
48+
}
49+
50+
@JvmStatic
51+
public fun createFileLoader(
52+
fileName: String,
53+
assetUrl: String,
54+
loadSynchronously: Boolean
55+
): JSBundleLoader {
56+
return object : JSBundleLoader() {
57+
override fun loadScript(delegate: JSBundleLoaderDelegate): String {
58+
delegate.loadScriptFromFile(fileName, assetUrl, loadSynchronously)
59+
return fileName
60+
}
61+
}
62+
}
63+
64+
/**
65+
* This loader is used when bundle gets reloaded from dev server. In that case loader expect JS
66+
* bundle to be prefetched and stored in local file. We do that to avoid passing large strings
67+
* between java and native code and avoid allocating memory in java to fit whole JS bundle in
68+
* it. Providing correct [sourceURL] of downloaded bundle is required for JS stacktraces to
69+
* work correctly and allows for source maps to correctly symbolize those.
70+
*/
71+
@JvmStatic
72+
public fun createCachedBundleFromNetworkLoader(
73+
sourceURL: String,
74+
cachedFileLocation: String
75+
): JSBundleLoader {
76+
return object : JSBundleLoader() {
77+
override fun loadScript(delegate: JSBundleLoaderDelegate): String {
78+
return try {
79+
delegate.loadScriptFromFile(cachedFileLocation, sourceURL, false)
80+
sourceURL
81+
} catch (e: Exception) {
82+
throw DebugServerException.makeGeneric(sourceURL, e.message ?: "", e)
83+
}
84+
}
85+
}
86+
}
87+
88+
/**
89+
* Same as [createCachedBundleFromNetworkLoader], but for split bundles in development.
90+
*/
91+
@JvmStatic
92+
public fun createCachedSplitBundleFromNetworkLoader(
93+
sourceURL: String,
94+
cachedFileLocation: String
95+
): JSBundleLoader {
96+
return object : JSBundleLoader() {
97+
override fun loadScript(delegate: JSBundleLoaderDelegate): String {
98+
return try {
99+
delegate.loadSplitBundleFromFile(cachedFileLocation, sourceURL)
100+
sourceURL
101+
} catch (e: Exception) {
102+
throw DebugServerException.makeGeneric(sourceURL, e.message ?: "", e)
103+
}
104+
}
105+
}
106+
}
107+
}
108+
}

0 commit comments

Comments
 (0)