[invites] Add jsInitialised method to improve getInitialInvitation

This commit is contained in:
Chris Bianca 2018-03-30 10:07:23 +01:00
parent e7be3cc7f8
commit 06c06b764f
3 changed files with 37 additions and 7 deletions

View File

@ -13,8 +13,16 @@
@implementation RNFirebaseInvites
static RNFirebaseInvites *theRNFirebaseInvites = nil;
static NSString *initialInvite = nil;
static bool jsReady = FALSE;
+ (nonnull instancetype)instance {
// If an event comes in before the bridge has initialised the native module
// then we create a temporary instance which handles events until the bridge
// and JS side are ready
if (theRNFirebaseInvites == nil) {
theRNFirebaseInvites = [[RNFirebaseInvites alloc] init];
}
return theRNFirebaseInvites;
}
@ -101,11 +109,11 @@ RCT_EXPORT_METHOD(getInitialInvitation:(RCTPromiseResolveBlock)resolve rejecter:
@"invitationId": receivedInvite.inviteId,
});
} else {
resolve(nil);
resolve(initialInvite);
}
}];
} else {
resolve(nil);
resolve(initialInvite);
}
}
@ -151,22 +159,38 @@ RCT_EXPORT_METHOD(sendInvitation:(NSDictionary *) invitation
});
}
RCT_EXPORT_METHOD(jsInitialised:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject) {
jsReady = TRUE;
resolve(nil);
}
// ** Start internals **
- (BOOL)handleUrl:(NSURL *)url {
return [FIRInvites handleUniversalLink:url completion:^(FIRReceivedInvite * _Nullable receivedInvite, NSError * _Nullable error) {
if (error) {
NSLog(@"Failed to handle invitation: %@", [error localizedDescription]);
} else if (receivedInvite && receivedInvite.inviteId) {
[RNFirebaseUtil sendJSEvent:self name:INVITES_INVITATION_RECEIVED body:@{
@"deepLink": receivedInvite.deepLink,
@"invitationId": receivedInvite.inviteId,
}];
[self sendJSEvent:self name:INVITES_INVITATION_RECEIVED body:@{
@"deepLink": receivedInvite.deepLink,
@"invitationId": receivedInvite.inviteId,
}];
} else {
[[RNFirebaseLinks instance] sendLink:receivedInvite.deepLink];
}
}];
}
// Because of the time delay between the app starting and the bridge being initialised
// we catch any events that are received before the JS is ready to receive them
- (void)sendJSEvent:(RCTEventEmitter *)emitter name:(NSString *)name body:(id)body {
if (emitter.bridge && jsReady) {
[RNFirebaseUtil sendJSEvent:emitter name:name body:body];
} else if (!initialInvite) {
initialInvite = body;
} else {
NSLog(@"Multiple invite events received before the JS invites module has been initialised");
}
}
- (NSArray<NSString *> *)supportedEvents {
return @[INVITES_INVITATION_RECEIVED];

View File

@ -171,7 +171,7 @@ RCT_EXPORT_METHOD(jsInitialised:(RCTPromiseResolveBlock)resolve rejecter:(RCTPro
} else if (!initialLink) {
initialLink = body;
} else {
NSLog(@"Multiple link events received before the JS links modules has been initialised");
NSLog(@"Multiple link events received before the JS links module has been initialised");
}
}

View File

@ -2,6 +2,7 @@
* @flow
* Invites representation wrapper
*/
import { Platform } from 'react-native';
import { SharedEventEmitter } from '../../utils/events';
import { getLogger } from '../../utils/log';
import ModuleBase from '../../utils/ModuleBase';
@ -37,6 +38,11 @@ export default class Invites extends ModuleBase {
SharedEventEmitter.emit('onInvitation', invitation);
}
);
// Tell the native module that we're ready to receive events
if (Platform.OS === 'ios') {
getNativeModule(this).jsInitialised();
}
}
/**