Skip to content

Commit

Permalink
fix: add reason to react-native patch package (#256)
Browse files Browse the repository at this point in the history
  • Loading branch information
huytran-finx authored Dec 23, 2023
1 parent 1b8a989 commit 62143b2
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 9 deletions.
23 changes: 19 additions & 4 deletions android/src/main/java/com/reactnativerestart/RestartModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,14 @@
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import com.jakewharton.processphoenix.ProcessPhoenix;
import com.facebook.react.bridge.Promise;


public class RestartModule extends ReactContextBaseJavaModule {

private static final String REACT_APPLICATION_CLASS_NAME = "com.facebook.react.ReactApplication";
private static final String REACT_NATIVE_HOST_CLASS_NAME = "com.facebook.react.ReactNativeHost";
private static String restartReason = null;

private LifecycleEventListener mLifecycleEventListener = null;

Expand Down Expand Up @@ -95,14 +98,26 @@ private void clearLifecycleEventListener() {
}

@ReactMethod
public void Restart() {
ProcessPhoenix.triggerRebirth(getReactApplicationContext());
public void Restart(String reason) {
restartReason = reason;
loadBundle();
}

@ReactMethod
public void restart() {
ProcessPhoenix.triggerRebirth(getReactApplicationContext());
public void restart(String reason) {
restartReason = reason;
loadBundle();
}

@ReactMethod
public void getReason(Promise promise) {
try {
promise.resolve(restartReason);
} catch(Exception e) {
promise.reject("Create Event Error", e);
}
}


@Override
public String getName() {
Expand Down
7 changes: 5 additions & 2 deletions ios/Restart.m
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@
@implementation Restart

RCT_EXPORT_MODULE(RNRestart)
NSString *restartReason = nil;

- (void)loadBundle
{
RCTTriggerReloadCommandListeners(@"react-native-restart: Restart");
}

RCT_EXPORT_METHOD(Restart) {
RCT_EXPORT_METHOD(Restart: (NSString *)reason) {
restartReason = reason;
if ([NSThread isMainThread]) {
[self loadBundle];
} else {
Expand All @@ -20,7 +22,8 @@ - (void)loadBundle
return;
}

RCT_EXPORT_METHOD(restart) {
RCT_EXPORT_METHOD(restart: (NSString *)reason) {
restartReason = reason;
if ([NSThread isMainThread]) {
[self loadBundle];
} else {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-native-restart",
"version": "0.0.27",
"version": "0.0.28",
"description": "Sometimes you want to reload your app bundle during app runtime. This package will allow you to do it.",
"main": "lib/commonjs/index.js",
"module": "lib/module/index.js",
Expand Down
18 changes: 16 additions & 2 deletions src/index.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,27 @@
import { NativeModules } from "react-native";

const { RNRestart } = NativeModules;
const { RNRestart: rnRestart } = NativeModules;

type RestartType = {
/**
* @deprecated use `restart` instead
*/
Restart(): void;
restart(): void;
getReason(): Promise<string>;
};

export default RNRestart as RestartType;
const Restart = (reason?: string) => {
if (!reason) {
rnRestart.Restart(null);
} else {
rnRestart.Restart(reason);
}
};
const RNRestart: RestartType = {
...rnRestart,
restart: Restart,
Restart,
};

export default RNRestart;

0 comments on commit 62143b2

Please sign in to comment.