Skip to content

Extend NativeBridge to support more events, use singleton for dispatching#1

Open
harunsmrkovic wants to merge 1 commit into
digime:masterfrom
harunsmrkovic:patch-1
Open

Extend NativeBridge to support more events, use singleton for dispatching#1
harunsmrkovic wants to merge 1 commit into
digime:masterfrom
harunsmrkovic:patch-1

Conversation

@harunsmrkovic

Copy link
Copy Markdown

We were experiencing intermittent issues with the original code, sometimes receiving the error:

Bridge is not set. This is probably because you've explicitly synthesized the bridge in NativeBridge, even though it's inherited from RCTEventEmitter.

As proposed in this issue react/react-native#15421 (comment), solution is to use a singleton of NativeBridge, and then dispatch events via it.

…hing

We were experiencing intermittent issues with the original code, sometimes receiving the error:

Bridge is not set. This is probably because you've explicitly synthesized the bridge in NativeBridge, even though it's inherited from RCTEventEmitter.

As proposed in this issue react/react-native#15421 (comment), solution is to use a singleton of NativeBridge, and then dispatch events via it.

@JacobKingDev JacobKingDev left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From the perspective of an Objective-C developer, the code looks mostly fine with only a couple of comments (mostly referring to the same issue).


NSLog(@"digime: Got accounts %@", body);

[[NativeBridge allocWithZone: nil] emitEventWithName:@"accountsRetrieved" body: body];

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You've instantiated a singleton instance of NativeBridge above, but you're creating a new one each time here? Also, it's worth noting that the instance created here will be destroyed immediately after receiving the method invocation, which probably isn't a problem in this use case but could present an issue for long-lived processes.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was honestly hoping that this code is doing the exact opposite of what you described 🙃

Can you write the proper code, and I can replace it?

[[NativeBridge allocWithZone: nil] emitEventWithName:@"accountsRetrieved" body: body];
} else {
NSLog(@"digime: Getting accounts failed %@", error);
[[NativeBridge allocWithZone: nil] emitEventWithName:@"accountsRetrieveFailed" body: error.localizedDescription];

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As above.

[self sendEventWithName:@"userAuthAccept" body:@""];
[[DMEClient sharedClient] getFileList];
NSLog(@"digime: Auth Success");
[[NativeBridge allocWithZone: nil] emitEventWithName:@"userAuthAccept" body:@""];

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As above.

NSLog(@"Auth Deny");
[self sendEventWithName:@"userAuthReject" body:@""];
NSLog(@"digime: Auth Deny");
[[NativeBridge allocWithZone: nil] emitEventWithName:@"userAuthDenied" body:@""];

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As above.

NSLog(@"Auth Failed");
[self sendEventWithName:@"userAuthReject" body:@""];
NSLog(@"digime: Auth Failed");
[[NativeBridge allocWithZone: nil] emitEventWithName:@"userAuthFailed" body:@""];

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As above.

NSData * jsonData = [NSJSONSerialization dataWithJSONObject:files.fileIds options:0 error:&err];
NSString * body = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];

[[NativeBridge allocWithZone: nil] emitEventWithName:@"fileListRetrieved" body: body];

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As above.

- (void)clientFailedToRetrieveFileList:(NSError *)error {
NSLog(@"Client failed to get file list");
NSLog(@"digime: Client failed to get file list");
[[NativeBridge allocWithZone: nil] emitEventWithName:@"fileListFailed" body:@""];

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As above.


[self sendEventWithName:@"fileData" body: stringData];
NSLog(@"digime: Sending file data Event for %@", file.fileId);
[[NativeBridge allocWithZone: nil] emitEventWithName:@"fileRetrieved" body: stringData];

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As above.

- (void)fileRetrieveFailed:(NSString *)fileId error:(NSError *)error {
NSLog(@"file retrived failed");
NSLog(@"digime: Failed retrieving file %@, %@", fileId, error);
[[NativeBridge allocWithZone: nil] emitEventWithName:@"fileRetrieveFailed" body: fileId];

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As above.

[[DMEClient sharedClient] getAccountsWithCompletion:^(CAAccounts * _Nullable accounts, NSError * _Nullable error) {

if (!error) {
NSError * err;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please bump asterisk over to be next to name, IE NSError *err. We do have internal Objective-C semantics guidelines but I realise you can't see these!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants