Skip to content

Commit e74ab69

Browse files
committed
Add support for setup to resolve its promises
With this change the setup method will now resolve its promises and thereby be able to catch errors during setup.
1 parent 5615f89 commit e74ab69

File tree

2 files changed

+22
-6
lines changed

2 files changed

+22
-6
lines changed

packages/core/android/src/main/java/com/segment/analytics/reactnative/core/RNAnalyticsModule.kt

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import com.facebook.react.bridge.ReactApplicationContext
2828
import com.facebook.react.bridge.ReactContextBaseJavaModule
2929
import com.facebook.react.bridge.ReactMethod
3030
import com.facebook.react.bridge.ReadableMap
31+
import com.facebook.react.bridge.Promise
3132
import com.segment.analytics.Analytics
3233
import com.segment.analytics.Properties
3334
import com.segment.analytics.Traits
@@ -41,7 +42,7 @@ class RNAnalyticsModule(context: ReactApplicationContext): ReactContextBaseJavaM
4142
override fun getName() = "RNAnalytics"
4243

4344
@ReactMethod
44-
fun setup(options: ReadableMap) {
45+
fun setup(options: ReadableMap, promise: Promise) {
4546
val builder = Analytics
4647
.Builder(reactApplicationContext, options.getString("writeKey"))
4748
.flushQueueSize(options.getInt("flushAt"))
@@ -69,9 +70,14 @@ class RNAnalyticsModule(context: ReactApplicationContext): ReactContextBaseJavaM
6970
builder.logLevel(Analytics.LogLevel.VERBOSE)
7071
}
7172

72-
Analytics.setSingletonInstance(
73-
RNAnalytics.buildWithIntegrations(builder)
74-
)
73+
try {
74+
Analytics.setSingletonInstance(
75+
RNAnalytics.buildWithIntegrations(builder)
76+
)
77+
promise.resolve(true)
78+
} catch(e: Exception) {
79+
promise.reject("E_SEGMENT_ERROR", e)
80+
}
7581
}
7682

7783
@ReactMethod

packages/core/ios/RNAnalytics/RNAnalytics.m

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,10 @@ +(void)initialize {
3232

3333
@synthesize bridge = _bridge;
3434

35-
RCT_EXPORT_METHOD(setup:(NSDictionary*)options) {
35+
RCT_EXPORT_METHOD(setup:(NSDictionary*)options
36+
setupResolver:(RCTPromiseResolveBlock)resolve
37+
setupRejecter:(RCTPromiseRejectBlock)reject)
38+
{
3639
SEGAnalyticsConfiguration* config = [SEGAnalyticsConfiguration configurationWithWriteKey:options[@"writeKey"]];
3740

3841
config.recordScreenViews = [options[@"recordScreenViews"] boolValue];
@@ -46,7 +49,13 @@ +(void)initialize {
4649
}
4750

4851
[SEGAnalytics debug:[options[@"debug"] boolValue]];
49-
[SEGAnalytics setupWithConfiguration:config];
52+
53+
@try {
54+
[SEGAnalytics setupWithConfiguration:config];
55+
}
56+
@catch (NSException *exception) {
57+
reject(exception);
58+
}
5059

5160
// On iOS we use method swizzling to intercept lifecycle events
5261
// However, React-Native calls our library after applicationDidFinishLaunchingWithOptions: is called
@@ -60,6 +69,7 @@ +(void)initialize {
6069
withObject:_bridge.launchOptions];
6170
}
6271
}
72+
resolve(YES);
6373
}
6474

6575
#define withContext(context) @{@"context": context}

0 commit comments

Comments
 (0)