Skip to content

Commit

Permalink
Migrated to React Host for new Arch
Browse files Browse the repository at this point in the history
  • Loading branch information
Vignesh-772 committed Sep 6, 2024
1 parent 2bae1de commit 1f8edcc
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import android.content.pm.PackageManager;
import android.content.res.Resources;

import com.facebook.react.ReactHost;
import com.facebook.react.ReactInstanceManager;
import com.facebook.react.ReactPackage;
import com.facebook.react.bridge.JavaScriptModule;
Expand Down Expand Up @@ -50,6 +51,7 @@ public class CodePush implements ReactPackage {
private static ReactInstanceHolder mReactInstanceHolder;
private static CodePush mCurrentInstance;


public CodePush(String deploymentKey, Context context) {
this(deploymentKey, context, false);
}
Expand Down Expand Up @@ -109,6 +111,20 @@ public CodePush(String deploymentKey, Context context, boolean isDebugMode, Stri
mServerUrl = serverUrl;
}

static ReactHost getReactHostFromHolder() {
if (mReactInstanceHolder == null) {
return null;
}
return mReactInstanceHolder.getReactHost();
}

public static CodePush getInstance(String deploymentKey, Context context, boolean isDebugMode) {
if (mCurrentInstance == null) {
mCurrentInstance = new CodePush(deploymentKey,context,isDebugMode);
}
return mCurrentInstance;
}

private String getPublicKeyByResourceDescriptor(int publicKeyResourceDescriptor){
String publicKey;
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import android.view.View;

import com.facebook.react.ReactApplication;
import com.facebook.react.ReactHost;
import com.facebook.react.ReactInstanceManager;
import com.facebook.react.ReactRootView;
import com.facebook.react.bridge.Arguments;
Expand All @@ -23,6 +24,7 @@
import com.facebook.react.modules.core.ChoreographerCompat;
import com.facebook.react.modules.core.DeviceEventManagerModule;
import com.facebook.react.modules.core.ReactChoreographer;
import com.facebook.react.runtime.ReactHostDelegate;

import org.json.JSONArray;
import org.json.JSONException;
Expand Down Expand Up @@ -129,10 +131,30 @@ private void setJSBundle(ReactInstanceManager instanceManager, String latestJSBu
}
}

private void setJSBundle(ReactHost host, String latestJSBundleFile) throws {
try {
Field reactHostDelegate = host.getClass().getDeclaredField("mReactHostDelegate");
reactHostDelegate.setAccessible(true);
ReactHostDelegate delegate = (ReactHostDelegate) reactHostDelegate.get(host);
Field loader = delegate.getClass().getDeclaredField("jsBundleLoader");
loader.setAccessible(true);
JSBundleLoader latestJSBundleLoader;
if (latestJSBundleFile.toLowerCase().startsWith("assets://")) {
latestJSBundleLoader = JSBundleLoader.createAssetLoader(getReactApplicationContext(), latestJSBundleFile, true);
} else {
latestJSBundleLoader = JSBundleLoader.createFileLoader(latestJSBundleFile);
}
loader.set(delegate,latestJSBundleLoader);
} catch (Exception e) {
CodePushUtils.log("Unable to set JSBundle - CodePush may not support this version of React Native");
throw new IllegalAccessException("Could not setJSBundle");
}
}

private void loadBundle() {
clearLifecycleEventListener();
try {
mCodePush.clearDebugCacheIfNeeded(resolveInstanceManager());
// mCodePush.clearDebugCacheIfNeeded(resolveInstanceManager());
} catch(Exception e) {
// If we got error in out reflection we should clear debug cache anyway.
mCodePush.clearDebugCacheIfNeeded(null);
Expand All @@ -141,15 +163,15 @@ private void loadBundle() {
try {
// #1) Get the ReactInstanceManager instance, which is what includes the
// logic to reload the current React context.
final ReactInstanceManager instanceManager = resolveInstanceManager();
if (instanceManager == null) {
return;
}
// final ReactInstanceManager instanceManager = resolveInstanceManager();
// if (instanceManager == null) {
// return;
// }

String latestJSBundleFile = mCodePush.getJSBundleFileInternal(mCodePush.getAssetsBundleFileName());

// #2) Update the locally stored JS bundle file path
setJSBundle(instanceManager, latestJSBundleFile);
// setJSBundle(instanceManager, latestJSBundleFile);

// #3) Get the context creation method and fire it on the UI thread (which RN enforces)
new Handler(Looper.getMainLooper()).post(new Runnable() {
Expand All @@ -159,10 +181,11 @@ public void run() {
// We don't need to resetReactRootViews anymore
// due the issue https://github.com/facebook/react-native/issues/14533
// has been fixed in RN 0.46.0
//resetReactRootViews(instanceManager);

instanceManager.recreateReactContextInBackground();
mCodePush.initializeUpdateAfterRestart();
// resetReactRootViews(instanceManager);
ReactHost host = CodePush.getReactHostFromHolder();
CodePush.getReactHostFromHolder().reload("Restart-app");
// instanceManager.recreateReactContextInBackground();
// mCodePush.initializeUpdateAfterRestart();
} catch (Exception e) {
// The recreation method threw an unknown exception
// so just simply fallback to restarting the Activity (if it exists)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.microsoft.codepush.react;

import com.facebook.react.ReactHost;
import com.facebook.react.ReactInstanceManager;

/**
Expand All @@ -14,4 +15,5 @@ public interface ReactInstanceHolder {
* Get the current {@link ReactInstanceManager} instance. May return null.
*/
ReactInstanceManager getReactInstanceManager();
ReactHost getReactHost();
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@
},
"rnpm": {
"android": {
"packageInstance": "new CodePush(getResources().getString(R.string.CodePushDeploymentKey), getApplicationContext(), BuildConfig.DEBUG)"
"packageInstance": "CodePush.getInstance(getResources().getString(R.string.CodePushDeploymentKey), getApplicationContext(), BuildConfig.DEBUG)"
},
"ios": {
"sharedLibraries": [
Expand Down
2 changes: 1 addition & 1 deletion react-native.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module.exports = {
platforms: {
android: {
packageInstance:
"new CodePush(getResources().getString(R.string.CodePushDeploymentKey), getApplicationContext(), BuildConfig.DEBUG)"
"CodePush.getInstance(getResources().getString(R.string.CodePushDeploymentKey), getApplicationContext(), BuildConfig.DEBUG)"
}
}
}
Expand Down

0 comments on commit 1f8edcc

Please sign in to comment.