react-native-hyperkyc-sdk 2.7.0 → 2.8.0

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.
@@ -37,10 +37,41 @@ public class HyperkycSdkModule extends ReactContextBaseJavaModule {
37
37
  private final ReactApplicationContext reactContext;
38
38
  private final int HYPERKYC_REQUEST_CODE = 56455;
39
39
  private boolean isEventListenerAdded = false;
40
+ /* volatile because
41
+ 1. the pendingCallback is written on JS bridge thread (launch()),
42
+ 2. read/nulled on main thread (onActivityResult),
43
+ 3. nulled on teardown thread (invalidate()).
44
+ Without volatile, the JMM provides no happens-before guarantee — the main thread may cache a stale null
45
+ and silently drop the KYC result. Works in practice without volatile (user spending more time in completing the workflow between write and read),
46
+ but timing is not a JMM guarantee. */
47
+ private volatile Callback pendingCallback = null;
48
+ /* Contract is stateless — createIntent and parseResult carry no per-launch state. */
49
+ private final HyperKyc.Contract contract = new HyperKyc.Contract();
50
+ /* Registered once at construction; never re-registered. Accumulation is impossible. */
51
+ private final ActivityEventListener kycActivityListener = new ActivityEventListener() {
52
+ @Override
53
+ public void onActivityResult(Activity activity, int requestCode,
54
+ int resultCode, Intent data) {
55
+ if (requestCode != HYPERKYC_REQUEST_CODE) return;
56
+ Callback cb = pendingCallback;
57
+ if (cb == null) return;
58
+ pendingCallback = null;
59
+ HyperKycResult result = contract.parseResult(resultCode, data);
60
+ if (result == null) {
61
+ cb.invoke(getDummyResultMap());
62
+ } else {
63
+ parseKYCResult(result, cb);
64
+ }
65
+ }
66
+
67
+ @Override
68
+ public void onNewIntent(Intent intent) {}
69
+ };
40
70
 
41
71
  public HyperkycSdkModule(ReactApplicationContext reactContext) {
42
72
  super(reactContext);
43
73
  this.reactContext = reactContext;
74
+ reactContext.addActivityEventListener(kycActivityListener);
44
75
  }
45
76
 
46
77
  @Override
@@ -75,28 +106,15 @@ public class HyperkycSdkModule extends ReactContextBaseJavaModule {
75
106
  @ReactMethod
76
107
  public void launch(ReadableMap kycConfig, Callback resultCallback) {
77
108
  ReactActivity currentActivity = (ReactActivity) getCurrentActivity();
78
- assert kycConfig != null;
109
+ if (currentActivity == null || kycConfig == null) {
110
+ resultCallback.invoke(getDummyResultMap());
111
+ return;
112
+ }
79
113
  HyperKycConfig config = getHyperKycConfigFromMap(kycConfig);
80
- HyperKyc.Contract contract = new HyperKyc.Contract();
81
-
82
- reactContext.addActivityEventListener(new ActivityEventListener() {
83
- @Override
84
- public void onActivityResult(Activity activity, int requestCode, int resultCode, Intent data) {
85
- if (requestCode == HYPERKYC_REQUEST_CODE) {
86
- HyperKycResult hyperKycResult = contract.parseResult(resultCode, data);
87
- parseKYCResult(hyperKycResult, resultCallback);
88
- } else {
89
- resultCallback.invoke(getDummyResultMap());
90
- }
91
- reactContext.removeActivityEventListener(this);
92
- }
93
-
94
- @Override
95
- public void onNewIntent(Intent intent) {
96
- }
97
- });
98
114
 
115
+ /* Clear any accumulated event listeners before adding a fresh one, so at most one event listener is active at any time. */
99
116
  if (isEventListenerAdded) {
117
+ HyperKyc.removeAllEventListeners();
100
118
  Function1<JSONObject, Unit> listener = jsonObject -> {
101
119
  try {
102
120
  WritableMap eventMap = HyperkycSdkUtils.convertJsonToMap(jsonObject);
@@ -109,12 +127,19 @@ public class HyperkycSdkModule extends ReactContextBaseJavaModule {
109
127
  HyperKyc.addEventListener(listener);
110
128
  }
111
129
 
112
- if (currentActivity != null) {
113
- Intent newIntent = contract.createIntent(currentActivity, config);
114
- currentActivity.startActivityIfNeeded(newIntent, HYPERKYC_REQUEST_CODE);
115
- } else {
116
- resultCallback.invoke(getDummyResultMap());
117
- }
130
+ /* Supersede any prior in-flight session — old callback silently discarded. */
131
+ pendingCallback = resultCallback;
132
+
133
+ Intent newIntent = contract.createIntent(currentActivity, config);
134
+ currentActivity.startActivityIfNeeded(newIntent, HYPERKYC_REQUEST_CODE);
135
+ }
136
+
137
+ /* invalidate() is the correct teardown hook for both old and new RN architecture;
138
+ onCatalystInstanceDestroy() is deprecated and removed in some RN versions. */
139
+ @Override
140
+ public void invalidate() {
141
+ pendingCallback = null;
142
+ reactContext.removeActivityEventListener(kycActivityListener);
118
143
  }
119
144
 
120
145
  private void sendEvent(String eventName, WritableMap eventProperties) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-native-hyperkyc-sdk",
3
- "version": "2.7.0",
3
+ "version": "2.8.0",
4
4
  "description": "React Native wrapper for HyperKYC SDK",
5
5
  "main": "lib/commonjs/index",
6
6
  "module": "lib/module/index",