Skip to content
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

fix: Headless tasks in bridgeless mode (fixes #44255) #45100

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,13 @@
import android.content.Intent;
import android.os.IBinder;
import android.os.PowerManager;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.facebook.infer.annotation.Assertions;
import com.facebook.react.bridge.ReactContext;
import com.facebook.react.bridge.UiThreadUtil;
import com.facebook.react.defaults.DefaultNewArchitectureEntryPoint;
import com.facebook.react.jstasks.HeadlessJsTaskConfig;
import com.facebook.react.jstasks.HeadlessJsTaskContext;
import com.facebook.react.jstasks.HeadlessJsTaskEventListener;
Expand Down Expand Up @@ -94,19 +97,11 @@ public static void acquireWakeLockNow(Context context) {
protected void startTask(final HeadlessJsTaskConfig taskConfig) {
UiThreadUtil.assertOnUiThread();
acquireWakeLockNow(this);
final ReactInstanceManager reactInstanceManager =
getReactNativeHost().getReactInstanceManager();
ReactContext reactContext = reactInstanceManager.getCurrentReactContext();

ReactContext reactContext = getReactContext();

if (reactContext == null) {
reactInstanceManager.addReactInstanceEventListener(
new ReactInstanceEventListener() {
@Override
public void onReactContextInitialized(ReactContext reactContext) {
invokeStartTask(reactContext, taskConfig);
reactInstanceManager.removeReactInstanceEventListener(this);
}
});
reactInstanceManager.createReactContextInBackground();
createReactContextAndScheduleTask(taskConfig);
} else {
invokeStartTask(reactContext, taskConfig);
}
Expand Down Expand Up @@ -166,4 +161,55 @@ public void onHeadlessJsTaskFinish(int taskId) {
protected ReactNativeHost getReactNativeHost() {
return ((ReactApplication) getApplication()).getReactNativeHost();
}

/**
* Get the {@link ReactHost} used by this app. By default, assumes {@link #getApplication()}
* is an instance of {@link ReactApplication} and calls {@link
* ReactApplication#getReactHost()}.
* This method assumes it is called in new architecture and returns null if not.
*/
protected ReactHost getReactHost() {
return ((ReactApplication) getApplication()).getReactHost();
}

protected ReactContext getReactContext() {
if (DefaultNewArchitectureEntryPoint.getBridgelessEnabled()) {
arushikesarwani94 marked this conversation as resolved.
Show resolved Hide resolved
ReactHost reactHost = getReactHost();
Assertions.assertNotNull(reactHost, "React host is null in newArchitecture");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Assertions.assertNotNull(reactHost, "React host is null in newArchitecture");
Assertions.assertNotNull(reactHost, "getReactHost() is null in New Architecture");

return reactHost.getCurrentReactContext();
}

final ReactInstanceManager reactInstanceManager =
getReactNativeHost().getReactInstanceManager();
return reactInstanceManager.getCurrentReactContext();
Comment on lines +182 to +184
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

put this in the else branch pls

}

private void createReactContextAndScheduleTask(final HeadlessJsTaskConfig taskConfig) {
final ReactHost reactHost = getReactHost();

if (reactHost == null) { // old arch
final ReactInstanceManager reactInstanceManager = getReactNativeHost().getReactInstanceManager();

reactInstanceManager.addReactInstanceEventListener(
new ReactInstanceEventListener() {
@Override
public void onReactContextInitialized(@NonNull ReactContext reactContext) {
invokeStartTask(reactContext, taskConfig);
reactInstanceManager.removeReactInstanceEventListener(this);
}
});
reactInstanceManager.createReactContextInBackground();
} else { // new arch
reactHost.addReactInstanceEventListener(
new ReactInstanceEventListener() {
@Override
public void onReactContextInitialized(@NonNull ReactContext reactContext) {
invokeStartTask(reactContext, taskConfig);
reactHost.removeReactInstanceEventListener(this);
}
}
);
reactHost.start();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,10 @@ public interface ReactHost {

public fun onConfigurationChanged(context: Context)

public fun addReactInstanceEventListener(listener: ReactInstanceEventListener)

public fun removeReactInstanceEventListener(listener: ReactInstanceEventListener)

public fun addBeforeDestroyListener(onBeforeDestroy: () -> Unit)

public fun removeBeforeDestroyListener(onBeforeDestroy: () -> Unit)
Expand Down
Loading