Skip to content

Commit 98d5236

Browse files
a7medevHeshamMegid
authored andcommitted
[MOB-10486] Change APIs with Callbacks to Promises (#948)
1 parent 0c67747 commit 98d5236

40 files changed

+380
-224
lines changed

CHANGELOG.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,29 @@
88

99
### Changed
1010

11+
- Return a `Promise` from the below APIs ([#948](https://github.com/Instabug/Instabug-React-Native/pull/948)):
12+
13+
- `Instabug.getTags`
14+
- `Instabug.getUserAttribute`
15+
- `Instabug.getAllUserAttributes`
16+
- `Replies.hasChats`
17+
- `Replies.getUnreadRepliesCount`
18+
- `Surveys.getAvailableSurveys`
19+
- `Surveys.hasRespondedToSurvey`
20+
21+
You should not pass it a callback but use the returned `Promise` as follows:
22+
23+
```js
24+
const tags = await Instabug.getTags();
25+
```
26+
1127
- Improve release variant's build time on Android, by using the react-native-generated source map file, instead of generating it within our scripts ([#938](https://github.com/Instabug/Instabug-React-Native/pull/938)).
1228
- Improve debug variant's build time on iOS, by disabling automatic source map file uploads ([#942](https://github.com/Instabug/Instabug-React-Native/pull/942)).
1329

30+
### Deprecated
31+
32+
- Deprecate the callback parameter in the aforementioned methods ([#948](https://github.com/Instabug/Instabug-React-Native/pull/948)).
33+
1434
## [11.9.1](https://github.com/Instabug/Instabug-React-Native/compare/v11.9.0...v11.9.1) (March 01, 2023)
1535

1636
### Changed

android/src/main/java/com/instabug/reactlibrary/RNInstabugAPMModule.java

Lines changed: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,24 @@
11

22
package com.instabug.reactlibrary;
33

4-
import android.os.Handler;
5-
import android.os.Looper;
64
import android.os.SystemClock;
75
import android.util.Log;
86

9-
import com.facebook.react.bridge.Callback;
7+
import com.facebook.react.bridge.Promise;
108
import com.facebook.react.bridge.ReactApplicationContext;
119
import com.facebook.react.bridge.ReactContextBaseJavaModule;
1210
import com.facebook.react.bridge.ReactMethod;
1311
import com.instabug.apm.APM;
1412
import com.instabug.apm.model.ExecutionTrace;
15-
import com.instabug.apm.model.LogLevel;
1613
import com.instabug.apm.networking.APMNetworkLogger;
17-
import com.instabug.bug.BugReporting;
18-
import com.instabug.library.Feature;
19-
import com.instabug.reactlibrary.utils.InstabugUtil;
2014
import com.instabug.reactlibrary.utils.MainThreadHandler;
2115

2216
import org.json.JSONException;
2317
import org.json.JSONObject;
24-
25-
import java.io.File;
2618
import java.lang.reflect.InvocationTargetException;
2719
import java.lang.reflect.Method;
2820

29-
import java.io.File;
30-
import java.lang.reflect.InvocationTargetException;
31-
import java.lang.reflect.Method;
32-
import java.util.ArrayList;
3321
import java.util.HashMap;
34-
import java.util.Iterator;
35-
import java.util.LinkedHashMap;
36-
import java.util.List;
37-
import java.util.Locale;
38-
import java.util.Map;
39-
import com.instabug.library.Platform;
4022

4123
import javax.annotation.Nonnull;
4224

@@ -167,7 +149,7 @@ public void run() {
167149
* @param name string name of the trace.
168150
*/
169151
@ReactMethod
170-
public void startExecutionTrace(final String name, final String id, final Callback callback) {
152+
public void startExecutionTrace(final String name, final String id, final Promise promise) {
171153
MainThreadHandler.runOnMainThread(new Runnable() {
172154
@Override
173155
public void run() {
@@ -178,9 +160,10 @@ public void run() {
178160
result = id;
179161
traces.put(id, trace);
180162
}
181-
callback.invoke(result);
163+
promise.resolve(result);
182164
} catch (Exception e) {
183165
e.printStackTrace();
166+
promise.resolve(null);
184167
}
185168
}
186169
});

android/src/main/java/com/instabug/reactlibrary/RNInstabugReactnativeModule.java

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
import com.facebook.react.bridge.Arguments;
1111
import com.facebook.react.bridge.Callback;
12+
import com.facebook.react.bridge.Promise;
1213
import com.facebook.react.bridge.ReactApplicationContext;
1314
import com.facebook.react.bridge.ReactMethod;
1415
import com.facebook.react.bridge.ReadableArray;
@@ -289,22 +290,20 @@ public void run() {
289290
* @see #resetTags()
290291
*/
291292
@ReactMethod
292-
public void getTags(final Callback tagsCallback) {
293+
public void getTags(final Promise promise) {
293294
MainThreadHandler.runOnMainThread(new Runnable() {
294295
@Override
295296
public void run() {
296-
WritableArray tagsArray;
297+
WritableArray tagsArray = Arguments.createArray();
297298
try {
298299
ArrayList<String> tags = Instabug.getTags();
299-
tagsArray = Arguments.createArray();
300300
for (int i = 0; i < tags.size(); i++) {
301301
tagsArray.pushString(tags.get(i));
302302
}
303-
tagsCallback.invoke(tagsArray);
304-
305303
} catch (Exception e) {
306304
e.printStackTrace();
307305
}
306+
promise.resolve(tagsArray);
308307
}
309308
});
310309
}
@@ -481,17 +480,17 @@ public void run() {
481480
* @return the desired user attribute
482481
*/
483482
@ReactMethod
484-
public void getUserAttribute(final String key, final Callback userAttributeCallback) {
483+
public void getUserAttribute(final String key, final Promise promise) {
485484
MainThreadHandler.runOnMainThread(new Runnable() {
486485
@Override
487486
public void run() {
488-
String userAttribute;
487+
String userAttribute = "";
489488
try {
490489
userAttribute = Instabug.getUserAttribute(key);
491-
userAttributeCallback.invoke(userAttribute);
492490
} catch (Exception e) {
493491
e.printStackTrace();
494492
}
493+
promise.resolve(userAttribute);
495494
}
496495
});
497496
}
@@ -522,7 +521,7 @@ public void run() {
522521
* @return all user attributes as HashMap<String, String>
523522
*/
524523
@ReactMethod
525-
public void getAllUserAttributes(final Callback userAttributesCallback) {
524+
public void getAllUserAttributes(final Promise promise) {
526525
MainThreadHandler.runOnMainThread(new Runnable() {
527526
@Override
528527
public void run() {
@@ -535,7 +534,7 @@ public void run() {
535534
} catch (Exception e) {
536535
e.printStackTrace();
537536
}
538-
userAttributesCallback.invoke(writableMap);
537+
promise.resolve(writableMap);
539538
}
540539
});
541540
}

android/src/main/java/com/instabug/reactlibrary/RNInstabugRepliesModule.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.instabug.reactlibrary;
22

33
import com.facebook.react.bridge.Callback;
4+
import com.facebook.react.bridge.Promise;
45
import com.facebook.react.bridge.ReactApplicationContext;
56
import com.facebook.react.bridge.ReactMethod;
67
import com.facebook.react.bridge.ReadableMap;
@@ -56,12 +57,12 @@ public void run() {
5657
}
5758

5859
@ReactMethod
59-
public void hasChats(final Callback callback) {
60+
public void hasChats(final Promise promise) {
6061
MainThreadHandler.runOnMainThread(new Runnable() {
6162
@Override
6263
public void run() {
6364
boolean hasChats = Replies.hasChats();
64-
callback.invoke(hasChats);
65+
promise.resolve(hasChats);
6566
}
6667
});
6768
}
@@ -103,7 +104,7 @@ public void run() {
103104
* @return number of messages that are unread for this user
104105
*/
105106
@ReactMethod
106-
public void getUnreadRepliesCount(final Callback messageCountCallback) {
107+
public void getUnreadRepliesCount(final Promise promise) {
107108
MainThreadHandler.runOnMainThread(new Runnable() {
108109
@Override
109110
public void run() {
@@ -114,7 +115,7 @@ public void run() {
114115
e.printStackTrace();
115116
}
116117

117-
messageCountCallback.invoke(unreadMessages);
118+
promise.resolve(unreadMessages);
118119
}
119120
});
120121
}

android/src/main/java/com/instabug/reactlibrary/RNInstabugSurveysModule.java

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package com.instabug.reactlibrary;
22

3+
import com.facebook.react.bridge.Arguments;
34
import com.facebook.react.bridge.Callback;
5+
import com.facebook.react.bridge.Promise;
46
import com.facebook.react.bridge.ReactApplicationContext;
57
import com.facebook.react.bridge.ReactMethod;
68
import com.facebook.react.bridge.WritableArray;
@@ -46,22 +48,22 @@ public void removeListeners(Integer count) {
4648
* Will return false if the token does not exist or if the survey was not answered before.
4749
*
4850
* @param surveyToken the attribute key as string
49-
* @param hasRespondedCallback A callback that gets invoked with the returned value of whether
51+
* @param promise A promise that gets resolved with the returned value of whether
5052
* the user has responded to the survey or not.
5153
* @return the desired value of whether the user has responded to the survey or not.
5254
*/
5355
@ReactMethod
54-
public void hasRespondedToSurvey(final String surveyToken, final Callback hasRespondedCallback) {
56+
public void hasRespondedToSurvey(final String surveyToken, final Promise promise) {
5557
MainThreadHandler.runOnMainThread(new Runnable() {
5658
@Override
5759
public void run() {
58-
boolean hasResponded;
60+
boolean hasResponded = false;
5961
try {
6062
hasResponded = Surveys.hasRespondToSurvey(surveyToken);
61-
hasRespondedCallback.invoke(hasResponded);
6263
} catch (Exception e) {
6364
e.printStackTrace();
6465
}
66+
promise.resolve(hasResponded);
6567
}
6668
});
6769
}
@@ -175,21 +177,20 @@ public void onDismiss() {
175177

176178
/**
177179
* Returns an array containing the available surveys.
178-
* @param availableSurveysCallback - Callback with the returned value of the available surveys
179-
*
180180
*/
181181
@ReactMethod
182-
public void getAvailableSurveys(final Callback availableSurveysCallback) {
182+
public void getAvailableSurveys(final Promise promise) {
183183
MainThreadHandler.runOnMainThread(new Runnable() {
184184
@Override
185185
public void run() {
186186
try {
187187
List<Survey> availableSurveys = Surveys.getAvailableSurveys();
188188
JSONArray surveysArray = InstabugUtil.surveyObjectToJson(availableSurveys);
189189
WritableArray array = ArrayUtil.convertJsonToWritableArray(surveysArray);
190-
availableSurveysCallback.invoke(array);
190+
promise.resolve(array);
191191
} catch (Exception e) {
192192
e.printStackTrace();
193+
promise.resolve(Arguments.createArray());
193194
}
194195
}
195196
});

android/src/test/java/com/instabug/reactlibrary/RNInstabugAPMModuleTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import android.os.Looper;
33

44
import com.facebook.react.bridge.Callback;
5+
import com.facebook.react.bridge.Promise;
56
import com.instabug.apm.APM;
67

78
import com.instabug.reactlibrary.utils.MainThreadHandler;
@@ -116,14 +117,13 @@ public void givenTruesetEnabled_whenQuery_thenShouldCallNativeApiWithEnabled() {
116117

117118
@Test
118119
public void givenString$startExecutionTrace_whenQuery_thenShouldCallNativeApi() {
119-
120-
Callback callback = mock(Callback.class);
120+
Promise promise = mock(Promise.class);
121121
// when
122-
apmModule.startExecutionTrace("trace", "1", callback);
122+
apmModule.startExecutionTrace("trace", "1", promise);
123123
// then
124124
verify(APM.class, times(1));
125125
APM.startExecutionTrace("trace");
126-
verify(callback).invoke(any());
126+
verify(promise).resolve(any());
127127
}
128128

129129
// @Test

android/src/test/java/com/instabug/reactlibrary/RNInstabugReactnativeModuleTest.java

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import com.facebook.react.bridge.Callback;
1111
import com.facebook.react.bridge.JavaOnlyArray;
1212
import com.facebook.react.bridge.JavaOnlyMap;
13+
import com.facebook.react.bridge.Promise;
1314
import com.facebook.react.bridge.WritableArray;
1415
import com.facebook.react.bridge.WritableMap;
1516
import com.instabug.library.Feature;
@@ -368,62 +369,62 @@ public void tearDown() {
368369
}
369370

370371
@Test
371-
public void givenCallback$getTags_whenQuery_thenShouldCallNativeApiAndInvokeCallback() {
372+
public void givenCallback$getTags_whenQuery_thenShouldCallNativeApiAndResolvePromise() {
372373
// given
373374

374375
MockedStatic mockArgument = mockStatic(Arguments.class);
375-
Callback callback = mock(Callback.class);
376+
Promise promise = mock(Promise.class);
376377
// when
377378
ArrayList<String> tags = new ArrayList<>();
378379
tags.add("tag1");
379380
tags.add("tag2");
380381
when(Instabug.getTags()).thenReturn(tags);
381382
when(Arguments.createArray()).thenReturn(new JavaOnlyArray());
382-
rnModule.getTags(callback);
383+
rnModule.getTags(promise);
383384
// then
384385
verify(Instabug.class,times(1));
385386
Instabug.getTags();
386387
WritableArray expectedArray = new JavaOnlyArray();
387388
expectedArray.pushString("tag1");
388389
expectedArray.pushString("tag2");
389-
verify(callback).invoke(expectedArray);
390+
verify(promise).resolve(expectedArray);
390391
mockArgument.close();
391392

392393
}
393394

394395
@Test
395-
public void givenArgs$getUserAttribute_whenQuery_thenShouldCallNativeApiAndInvokeCallback() {
396+
public void givenArgs$getUserAttribute_whenQuery_thenShouldCallNativeApiAndResolvePromise() {
396397
// given
397398

398-
Callback callback = mock(Callback.class);
399+
Promise promise = mock(Promise.class);
399400
// when
400401
String key = "company";
401402
String value = "Instabug";
402403
when(Instabug.getUserAttribute(key)).thenReturn(value);
403-
rnModule.getUserAttribute(key, callback);
404+
rnModule.getUserAttribute(key, promise);
404405
// then
405406
verify(Instabug.class,times(1));
406407
Instabug.getUserAttribute(key);
407-
verify(callback).invoke(value);
408+
verify(promise).resolve(value);
408409
}
409410

410411
@Test
411-
public void givenCallback$getAllUserAttributes_whenQuery_thenShouldCallNativeApiAndInvokeCallback() {
412+
public void givenCallback$getAllUserAttributes_whenQuery_thenShouldCallNativeApiAndResolvePromise() {
412413
// given
413414
MockedStatic mockArgument = mockStatic(Arguments.class);
414-
Callback callback = mock(Callback.class);
415+
Promise promise = mock(Promise.class);
415416
// when
416417
HashMap<String, String> userAttributes = new HashMap<>();
417418
userAttributes.put("email", "[email protected]");
418419
when(Arguments.createMap()).thenReturn(new JavaOnlyMap());
419420
when(Instabug.getAllUserAttributes()).thenReturn(userAttributes);
420-
rnModule.getAllUserAttributes(callback);
421+
rnModule.getAllUserAttributes(promise);
421422
// then
422423
verify(Instabug.class,times(1));
423424
Instabug.getAllUserAttributes();
424425
WritableMap expectedMap = new JavaOnlyMap();
425426
expectedMap.putString("email", "[email protected]");
426-
verify(callback).invoke(expectedMap);
427+
verify(promise).resolve(expectedMap);
427428
mockArgument.close();
428429
}
429430

0 commit comments

Comments
 (0)