@@ -403,6 +403,12 @@ - (void)setCachedSettings:(NSDictionary *)settings
403403
404404- (void )updateIntegrationsWithSettings : (NSDictionary *)projectSettings
405405{
406+ // see if we have a new segment API host and set it.
407+ NSString *apiHost = projectSettings[@" Segment.io" ][@" apiHost" ];
408+ if (apiHost) {
409+ [SEGUtils saveAPIHost: apiHost];
410+ }
411+
406412 seg_dispatch_specific_sync (_serialQueue, ^{
407413 if (self.initialized ) {
408414 return ;
@@ -443,8 +449,29 @@ - (void)configureEdgeFunctions:(NSDictionary *)settings
443449 }
444450}
445451
452+ - (NSDictionary *)defaultSettings
453+ {
454+ return @{
455+ @" integrations" : @{
456+ @" Segment.io" : @{
457+ @" apiKey" : self.configuration .writeKey ,
458+ @" apiHost" : [SEGUtils getAPIHost ]
459+ },
460+ },
461+ @" plan" : @{@" track" : @{}}
462+ };
463+ }
464+
446465- (void )refreshSettings
447466{
467+ // look at our cache immediately, lets try to get things running
468+ // with the last values while we wait to see about any updates.
469+ NSDictionary *previouslyCachedSettings = [self cachedSettings ];
470+ if (previouslyCachedSettings && [previouslyCachedSettings count ] > 0 ) {
471+ [self setCachedSettings: previouslyCachedSettings];
472+ [self configureEdgeFunctions: previouslyCachedSettings];
473+ }
474+
448475 seg_dispatch_specific_async (_serialQueue, ^{
449476 if (self.settingsRequest ) {
450477 return ;
@@ -459,23 +486,24 @@ - (void)refreshSettings
459486 NSDictionary *previouslyCachedSettings = [self cachedSettings ];
460487 if (previouslyCachedSettings && [previouslyCachedSettings count ] > 0 ) {
461488 [self setCachedSettings: previouslyCachedSettings];
462- [self configureEdgeFunctions: settings ];
489+ [self configureEdgeFunctions: previouslyCachedSettings ];
463490 } else if (self.configuration .defaultSettings != nil ) {
464491 // If settings request fail, load a user-supplied version if present.
465492 // but make sure segment.io is in the integrations
466493 NSMutableDictionary *newSettings = [self .configuration.defaultSettings serializableMutableDeepCopy ];
467- newSettings[@" integrations" ][@" Segment.io" ][@" apiKey" ] = self.configuration .writeKey ;
494+ NSMutableDictionary *integrations = newSettings[@" integrations" ];
495+ if (integrations != nil ) {
496+ integrations[@" Segment.io" ] = @{@" apiKey" : self.configuration .writeKey , @" apiHost" : [SEGUtils getAPIHost ]};
497+ } else {
498+ newSettings[@" integrations" ] = @{@" integrations" : @{@" apiKey" : self.configuration .writeKey , @" apiHost" : [SEGUtils getAPIHost ]}};
499+ }
500+
468501 [self setCachedSettings: newSettings];
469502 // don't configure edge functions here. it'll do the right thing on it's own.
470503 } else {
471504 // If settings request fail, fall back to using just Segment integration.
472505 // Doesn't address situations where this callback never gets called (though we don't expect that to ever happen).
473- [self setCachedSettings: @{
474- @" integrations" : @{
475- @" Segment.io" : @{@" apiKey" : self.configuration .writeKey },
476- },
477- @" plan" : @{@" track" : @{}}
478- }];
506+ [self setCachedSettings: [self defaultSettings ]];
479507 // don't configure edge functions here. it'll do the right thing on it's own.
480508 }
481509 }
0 commit comments