Files
react-native/ReactAndroid/src/main/java/com/facebook/react/bridge/GuardedResultAsyncTask.java
T
Emily Janzer 83969c26fb Decouple GuardedRunnable + friends from ReactContext
Summary:
Step 1 in removing the dependency on ReactContext from GuardedRunnable and other related classes. These are extensively by native modules and view managers, so in order to remove the bridge dependency from those modules we'll need to first decouple these classes from ReactContext. It turns out they only need ReactContext for its handleException method, which delegates out to product code. For backwards compatibility I'm exposing another NativeModuleExceptionManager in ReactContext that simply wraps its handleException method (since this interface already does everything we need).

I figured I'd keep around an extra constructor that still uses ReactContext for now instead of trying to migrate everything over at once.

Reviewed By: makovkastar

Differential Revision: D16270995

fbshipit-source-id: c9a8714bea7ac2a98e78234a0bae49140c00980d
2019-07-16 14:05:19 -07:00

51 lines
1.4 KiB
Java

// Copyright (c) Facebook, Inc. and its 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.os.AsyncTask;
/**
* Abstract base for a AsyncTask with result support that should have any RuntimeExceptions it
* throws handled by the {@link com.facebook.react.bridge.NativeModuleCallExceptionHandler}
* registered if the app is in dev mode.
*/
public abstract class GuardedResultAsyncTask<Result> extends AsyncTask<Void, Void, Result> {
private final NativeModuleCallExceptionHandler mExceptionHandler;
@Deprecated
protected GuardedResultAsyncTask(ReactContext reactContext) {
this(reactContext.getExceptionHandler());
}
protected GuardedResultAsyncTask(NativeModuleCallExceptionHandler exceptionHandler) {
mExceptionHandler = exceptionHandler;
}
@Override
protected final Result doInBackground(Void... params) {
try {
return doInBackgroundGuarded();
} catch (RuntimeException e) {
mExceptionHandler.handleException(e);
throw e;
}
}
@Override
protected final void onPostExecute(Result result) {
try {
onPostExecuteGuarded(result);
} catch (RuntimeException e) {
mExceptionHandler.handleException(e);
}
}
protected abstract Result doInBackgroundGuarded();
protected abstract void onPostExecuteGuarded(Result result);
}