Simplify invalidate batching in Layout plugin
Summary: - Avoid holding lock while calling out to `send:withParams:`; it's hard to reason about whether we could trigger deadlock. - Remove unnecessary `lastInvalidateMessage` ivar; we already ensure that we send at most one invalidate batch per 500ms by using `invalidateMessageQueued`. - Misc code style items Reviewed By: Andrey-Mishanin Differential Revision: D20462193 fbshipit-source-id: 80f61e5a7ce5021e16ebc19a2ec40adfc46f9b92
This commit is contained in:
committed by
Facebook GitHub Bot
parent
4784c45778
commit
89d6dfcf95
@@ -24,9 +24,8 @@
|
|||||||
NSMapTable<NSString*, id>* _trackedObjects;
|
NSMapTable<NSString*, id>* _trackedObjects;
|
||||||
NSString* _lastHighlightedNode;
|
NSString* _lastHighlightedNode;
|
||||||
NSMutableSet* _invalidObjects;
|
NSMutableSet* _invalidObjects;
|
||||||
Boolean _invalidateMessageQueued;
|
BOOL _invalidateMessageQueued;
|
||||||
NSDate* _lastInvalidateMessage;
|
std::mutex _invalidObjectsMutex;
|
||||||
std::mutex invalidObjectsMutex;
|
|
||||||
|
|
||||||
id<NSObject> _rootNode;
|
id<NSObject> _rootNode;
|
||||||
id<SKTapListener> _tapListener;
|
id<SKTapListener> _tapListener;
|
||||||
@@ -49,10 +48,7 @@
|
|||||||
if (self = [super init]) {
|
if (self = [super init]) {
|
||||||
_descriptorMapper = mapper;
|
_descriptorMapper = mapper;
|
||||||
_trackedObjects = [NSMapTable strongToWeakObjectsMapTable];
|
_trackedObjects = [NSMapTable strongToWeakObjectsMapTable];
|
||||||
_lastHighlightedNode = nil;
|
|
||||||
_invalidObjects = [NSMutableSet new];
|
_invalidObjects = [NSMutableSet new];
|
||||||
_invalidateMessageQueued = false;
|
|
||||||
_lastInvalidateMessage = [NSDate date];
|
|
||||||
_rootNode = rootNode;
|
_rootNode = rootNode;
|
||||||
_tapListener = tapListener;
|
_tapListener = tapListener;
|
||||||
|
|
||||||
@@ -399,36 +395,33 @@
|
|||||||
[descriptor invalidateNode:node];
|
[descriptor invalidateNode:node];
|
||||||
|
|
||||||
// Collect invalidate messages before sending in a batch
|
// Collect invalidate messages before sending in a batch
|
||||||
std::lock_guard<std::mutex> lock(invalidObjectsMutex);
|
std::lock_guard<std::mutex> lock(_invalidObjectsMutex);
|
||||||
[_invalidObjects addObject:nodeId];
|
[_invalidObjects addObject:nodeId];
|
||||||
if (_invalidateMessageQueued) {
|
if (_invalidateMessageQueued) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
_invalidateMessageQueued = true;
|
_invalidateMessageQueued = YES;
|
||||||
|
|
||||||
if (_lastInvalidateMessage.timeIntervalSinceNow < -1) {
|
|
||||||
dispatch_after(
|
dispatch_after(
|
||||||
dispatch_time(DISPATCH_TIME_NOW, 500 * NSEC_PER_MSEC),
|
dispatch_time(DISPATCH_TIME_NOW, 500 * NSEC_PER_MSEC),
|
||||||
dispatch_get_main_queue(),
|
dispatch_get_main_queue(),
|
||||||
^{
|
^{
|
||||||
[self reportInvalidatedObjects];
|
[self _reportInvalidatedObjects];
|
||||||
});
|
});
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
- (void)reportInvalidatedObjects {
|
- (void)_reportInvalidatedObjects {
|
||||||
std::lock_guard<std::mutex> lock(invalidObjectsMutex);
|
|
||||||
NSMutableArray* nodes = [NSMutableArray new];
|
NSMutableArray* nodes = [NSMutableArray new];
|
||||||
for (NSString* nodeId in self->_invalidObjects) {
|
{ // scope mutex acquisition
|
||||||
[nodes addObject:[NSDictionary dictionaryWithObject:nodeId forKey:@"id"]];
|
std::lock_guard<std::mutex> lock(_invalidObjectsMutex);
|
||||||
|
for (NSString* nodeId in _invalidObjects) {
|
||||||
|
[nodes addObject:@{@"id" : nodeId}];
|
||||||
}
|
}
|
||||||
[self->_connection send:@"invalidate"
|
_invalidObjects = [NSMutableSet new];
|
||||||
withParams:[NSDictionary dictionaryWithObject:nodes
|
_invalidateMessageQueued = NO;
|
||||||
forKey:@"nodes"]];
|
} // release mutex before calling out to other code
|
||||||
self->_lastInvalidateMessage = [NSDate date];
|
|
||||||
self->_invalidObjects = [NSMutableSet new];
|
[_connection send:@"invalidate" withParams:@{@"nodes" : nodes}];
|
||||||
self->_invalidateMessageQueued = false;
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
- (void)updateNodeReference:(id<NSObject>)node {
|
- (void)updateNodeReference:(id<NSObject>)node {
|
||||||
|
|||||||
Reference in New Issue
Block a user