Skip to content

Commit

Permalink
Add check to verify that catalystInstance is active before require mo…
Browse files Browse the repository at this point in the history
…dule. Remove duplicated code.
  • Loading branch information
EnricoMazzu committed Sep 18, 2024
1 parent bffb31f commit 24d9eaa
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,11 @@

public class JsIOHelper {
public boolean sendEventToJS(String eventName, Bundle data, ReactContext reactContext) {
if (reactContext != null) {
sendEventToJS(eventName, Arguments.fromBundle(data), reactContext);
return true;
}
return false;
return sendEventToJS(eventName, Arguments.fromBundle(data), reactContext);
}

public boolean sendEventToJS(String eventName, WritableMap data, ReactContext reactContext) {
if (reactContext != null) {
if (reactContext != null && reactContext.hasActiveCatalystInstance()) {
reactContext.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class).emit(eventName, data);
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,12 +90,10 @@ protected void refreshToken() {
protected void sendTokenToJS() {
final ReactInstanceManager instanceManager = ((ReactApplication) mAppContext).getReactNativeHost().getReactInstanceManager();
final ReactContext reactContext = instanceManager.getCurrentReactContext();
Bundle tokenMap = new Bundle();
tokenMap.putString("deviceToken", sToken);
// mJsIOHelper is safe now when context is null or react instance is not active
mJsIOHelper.sendEventToJS(TOKEN_RECEIVED_EVENT_NAME, tokenMap, reactContext);

// Note: Cannot assume react-context exists cause this is an async dispatched service.
if (reactContext != null && reactContext.hasActiveCatalystInstance()) {
Bundle tokenMap = new Bundle();
tokenMap.putString("deviceToken", sToken);
mJsIOHelper.sendEventToJS(TOKEN_RECEIVED_EVENT_NAME, tokenMap, reactContext);
}
}
}

0 comments on commit 24d9eaa

Please sign in to comment.