Skip to content

Commit ff62ad1

Browse files
RSNarafacebook-github-bot
authored andcommitted
Refactor RCTUnsafeExecute{On,Once}MainQueueSync utils (#51428)
Summary: Pull Request resolved: #51428 Just refactoring the control flow in these functions (in a separate diff). So, that the logic in subsequent diffs is easier to read: D74769326. Changelog: [Internal] Reviewed By: yungsters Differential Revision: D74940681 fbshipit-source-id: 7aabc722948666a13993a1feff7eeca8ef1403cf
1 parent bca7c5a commit ff62ad1

1 file changed

Lines changed: 26 additions & 28 deletions

File tree

packages/react-native/React/Base/RCTUtils.mm

Lines changed: 26 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -302,16 +302,7 @@ void RCTExecuteOnMainQueue(dispatch_block_t block)
302302
// unless you know what you are doing.
303303
void RCTUnsafeExecuteOnMainQueueSync(dispatch_block_t block)
304304
{
305-
if (RCTIsMainQueue()) {
306-
block();
307-
} else {
308-
if (facebook::react::ReactNativeFeatureFlags::disableMainQueueSyncDispatchIOS()) {
309-
RCTLogError(@"RCTUnsafeExecuteOnMainQueueSync: Sync dispatches to the main queue can deadlock React Native.");
310-
}
311-
dispatch_sync(dispatch_get_main_queue(), ^{
312-
block();
313-
});
314-
}
305+
RCTUnsafeExecuteOnMainQueueSyncWithError(block, @"Sync dispatches to the main queue can deadlock React Native.");
315306
}
316307

317308
// Please do not use this method
@@ -320,34 +311,41 @@ void RCTUnsafeExecuteOnMainQueueSyncWithError(dispatch_block_t block, NSString *
320311
{
321312
if (RCTIsMainQueue()) {
322313
block();
323-
} else {
324-
if (facebook::react::ReactNativeFeatureFlags::disableMainQueueSyncDispatchIOS()) {
325-
RCTLogError(@"RCTUnsafeExecuteOnMainQueueSync: %@", context);
326-
}
327-
dispatch_sync(dispatch_get_main_queue(), ^{
328-
block();
329-
});
314+
return;
330315
}
316+
317+
if (facebook::react::ReactNativeFeatureFlags::disableMainQueueSyncDispatchIOS()) {
318+
RCTLogError(@"RCTUnsafeExecuteOnMainQueueSync: %@", context);
319+
}
320+
321+
dispatch_sync(dispatch_get_main_queue(), ^{
322+
block();
323+
});
331324
}
332325

333326
static void RCTUnsafeExecuteOnMainQueueOnceSync(dispatch_once_t *onceToken, dispatch_block_t block)
334327
{
335328
// The solution was borrowed from a post by Sophie Alpert:
336329
// https://sophiebits.com/2014/04/02/dispatch-once-initialization-on-the-main-thread
337330
// See also: https://www.mikeash.com/pyblog/friday-qa-2014-06-06-secrets-of-dispatch_once.html
338-
if (RCTIsMainQueue()) {
331+
auto executeOnce = ^{
339332
dispatch_once(onceToken, block);
340-
} else {
341-
if (DISPATCH_EXPECT(*onceToken == 0L, NO)) {
342-
if (facebook::react::ReactNativeFeatureFlags::disableMainQueueSyncDispatchIOS()) {
343-
RCTLogError(
344-
@"RCTUnsafeExecuteOnMainQueueOnceSync: Sync dispatches to the main queue can deadlock React Native.");
345-
}
346-
dispatch_sync(dispatch_get_main_queue(), ^{
347-
dispatch_once(onceToken, block);
348-
});
349-
}
333+
};
334+
335+
if (RCTIsMainQueue()) {
336+
executeOnce();
337+
return;
338+
}
339+
340+
if (!DISPATCH_EXPECT(*onceToken == 0L, NO)) {
341+
return;
350342
}
343+
344+
if (facebook::react::ReactNativeFeatureFlags::disableMainQueueSyncDispatchIOS()) {
345+
RCTLogError(@"RCTUnsafeExecuteOnMainQueueOnceSync: Sync dispatches to the main queue can deadlock React Native.");
346+
}
347+
348+
dispatch_sync(dispatch_get_main_queue(), executeOnce);
351349
}
352350

353351
CGFloat RCTScreenScale(void)

0 commit comments

Comments
 (0)