Skip to content

Add support for setup to resolve its promises #15

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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