From 193a2bd4cdffbbc79b69c067b31420663dc9b03a Mon Sep 17 00:00:00 2001 From: Valentin Shergin Date: Tue, 16 Jan 2018 12:11:23 -0800 Subject: [PATCH] Tweaking -[RCTSurface synchronouslyWaitForStage:] Summary: Waiting for layout is now available on main thread. Reviewed By: mmmulani Differential Revision: D6719836 fbshipit-source-id: ef655095e999df5f77e69c5931459cce1aaeb1f0 --- React/Base/Surface/RCTSurface.h | 9 ++++++--- React/Base/Surface/RCTSurface.mm | 20 ++++++++++---------- 2 files changed, 16 insertions(+), 13 deletions(-) diff --git a/React/Base/Surface/RCTSurface.h b/React/Base/Surface/RCTSurface.h index 603f0e851acf1b..44a53f6569cba5 100644 --- a/React/Base/Surface/RCTSurface.h +++ b/React/Base/Surface/RCTSurface.h @@ -84,7 +84,6 @@ NS_ASSUME_NONNULL_BEGIN */ @property (atomic, assign, readonly) CGSize maximumSize; - /** * Simple shortcut to `-[RCTSurface setMinimumSize:size maximumSize:size]`. */ @@ -109,8 +108,12 @@ NS_ASSUME_NONNULL_BEGIN /** * Synchronously blocks the current thread up to given `timeout` until - * the Surface will not have given `stage`. - * Do nothing, if called from the main or `UIManager` queue. + * the Surface reaches `stage`. + * Limitations: + * - Do nothing, if called on `UIManager` queue. + * - Calling on the main queue with `RCTSurfaceStageSurfaceDidInitialMounting` + * stage temporary is not supported; in this case the stage will be + * downgraded to `RCTSurfaceStageSurfaceDidInitialLayout`. */ - (BOOL)synchronouslyWaitForStage:(RCTSurfaceStage)stage timeout:(NSTimeInterval)timeout; diff --git a/React/Base/Surface/RCTSurface.mm b/React/Base/Surface/RCTSurface.mm index 736400953d535b..5002c9f6dd3b62 100644 --- a/React/Base/Surface/RCTSurface.mm +++ b/React/Base/Surface/RCTSurface.mm @@ -435,6 +435,16 @@ - (CGSize)intrinsicSize - (BOOL)synchronouslyWaitForStage:(RCTSurfaceStage)stage timeout:(NSTimeInterval)timeout { + if (RCTIsMainQueue() && (stage == RCTSurfaceStageSurfaceDidInitialRendering)) { + // This case *temporary* does not supported. + stage = RCTSurfaceStageSurfaceDidInitialLayout; + } + + if (RCTIsUIManagerQueue()) { + RCTLogInfo(@"Synchronous waiting is not supported on UIManager queue."); + return NO; + } + dispatch_semaphore_t semaphore; switch (stage) { case RCTSurfaceStageSurfaceDidInitialLayout: @@ -447,16 +457,6 @@ - (BOOL)synchronouslyWaitForStage:(RCTSurfaceStage)stage timeout:(NSTimeInterval RCTAssert(NO, @"Only waiting for `RCTSurfaceStageSurfaceDidInitialRendering` and `RCTSurfaceStageSurfaceDidInitialLayout` stages is supported."); } - if (RCTIsMainQueue()) { - RCTLogInfo(@"Synchronous waiting is not supported on the main queue."); - return NO; - } - - if (RCTIsUIManagerQueue()) { - RCTLogInfo(@"Synchronous waiting is not supported on UIManager queue."); - return NO; - } - BOOL timeoutOccured = dispatch_semaphore_wait(semaphore, dispatch_time(DISPATCH_TIME_NOW, timeout * NSEC_PER_SEC)); if (!timeoutOccured) { // Balancing the semaphore.