Skip to content

Commit d04fecf

Browse files
Konrad Kierusmanuquentin
authored andcommitted
Proof of concept to handle issue with JS not initialized on start
1 parent e95288a commit d04fecf

File tree

2 files changed

+48
-12
lines changed

2 files changed

+48
-12
lines changed

actions.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ const RNCallKeepDidToggleHoldAction = 'RNCallKeepDidToggleHoldAction';
1414
const RNCallKeepDidPerformDTMFAction = 'RNCallKeepDidPerformDTMFAction';
1515
const RNCallKeepProviderReset = 'RNCallKeepProviderReset';
1616
const RNCallKeepCheckReachability = 'RNCallKeepCheckReachability';
17+
const RNCallKeepDidLoadWithEvents = 'RNCallKeepDidLoadWithEvents';
1718
const isIOS = Platform.OS === 'ios';
1819

1920
const didReceiveStartCallAction = handler => {
@@ -55,6 +56,9 @@ const didResetProvider = handler =>
5556
const checkReachability = handler =>
5657
eventEmitter.addListener(RNCallKeepCheckReachability, handler);
5758

59+
const didLoadWithEvents = handler =>
60+
eventEmitter.addListener(RNCallKeepDidLoadWithEvents, handler);
61+
5862
export const listeners = {
5963
didReceiveStartCallAction,
6064
answerCall,
@@ -67,4 +71,5 @@ export const listeners = {
6771
didPerformDTMFAction,
6872
didResetProvider,
6973
checkReachability,
74+
didLoadWithEvents
7075
};

ios/RNCallKeep/RNCallKeep.m

Lines changed: 43 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,15 @@
3333
static NSString *const RNCallKeepDidToggleHoldAction = @"RNCallKeepDidToggleHoldAction";
3434
static NSString *const RNCallKeepProviderReset = @"RNCallKeepProviderReset";
3535
static NSString *const RNCallKeepCheckReachability = @"RNCallKeepCheckReachability";
36+
static NSString *const RNCallKeepDidLoadWithEvents = @"RNCallKeepDidLoadWithEvents";
3637

3738
@implementation RNCallKeep
3839
{
3940
NSOperatingSystemVersion _version;
4041
BOOL _isStartCallActionEventListenerAdded;
42+
bool _hasListeners;
43+
NSMutableArray *_delayedEvents;
44+
4145
}
4246

4347
static CXProvider* sharedProvider;
@@ -52,6 +56,7 @@ - (instancetype)init
5256
#endif
5357
if (self = [super init]) {
5458
_isStartCallActionEventListenerAdded = NO;
59+
_delayedEvents = [NSMutableArray array];
5560
}
5661
return self;
5762
}
@@ -92,10 +97,36 @@ - (void)dealloc
9297
RNCallKeepPerformPlayDTMFCallAction,
9398
RNCallKeepDidToggleHoldAction,
9499
RNCallKeepProviderReset,
95-
RNCallKeepCheckReachability
100+
RNCallKeepCheckReachability,
101+
RNCallKeepDidLoadWithEvents
96102
];
97103
}
98104

