2019-10-16 20:03:47 +03:00
|
|
|
/*
|
2018-09-12 01:27:47 +03:00
|
|
|
* Copyright (c) Facebook, Inc. and its affiliates.
|
2018-04-11 02:37:04 +03:00
|
|
|
*
|
|
|
|
* This source code is licensed under the MIT license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree.
|
|
|
|
*/
|
|
|
|
|
2019-08-08 17:13:22 +03:00
|
|
|
#import "RCTSurfaceRegistry.h"
|
2018-04-11 02:37:04 +03:00
|
|
|
|
2020-02-27 08:26:48 +03:00
|
|
|
#import <better/mutex.h>
|
2018-04-11 02:37:04 +03:00
|
|
|
#import <mutex>
|
2019-03-29 20:27:20 +03:00
|
|
|
#import <shared_mutex>
|
2018-04-11 02:37:04 +03:00
|
|
|
|
2019-08-08 17:13:22 +03:00
|
|
|
#import <React/RCTFabricSurface.h>
|
2018-04-11 02:37:04 +03:00
|
|
|
|
2019-03-29 20:27:20 +03:00
|
|
|
using namespace facebook;
|
|
|
|
|
2019-08-08 17:13:22 +03:00
|
|
|
@implementation RCTSurfaceRegistry {
|
2019-03-29 20:27:20 +03:00
|
|
|
better::shared_mutex _mutex;
|
2019-08-08 17:13:22 +03:00
|
|
|
NSMapTable<id, RCTFabricSurface *> *_registry;
|
2018-04-11 02:37:04 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
- (instancetype)init
|
|
|
|
{
|
|
|
|
if (self = [super init]) {
|
|
|
|
_registry = [NSMapTable mapTableWithKeyOptions:NSPointerFunctionsIntegerPersonality | NSPointerFunctionsOpaqueMemory
|
2018-09-08 09:38:51 +03:00
|
|
|
valueOptions:NSPointerFunctionsObjectPersonality | NSPointerFunctionsWeakMemory];
|
2018-04-11 02:37:04 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2019-03-22 09:34:14 +03:00
|
|
|
- (void)enumerateWithBlock:(RCTSurfaceEnumeratorBlock)block
|
2018-09-26 20:01:36 +03:00
|
|
|
{
|
2019-03-29 20:27:20 +03:00
|
|
|
std::shared_lock<better::shared_mutex> lock(_mutex);
|
2019-03-22 09:34:14 +03:00
|
|
|
block([_registry objectEnumerator]);
|
2018-09-26 20:01:36 +03:00
|
|
|
}
|
|
|
|
|
2019-08-08 17:13:22 +03:00
|
|
|
- (void)registerSurface:(RCTFabricSurface *)surface
|
2018-04-11 02:37:04 +03:00
|
|
|
{
|
2019-03-29 20:27:20 +03:00
|
|
|
std::unique_lock<better::shared_mutex> lock(_mutex);
|
2018-04-11 02:37:04 +03:00
|
|
|
|
|
|
|
ReactTag rootTag = surface.rootViewTag.integerValue;
|
|
|
|
[_registry setObject:surface forKey:(__bridge id)(void *)rootTag];
|
|
|
|
}
|
|
|
|
|
2019-08-08 17:13:22 +03:00
|
|
|
- (void)unregisterSurface:(RCTFabricSurface *)surface
|
2018-04-11 02:37:04 +03:00
|
|
|
{
|
2019-03-29 20:27:20 +03:00
|
|
|
std::unique_lock<better::shared_mutex> lock(_mutex);
|
2018-04-11 02:37:04 +03:00
|
|
|
|
|
|
|
ReactTag rootTag = surface.rootViewTag.integerValue;
|
|
|
|
[_registry removeObjectForKey:(__bridge id)(void *)rootTag];
|
|
|
|
}
|
|
|
|
|
2019-08-08 17:13:22 +03:00
|
|
|
- (RCTFabricSurface *)surfaceForRootTag:(ReactTag)rootTag
|
2018-04-11 02:37:04 +03:00
|
|
|
{
|
2019-03-29 20:27:20 +03:00
|
|
|
std::shared_lock<better::shared_mutex> lock(_mutex);
|
2018-04-11 02:37:04 +03:00
|
|
|
|
|
|
|
return [_registry objectForKey:(__bridge id)(void *)rootTag];
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|