Skip to content

Commit 6135df3

Browse files
Return a Promise from APM.startExecutionTrace
1 parent a71904c commit 6135df3

File tree

3 files changed

+24
-6
lines changed

3 files changed

+24
-6
lines changed

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,13 +112,18 @@ public void run() {
112112
* @param name string name of the trace.
113113
*/
114114
@ReactMethod
115-
public void startExecutionTrace(final String name, final String id) {
115+
public void startExecutionTrace(final String name, final String id, final Callback callback) {
116116
MainThreadHandler.runOnMainThread(new Runnable() {
117117
@Override
118118
public void run() {
119119
try {
120+
String result = "";
120121
ExecutionTrace trace = APM.startExecutionTrace(name);
121-
traces.put(id,trace);
122+
if (trace == null) {
123+
result = id;
124+
traces.put(id, trace);
125+
}
126+
callback.invoke(result);
122127
} catch (Exception e) {
123128
e.printStackTrace();
124129
}

ios/RNInstabug/InstabugAPMBridge.m

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,13 @@ - (id) init
6262
IBGAPM.autoUITraceEnabled = isEnabled;
6363
}
6464

65-
RCT_EXPORT_METHOD(startExecutionTrace:(NSString *)name :(NSString *)id) {
65+
RCT_EXPORT_METHOD(startExecutionTrace:(NSString *)name :(NSString *)id:(RCTResponseSenderBlock)callBack) {
6666
IBGExecutionTrace *trace = [IBGAPM startExecutionTraceWithName:name];
6767
if (trace != nil) {
6868
[traces setObject: trace forKey: id];
69+
callBack(@[id]);
70+
} else {
71+
callBack(@[[NSNull null]]);
6972
}
7073
}
7174

modules/APM.js

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,12 +77,22 @@ export default {
7777

7878
/**
7979
* Starts a custom trace
80+
* Returns a promise, the promise delivers the trace reference if APM is enabled, otherwise it gets rejected
8081
* @param {string} name
8182
*/
8283
startExecutionTrace(name) {
83-
const id = Date.now() + '';
84-
IBGAPM.startExecutionTrace(name, id);
85-
return new Trace(id, name);
84+
const TRACE_NOT_STARTED_APM_NOT_ENABLED = `Execution trace "${name}" wasn't created. Please make sure to enable APM first by following the instructions at this link: https://docs.instabug.com/reference#enable-or-disable-apm`;
85+
const timestamp = Date.now() + '';
86+
87+
return new Promise((resolve, reject) => {
88+
IBGAPM.startExecutionTrace(name, timestamp, (id) => {
89+
if (id) {
90+
resolve(new Trace(id, name));
91+
} else {
92+
reject(new Error(TRACE_NOT_STARTED_APM_NOT_ENABLED));
93+
}
94+
});
95+
});
8696
},
8797

8898
/**

0 commit comments

Comments
 (0)