Skip to content

Commit

Permalink
applying the fix for not handling promises on setup() from this segme…
Browse files Browse the repository at this point in the history
…ntio@e74ab69 referenced in this issue: segmentio#16 (comment)
  • Loading branch information
Tony Lara committed Mar 18, 2019
1 parent 8f13f6f commit dda5b6d
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import com.facebook.react.bridge.ReactApplicationContext
import com.facebook.react.bridge.ReactContextBaseJavaModule
import com.facebook.react.bridge.ReactMethod
import com.facebook.react.bridge.ReadableMap
import com.facebook.react.bridge.Promise
import com.segment.analytics.Analytics
import com.segment.analytics.Properties
import com.segment.analytics.Traits
Expand All @@ -41,7 +42,7 @@ class RNAnalyticsModule(context: ReactApplicationContext): ReactContextBaseJavaM
override fun getName() = "RNAnalytics"

@ReactMethod
fun setup(options: ReadableMap) {
fun setup(options: ReadableMap, promise: Promise) {
val builder = Analytics
.Builder(reactApplicationContext, options.getString("writeKey"))
.flushQueueSize(options.getInt("flushAt"))
Expand Down Expand Up @@ -69,9 +70,14 @@ class RNAnalyticsModule(context: ReactApplicationContext): ReactContextBaseJavaM
builder.logLevel(Analytics.LogLevel.VERBOSE)
}

Analytics.setSingletonInstance(
RNAnalytics.buildWithIntegrations(builder)
)
try {
Analytics.setSingletonInstance(
RNAnalytics.buildWithIntegrations(builder)
)
promise.resolve(true)
} catch(e: Exception) {
promise.reject("E_SEGMENT_ERROR", e)
}
}

@ReactMethod
Expand Down
14 changes: 12 additions & 2 deletions packages/core/ios/RNAnalytics/RNAnalytics.m
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@ +(void)initialize {

@synthesize bridge = _bridge;

RCT_EXPORT_METHOD(setup:(NSDictionary*)options) {
RCT_EXPORT_METHOD(setup:(NSDictionary*)options
setupResolver:(RCTPromiseResolveBlock)resolve
setupRejecter:(RCTPromiseRejectBlock)reject)
{
SEGAnalyticsConfiguration* config = [SEGAnalyticsConfiguration configurationWithWriteKey:options[@"writeKey"]];

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

[SEGAnalytics debug:[options[@"debug"] boolValue]];
[SEGAnalytics setupWithConfiguration:config];

@try {
[SEGAnalytics setupWithConfiguration:config];
}
@catch (NSException *exception) {
reject(exception);
}

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

#define withContext(context) @{@"context": context}
Expand Down

0 comments on commit dda5b6d

Please sign in to comment.