105+
- (void)startObserving
106+
{
107+
_hasListeners = YES;
108+
if ([_delayedEvents count] > 0) {
109+
[self sendEventWithName:RNCallKeepDidLoadWithEvents body:_delayedEvents];
110+
}
111+
}
112+
113+
- (void)stopObserving
114+
{
115+
_hasListeners = FALSE;
116+
}
117+
118+
- (void)sendEventWithNameWrapper:(NSString *)name body:(id)body {
119+
if (_hasListeners) {
120+
[self sendEventWithName:name body:body];
121+
} else {
122+
NSDictionary *dictionary = @{
123+
@"name": name,
124+
@"data": body
125+
};
126+
[_delayedEvents addObject:dictionary];
127+
}
128+
}
129+
99130
+ (void)initCallKitProvider {
100131
if (sharedProvider == nil) {
101132
NSDictionary *settings = [[NSUserDefaults standardUserDefaults] dictionaryForKey:@"RNCallKeepSettings"];
@@ -392,7 +423,7 @@ + (void)reportNewIncomingCall:(NSString *)uuidString
392423
[RNCallKeep initCallKitProvider];
393424
[sharedProvider reportNewIncomingCallWithUUID:uuid update:callUpdate completion:^(NSError * _Nullable error) {
394425
RNCallKeep *callKeep = [RNCallKeep allocWithZone: nil];
395-
[callKeep sendEventWithName:RNCallKeepDidDisplayIncomingCall body:@{
426+
[callKeep sendEventWithNameWrapper:RNCallKeepDidDisplayIncomingCall body:@{
396427
@"error": error && error.localizedDescription ? error.localizedDescription : @"",
397428
@"callUUID": uuidString,
398429
@"handle": handle,
@@ -613,7 +644,7 @@ - (void)handleStartCallNotification:(NSDictionary *)userInfo
613644
}
614645
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
615646
dispatch_after(popTime, dispatch_get_main_queue(), ^{
616-
[self sendEventWithName:RNCallKeepDidReceiveStartCallAction body:userInfo];
647+
[self sendEventWithNameWrapper:RNCallKeepDidReceiveStartCallAction body:userInfo];
617648
});
618649
}
619650

@@ -625,7 +656,7 @@ - (void)providerDidReset:(CXProvider *)provider{
625656
#endif
626657
//this means something big changed, so tell the JS. The JS should
627658
//probably respond by hanging up all calls.
628-
[self sendEventWithName:RNCallKeepProviderReset body:nil];
659+
[self sendEventWithNameWrapper:RNCallKeepProviderReset body:nil];
629660
}
630661

631662
// Starting outgoing call
@@ -637,7 +668,7 @@ - (void)provider:(CXProvider *)provider performStartCallAction:(CXStartCallActio
637668
//do this first, audio sessions are flakey
638669
[self configureAudioSession];
639670
//tell the JS to actually make the call
640-
[self sendEventWithName:RNCallKeepDidReceiveStartCallAction body:@{ @"callUUID": [action.callUUID.UUIDString lowercaseString], @"handle": action.handle.value }];
671+
[self sendEventWithNameWrapper:RNCallKeepDidReceiveStartCallAction body:@{ @"callUUID": [action.callUUID.UUIDString lowercaseString], @"handle": action.handle.value }];
641672
[action fulfill];
642673
}
643674

@@ -662,7 +693,7 @@ - (void)provider:(CXProvider *)provider performAnswerCallAction:(CXAnswerCallAct
662693
NSLog(@"[RNCallKeep][CXProviderDelegate][provider:performAnswerCallAction]");
663694
#endif
664695
[self configureAudioSession];
665-
[self sendEventWithName:RNCallKeepPerformAnswerCallAction body:@{ @"callUUID": [action.callUUID.UUIDString lowercaseString] }];
696+
[self sendEventWithNameWrapper:RNCallKeepPerformAnswerCallAction body:@{ @"callUUID": [action.callUUID.UUIDString lowercaseString] }];
666697
[action fulfill];
667698
}
668699

@@ -672,7 +703,7 @@ - (void)provider:(CXProvider *)provider performEndCallAction:(CXEndCallAction *)
672703
#ifdef DEBUG
673704
NSLog(@"[RNCallKeep][CXProviderDelegate][provider:performEndCallAction]");
674705
#endif
675-
[self sendEventWithName:RNCallKeepPerformEndCallAction body:@{ @"callUUID": [action.callUUID.UUIDString lowercaseString] }];
706+
[self sendEventWithNameWrapper:RNCallKeepPerformEndCallAction body:@{ @"callUUID": [action.callUUID.UUIDString lowercaseString] }];
676707
[action fulfill];
677708
}
678709

@@ -682,15 +713,15 @@ -(void)provider:(CXProvider *)provider performSetHeldCallAction:(CXSetHeldCallAc
682713
NSLog(@"[RNCallKeep][CXProviderDelegate][provider:performSetHeldCallAction]");
683714
#endif
684715

685-
[self sendEventWithName:RNCallKeepDidToggleHoldAction body:@{ @"hold": @(action.onHold), @"callUUID": [action.callUUID.UUIDString lowercaseString] }];
716+
[self sendEventWithNameWrapper:RNCallKeepDidToggleHoldAction body:@{ @"hold": @(action.onHold), @"callUUID": [action.callUUID.UUIDString lowercaseString] }];
686717
[action fulfill];
687718
}
688719

689720
- (void)provider:(CXProvider *)provider performPlayDTMFCallAction:(CXPlayDTMFCallAction *)action {
690721
#ifdef DEBUG
691722
NSLog(@"[RNCallKeep][CXProviderDelegate][provider:performPlayDTMFCallAction]");
692723
#endif
693-
[self sendEventWithName:RNCallKeepPerformPlayDTMFCallAction body:@{ @"digits": action.digits, @"callUUID": [action.callUUID.UUIDString lowercaseString] }];
724+
[self sendEventWithNameWrapper:RNCallKeepPerformPlayDTMFCallAction body:@{ @"digits": action.digits, @"callUUID": [action.callUUID.UUIDString lowercaseString] }];
694725
[action fulfill];
695726
}
696727

@@ -700,7 +731,7 @@ -(void)provider:(CXProvider *)provider performSetMutedCallAction:(CXSetMutedCall
700731
NSLog(@"[RNCallKeep][CXProviderDelegate][provider:performSetMutedCallAction]");
701732
#endif
702733

703-
[self sendEventWithName:RNCallKeepDidPerformSetMutedCallAction body:@{ @"muted": @(action.muted), @"callUUID": [action.callUUID.UUIDString lowercaseString] }];
734+
[self sendEventWithNameWrapper:RNCallKeepDidPerformSetMutedCallAction body:@{ @"muted": @(action.muted), @"callUUID": [action.callUUID.UUIDString lowercaseString] }];
704735
[action fulfill];
705736
}
706737

@@ -724,15 +755,15 @@ - (void)provider:(CXProvider *)provider didActivateAudioSession:(AVAudioSession
724755
[[NSNotificationCenter defaultCenter] postNotificationName:AVAudioSessionInterruptionNotification object:nil userInfo:userInfo];
725756

726757
[self configureAudioSession];
727-
[self sendEventWithName:RNCallKeepDidActivateAudioSession body:nil];
758+
[self sendEventWithNameWrapper:RNCallKeepDidActivateAudioSession body:nil];
728759
}
729760

730761
- (void)provider:(CXProvider *)provider didDeactivateAudioSession:(AVAudioSession *)audioSession
731762
{
732763
#ifdef DEBUG
733764
NSLog(@"[RNCallKeep][CXProviderDelegate][provider:didDeactivateAudioSession]");
734765
#endif
735-
[self sendEventWithName:RNCallKeepDidDeactivateAudioSession body:nil];
766+
[self sendEventWithNameWrapper:RNCallKeepDidDeactivateAudioSession body:nil];
736767
}
737768

738769
@end

0 commit comments

Comments
 (0)