@sentiance-react-native/core 6.15.0-rc.3 → 6.15.0-rc.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -5,7 +5,7 @@ sentiance_sdk_env_var_version = ENV["SENTIANCE_RN_IOS_SDK_VERSION"]
5
5
 
6
6
  Pod::Spec.new do |s|
7
7
  s.name = "RNSentianceCore"
8
- s.version = "6.15.0-rc.3"
8
+ s.version = "6.15.0-rc.4"
9
9
  s.summary = "RNSentianceCore"
10
10
  s.description = <<-DESC
11
11
  RNSentianceCore
@@ -4,56 +4,53 @@ import android.content.Context;
4
4
  import android.os.Handler;
5
5
  import android.os.Looper;
6
6
 
7
- import com.facebook.react.ReactApplication;
8
- import com.facebook.react.ReactNativeHost;
7
+ import androidx.annotation.NonNull;
8
+ import androidx.annotation.Nullable;
9
+
9
10
  import com.facebook.react.bridge.ReactContext;
10
11
  import com.facebook.react.bridge.WritableMap;
11
12
  import com.facebook.react.modules.core.DeviceEventManagerModule;
13
+ import com.sentiance.react.bridge.core.common.util.ReactContextProvider;
12
14
 
13
15
  public abstract class AbstractSentianceEmitter {
14
16
 
15
- protected final Handler mHandler = new Handler(Looper.getMainLooper());
16
- protected ReactContext reactContext;
17
- protected ReactNativeHost reactNativeHost;
18
-
19
- protected AbstractSentianceEmitter(Context context) {
20
- ReactApplication reactApplication = ((ReactApplication) context.getApplicationContext());
21
- reactNativeHost = reactApplication.getReactNativeHost();
22
- reactContext = createReactContext();
23
- }
24
-
25
- protected ReactContext createReactContext() {
26
- if (!reactNativeHost.getReactInstanceManager().hasStartedCreatingInitialContext())
27
- reactNativeHost.getReactInstanceManager().createReactContextInBackground();
28
- return reactNativeHost.getReactInstanceManager().getCurrentReactContext();
29
- }
30
-
31
- protected void sendEvent(final String key, final WritableMap map) {
32
- if (reactContext != null && reactContext.hasActiveCatalystInstance()) {
33
- this.reactContext.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class).emit(key, map);
34
- } else {
35
- // add delay
36
- final Counter retry = new Counter(20);
37
- mHandler.postDelayed(new Runnable() {
38
- @Override
39
- public void run() {
40
- if (AbstractSentianceEmitter.this.reactContext == null)
41
- AbstractSentianceEmitter.this.reactContext = createReactContext();
42
- if (AbstractSentianceEmitter.this.reactContext != null && AbstractSentianceEmitter.this.reactContext.hasActiveCatalystInstance()) {
43
- AbstractSentianceEmitter.this.reactContext.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class).emit(key, map);
44
- } else if (retry.count-- > 0) {
45
- mHandler.postDelayed(this, 500);
46
- }
17
+ protected final Handler mHandler = new Handler(Looper.getMainLooper());
18
+ @Nullable
19
+ protected ReactContext reactContext;
20
+ @NonNull
21
+ protected ReactContextProvider reactContextProvider;
22
+
23
+ protected AbstractSentianceEmitter(Context context) {
24
+ reactContextProvider = new ReactContextProvider(context.getApplicationContext());
25
+ reactContext = reactContextProvider.createReactContext();
26
+ }
27
+
28
+ protected void sendEvent(final String key, final WritableMap map) {
29
+ if (reactContext != null && reactContext.hasActiveCatalystInstance()) {
30
+ this.reactContext.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class).emit(key, map);
31
+ } else {
32
+ // add delay
33
+ final Counter retry = new Counter(20);
34
+ mHandler.postDelayed(new Runnable() {
35
+ @Override
36
+ public void run() {
37
+ if (AbstractSentianceEmitter.this.reactContext == null)
38
+ AbstractSentianceEmitter.this.reactContext = reactContextProvider.createReactContext();
39
+ if (AbstractSentianceEmitter.this.reactContext != null && AbstractSentianceEmitter.this.reactContext.hasActiveCatalystInstance()) {
40
+ AbstractSentianceEmitter.this.reactContext.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class).emit(key, map);
41
+ } else if (retry.count-- > 0) {
42
+ mHandler.postDelayed(this, 500);
43
+ }
44
+ }
45
+ }, 500);
47
46
  }
48
- }, 500);
49
47
  }
50
- }
51
48
 
