3333static NSString *const RNCallKeepDidToggleHoldAction = @" RNCallKeepDidToggleHoldAction" ;
3434static NSString *const RNCallKeepProviderReset = @" RNCallKeepProviderReset" ;
3535static 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
4347static 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