-
Notifications
You must be signed in to change notification settings - Fork 24.7k
refactor: Rewrite JSBundleLoader from Java to Kotlin #50911
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
yogeshpaliyal
wants to merge
2
commits into
facebook:main
from
yogeshpaliyal:migrateJSBundleLoaderKt
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
99 changes: 0 additions & 99 deletions
99
...ges/react-native/ReactAndroid/src/main/java/com/facebook/react/bridge/JSBundleLoader.java
This file was deleted.
Oops, something went wrong.
103 changes: 103 additions & 0 deletions
103
packages/react-native/ReactAndroid/src/main/java/com/facebook/react/bridge/JSBundleLoader.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
/* | ||
* Copyright (c) Meta Platforms, Inc. and affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
package com.facebook.react.bridge | ||
|
||
import android.content.Context | ||
import com.facebook.react.common.DebugServerException | ||
|
||
/** A class that stores JS bundle information and allows a [JSBundleLoaderDelegate]. */ | ||
public abstract class JSBundleLoader { | ||
|
||
/** Loads the script, returning the URL of the source it loaded. */ | ||
public abstract fun loadScript(delegate: JSBundleLoaderDelegate): String | ||
|
||
public companion object { | ||
mateoguzmana marked this conversation as resolved.
Show resolved
Hide resolved
|
||
/** | ||
* This loader is recommended one for release version of your app. In that case local JS | ||
* executor should be used. JS bundle will be read from assets in native code to save on passing | ||
* large strings from java to native memory. | ||
*/ | ||
@JvmStatic | ||
public fun createAssetLoader( | ||
context: Context, | ||
assetUrl: String, | ||
loadSynchronously: Boolean | ||
): JSBundleLoader { | ||
return object : JSBundleLoader() { | ||
override fun loadScript(delegate: JSBundleLoaderDelegate): String { | ||
delegate.loadScriptFromAssets(context.assets, assetUrl, loadSynchronously) | ||
return assetUrl | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* This loader loads bundle from file system. The bundle will be read in native code to save on | ||
* passing large strings from java to native memory. | ||
*/ | ||
@JvmStatic | ||
public fun createFileLoader(fileName: String): JSBundleLoader = | ||
createFileLoader(fileName, fileName, false) | ||
|
||
@JvmStatic | ||
public fun createFileLoader( | ||
fileName: String, | ||
assetUrl: String, | ||
loadSynchronously: Boolean | ||
): JSBundleLoader { | ||
return object : JSBundleLoader() { | ||
override fun loadScript(delegate: JSBundleLoaderDelegate): String { | ||
delegate.loadScriptFromFile(fileName, assetUrl, loadSynchronously) | ||
return fileName | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* This loader is used when bundle gets reloaded from dev server. In that case loader expect JS | ||
* bundle to be prefetched and stored in local file. We do that to avoid passing large strings | ||
* between java and native code and avoid allocating memory in java to fit whole JS bundle in | ||
* it. Providing correct [sourceURL] of downloaded bundle is required for JS stacktraces to work | ||
* correctly and allows for source maps to correctly symbolize those. | ||
*/ | ||
@JvmStatic | ||
public fun createCachedBundleFromNetworkLoader( | ||
sourceURL: String, | ||
cachedFileLocation: String | ||
): JSBundleLoader { | ||
return object : JSBundleLoader() { | ||
override fun loadScript(delegate: JSBundleLoaderDelegate): String { | ||
return try { | ||
delegate.loadScriptFromFile(cachedFileLocation, sourceURL, false) | ||
sourceURL | ||
} catch (e: Exception) { | ||
throw DebugServerException.makeGeneric(sourceURL, e.message ?: "", e) | ||
} | ||
} | ||
} | ||
} | ||
|
||
/** Same as [createCachedBundleFromNetworkLoader], but for split bundles in development. */ | ||
@JvmStatic | ||
public fun createCachedSplitBundleFromNetworkLoader( | ||
sourceURL: String, | ||
cachedFileLocation: String | ||
): JSBundleLoader { | ||
return object : JSBundleLoader() { | ||
override fun loadScript(delegate: JSBundleLoaderDelegate): String { | ||
return try { | ||
delegate.loadSplitBundleFromFile(cachedFileLocation, sourceURL) | ||
sourceURL | ||
} catch (e: Exception) { | ||
throw DebugServerException.makeGeneric(sourceURL, e.message ?: "", e) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.