52
- private static class Counter {
53
- int count;
49
+ private static class Counter {
50
+ int count;
54
51
 
55
- Counter(int count) {
56
- this.count = count;
52
+ Counter(int count) {
53
+ this.count = count;
54
+ }
57
55
  }
58
- }
59
56
  }
@@ -0,0 +1,94 @@
1
+ package com.sentiance.react.bridge.core.common.util;
2
+
3
+ import android.content.Context;
4
+ import android.util.Log;
5
+
6
+ import androidx.annotation.Nullable;
7
+
8
+ import com.facebook.react.ReactApplication;
9
+ import com.facebook.react.ReactNativeHost;
10
+ import com.facebook.react.bridge.ReactContext;
11
+
12
+ public final class ReactContextProvider {
13
+
14
+ private static final String TAG = "ReactNativeContextProvider";
15
+ private static Boolean isNewArchEnabled = null;
16
+
17
+ private final Context mApplicationContext;
18
+ private final ReactApplication mReactApplication;
19
+
20
+ public ReactContextProvider(Context applicationContext) {
21
+ mApplicationContext = applicationContext;
22
+ mReactApplication = (ReactApplication) mApplicationContext;
23
+
24
+ if (isNewArchEnabled == null) {
25
+ isNewArchEnabled = isNewArchEnabled();
26
+ }
27
+ }
28
+
29
+ /**
30
+ * Triggers the initialization of a React context if it is not initializing already,
31
+ * or returns the current React context if it is already initialized.
32
+ *
33
+ * If this call does end up triggering the initialization of a React context,
34
+ * then it will probably return <code>null</code> since the operation is asynchronous.
35
+ *
36
+ * @return a ReactContext if it exists, null otherwise.
37
+ */
38
+ @Nullable
39
+ public ReactContext createReactContext() {
40
+ if (isNewArchEnabled) {
41
+ return createReactContextInNewArchMode();
42
+ } else {
43
+ return createReactContextInLegacyArchMode();
44
+ }
45
+ }
46
+
47
+ @Nullable
48
+ private ReactContext createReactContextInNewArchMode() {
49
+ try {
50
+ java.lang.reflect.Method getReactHost = mReactApplication.getClass().getMethod("getReactHost");
51
+ Object reactHost = getReactHost.invoke(mReactApplication);
52
+ Class<?> reactHostClass = Class.forName("com.facebook.react.ReactHost");
53
+
54
+ java.lang.reflect.Method start = reactHostClass.getMethod("start");
55
+ java.lang.reflect.Method getCurrentReactContext = reactHostClass.getMethod("getCurrentReactContext");
56
+
57
+ // Trigger the initialization of a React context, should be safe to call multiple times
58
+ // https://github.com/facebook/react-native/blob/main/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/runtime/ReactHostImpl.kt#L723
59
+ start.invoke(reactHost);
60
+
61
+ return (ReactContext) getCurrentReactContext.invoke(reactHost);
62
+ } catch (Throwable t) {
63
+ Log.d(TAG, "Failed to create React context");
64
+ return null;
65
+ }
66
+ }
67
+
68
+ @Nullable
69
+ private ReactContext createReactContextInLegacyArchMode() {
70
+ ReactNativeHost reactNativeHost = mReactApplication.getReactNativeHost();
71
+
72
+ // Trigger the initialization of a React context
73
+ if (!reactNativeHost.getReactInstanceManager().hasStartedCreatingInitialContext())
74
+ reactNativeHost.getReactInstanceManager().createReactContextInBackground();
75
+
76
+ return reactNativeHost.getReactInstanceManager().getCurrentReactContext();
77
+ }
78
+
79
+ private boolean isNewArchEnabled() {
80
+ try {
81
+ String pkg = mApplicationContext.getPackageName();
82
+ Class<?> cfg = Class.forName(pkg + ".BuildConfig");
83
+ return cfg.getField("IS_NEW_ARCHITECTURE_ENABLED").getBoolean(null);
84
+ } catch (Throwable ignore) { /* app wasn't generated with the flag */ }
85
+
86
+ try {
87
+ Class<?> cfg = Class.forName("com.facebook.react.BuildConfig");
88
+ return cfg.getField("IS_NEW_ARCHITECTURE_ENABLED").getBoolean(null);
89
+ } catch (Throwable ignore) {
90
+ }
91
+
92
+ return false;
93
+ }
94
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sentiance-react-native/core",
3
- "version": "6.15.0-rc.3",
3
+ "version": "6.15.0-rc.4",
4
4
  "description": "The Sentiance Core library",
5
5
  "main": "lib/index.js",
6
6
  "typings": "lib/index.d.ts",