Skip to content

Commit

Permalink
Tweaking -[RCTSurface synchronouslyWaitForStage:]
Browse files Browse the repository at this point in the history
Summary: Waiting for layout is now available on main thread.

Reviewed By: mmmulani

Differential Revision: D6719836

fbshipit-source-id: ef655095e999df5f77e69c5931459cce1aaeb1f0
  • Loading branch information
shergin authored and facebook-github-bot committed Jan 16, 2018
1 parent d0f7d4d commit 193a2bd
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 13 deletions.
9 changes: 6 additions & 3 deletions React/Base/Surface/RCTSurface.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,6 @@ NS_ASSUME_NONNULL_BEGIN
*/
@property (atomic, assign, readonly) CGSize maximumSize;


/**
* Simple shortcut to `-[RCTSurface setMinimumSize:size maximumSize:size]`.
*/
Expand All @@ -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;

Expand Down
20 changes: 10 additions & 10 deletions React/Base/Surface/RCTSurface.mm
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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.
Expand Down

0 comments on commit 193a2bd

Please sign in to comment.