Skip to content

Commit

Permalink
Add copy for surface registry when return enumerator (#24056)
Browse files Browse the repository at this point in the history
Summary:
To ensure all methods in surface registry thread safe, add copy to enumerator method.
cc. shergin .

[iOS] [Fixed] - Add copy for surface registry when return enumerator
Pull Request resolved: #24056

Differential Revision: D14575446

Pulled By: shergin

fbshipit-source-id: 6757f71e251381c4a38d13df4729e9494b3164d1
  • Loading branch information
zhongwuzw authored and facebook-github-bot committed Mar 22, 2019
1 parent ef51219 commit 27e7279
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 12 deletions.
4 changes: 2 additions & 2 deletions React/Fabric/Mounting/RCTMountingManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ NS_ASSUME_NONNULL_BEGIN
@property (nonatomic, strong) RCTComponentViewRegistry *componentViewRegistry;

/**
* Transfroms mutation insturctions to mount items and execute them.
* The order of mutation tnstructions matters.
* Transfroms mutation instructions to mount items and executes them.
* The order of mutation instructions matters.
* Can be called from any thread.
*/
- (void)performTransactionWithMutations:(facebook::react::ShadowViewMutationList)mutations rootTag:(ReactTag)rootTag;
Expand Down
16 changes: 10 additions & 6 deletions React/Fabric/RCTSurfacePresenter.mm
Original file line number Diff line number Diff line change
Expand Up @@ -283,16 +283,20 @@ - (void)_stopSurface:(RCTFabricSurface *)surface

- (void)_startAllSurfaces
{
for (RCTFabricSurface *surface in _surfaceRegistry.enumerator) {
[self _startSurface:surface];
}
[_surfaceRegistry enumerateWithBlock:^(NSEnumerator<RCTFabricSurface *> *enumerator) {
for (RCTFabricSurface *surface in enumerator) {
[self _startSurface:surface];
}
}];
}

- (void)_stopAllSurfaces
{
for (RCTFabricSurface *surface in _surfaceRegistry.enumerator) {
[self _stopSurface:surface];
}
[_surfaceRegistry enumerateWithBlock:^(NSEnumerator<RCTFabricSurface *> *enumerator) {
for (RCTFabricSurface *surface in enumerator) {
[self _stopSurface:surface];
}
}];
}

#pragma mark - RCTSchedulerDelegate
Expand Down
4 changes: 3 additions & 1 deletion React/Fabric/RCTSurfaceRegistry.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ NS_ASSUME_NONNULL_BEGIN

@class RCTFabricSurface;

typedef void(^RCTSurfaceEnumeratorBlock)(NSEnumerator<RCTFabricSurface *> *enumerator);

/**
* Registry of Surfaces.
* Incapsulates storing Surface objects and quering them by root tag.
Expand All @@ -21,7 +23,7 @@ NS_ASSUME_NONNULL_BEGIN
*/
@interface RCTSurfaceRegistry : NSObject

- (NSEnumerator<RCTFabricSurface *> *)enumerator;
- (void)enumerateWithBlock:(RCTSurfaceEnumeratorBlock)block;

/**
* Adds Surface object into the registry.
Expand Down
5 changes: 2 additions & 3 deletions React/Fabric/RCTSurfaceRegistry.mm
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,10 @@ - (instancetype)init
return self;
}

- (NSEnumerator<RCTFabricSurface *> *)enumerator
- (void)enumerateWithBlock:(RCTSurfaceEnumeratorBlock)block
{
std::lock_guard<std::mutex> lock(_mutex);

return [_registry objectEnumerator];
block([_registry objectEnumerator]);
}

- (void)registerSurface:(RCTFabricSurface *)surface
Expand Down

0 comments on commit 27e7279

Please sign in to comment.