Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(DTXReactNativeSupport): JS and Content can load in any order #74

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 33 additions & 12 deletions DetoxSync/DetoxSync/ReactNativeSupport/DTXReactNativeSupport.m
Original file line number Diff line number Diff line change
Expand Up @@ -212,23 +212,44 @@ + (void)waitForReactNativeLoadWithCompletionHandler:(void (^)(void))handler
{
NSParameterAssert(handler != nil);

__block __weak id observer;
__block __weak id observer2;

observer = [[NSNotificationCenter defaultCenter] addObserverForName:@"RCTJavaScriptDidLoadNotification" object:nil queue:nil usingBlock:^(NSNotification * _Nonnull note) {
[[NSNotificationCenter defaultCenter] removeObserver:observer];
[[NSNotificationCenter defaultCenter] removeObserver:observer2];
__block __weak id jsObserver;
__block __weak id contentObserver;
__block __weak id failObserver;

// JavascriptDidLoad and ContentDidAppear can happen in any order
// When we receive a notification (either of them), we set this to 1 (atomically)
// If it was already at 1, then we received both, and so we can call the handler
static _Thread_local _Atomic int successfulNotificationsReceived;
atomic_store(&successfulNotificationsReceived, 0);

jsObserver = [[NSNotificationCenter defaultCenter] addObserverForName:@"RCTJavaScriptDidLoadNotification" object:nil queue:nil usingBlock:^(NSNotification * _Nonnull note) {
[[NSNotificationCenter defaultCenter] removeObserver:jsObserver];

// If the flag was already at 1 then we just received the 2nd, so we call the handler
int expected = 0;
if (!atomic_compare_exchange_strong(&successfulNotificationsReceived, &expected, 1))
{
[[NSNotificationCenter defaultCenter] removeObserver:failObserver];
handler();
}
}];

observer = [[NSNotificationCenter defaultCenter] addObserverForName:@"RCTContentDidAppearNotification" object:nil queue:nil usingBlock:^(NSNotification * _Nonnull note) {
[[NSNotificationCenter defaultCenter] removeObserver:observer];
contentObserver = [[NSNotificationCenter defaultCenter] addObserverForName:@"RCTContentDidAppearNotification" object:nil queue:nil usingBlock:^(NSNotification * _Nonnull note) {
[[NSNotificationCenter defaultCenter] removeObserver:contentObserver];

// If the flag was already at 1 then we just received the 2nd, so we call the handler
int expected = 0;
if (!atomic_compare_exchange_strong(&successfulNotificationsReceived, &expected, 1))
{
[[NSNotificationCenter defaultCenter] removeObserver:failObserver];
handler();
}];
}
}];

observer2 = [[NSNotificationCenter defaultCenter] addObserverForName:@"RCTJavaScriptDidFailToLoadNotification" object:nil queue:nil usingBlock:^(NSNotification * _Nonnull note) {
[[NSNotificationCenter defaultCenter] removeObserver:observer];
[[NSNotificationCenter defaultCenter] removeObserver:observer2];
failObserver = [[NSNotificationCenter defaultCenter] addObserverForName:@"RCTJavaScriptDidFailToLoadNotification" object:nil queue:nil usingBlock:^(NSNotification * _Nonnull note) {
[[NSNotificationCenter defaultCenter] removeObserver:jsObserver];
[[NSNotificationCenter defaultCenter] removeObserver:contentObserver];
[[NSNotificationCenter defaultCenter] removeObserver:failObserver];

handler();
}];
Expand Down