Fabric: Enabling and disabling EventEmitter at post commit phase

Summary:
@public
We need that to ensure that we will not deliver events to nodes with invalid state.

Reviewed By: mdvacca

Differential Revision: D8886234

fbshipit-source-id: 1d6ca129c97a5dca0411e85909aea48185f46c54
This commit is contained in:
Valentin Shergin 2018-08-27 07:21:18 -07:00 committed by Facebook Github Bot
parent b784adc7ae
commit 71295bcdac
2 changed files with 30 additions and 3 deletions

View File

@ -86,7 +86,7 @@ void ShadowTree::complete(UnsharedRootShadowNode newRootShadowNode) {
newRootShadowNode newRootShadowNode
); );
if (commit(oldRootShadowNode, newRootShadowNode)) { if (commit(oldRootShadowNode, newRootShadowNode, instructions)) {
emitLayoutEvents(instructions); emitLayoutEvents(instructions);
if (delegate_) { if (delegate_) {
@ -95,7 +95,11 @@ void ShadowTree::complete(UnsharedRootShadowNode newRootShadowNode) {
} }
} }
bool ShadowTree::commit(const SharedRootShadowNode &oldRootShadowNode, const SharedRootShadowNode &newRootShadowNode) { bool ShadowTree::commit(
const SharedRootShadowNode &oldRootShadowNode,
const SharedRootShadowNode &newRootShadowNode,
const TreeMutationInstructionList &mutationInstructions
) {
std::lock_guard<std::mutex> lock(commitMutex_); std::lock_guard<std::mutex> lock(commitMutex_);
if (oldRootShadowNode != rootShadowNode_) { if (oldRootShadowNode != rootShadowNode_) {
@ -103,6 +107,8 @@ bool ShadowTree::commit(const SharedRootShadowNode &oldRootShadowNode, const Sha
} }
rootShadowNode_ = newRootShadowNode; rootShadowNode_ = newRootShadowNode;
toggleEventEmitters(mutationInstructions);
return true; return true;
} }
@ -153,6 +159,22 @@ void ShadowTree::emitLayoutEvents(const TreeMutationInstructionList &instruction
} }
} }
void ShadowTree::toggleEventEmitters(const TreeMutationInstructionList &instructions) {
std::lock_guard<std::recursive_mutex> lock(EventEmitter::DispatchMutex());
for (const auto &instruction : instructions) {
if (instruction.getType() == TreeMutationInstruction::Deletion) {
instruction.getOldChildNode()->getEventEmitter()->setEnabled(false);
}
}
for (const auto &instruction : instructions) {
if (instruction.getType() == TreeMutationInstruction::Creation) {
instruction.getNewChildNode()->getEventEmitter()->setEnabled(true);
}
}
}
#pragma mark - Delegate #pragma mark - Delegate
void ShadowTree::setDelegate(ShadowTreeDelegate *delegate) { void ShadowTree::setDelegate(ShadowTreeDelegate *delegate) {

View File

@ -77,7 +77,12 @@ private:
UnsharedRootShadowNode cloneRootShadowNode(const LayoutConstraints &layoutConstraints, const LayoutContext &layoutContext) const; UnsharedRootShadowNode cloneRootShadowNode(const LayoutConstraints &layoutConstraints, const LayoutContext &layoutContext) const;
void complete(UnsharedRootShadowNode newRootShadowNode); void complete(UnsharedRootShadowNode newRootShadowNode);
bool commit(const SharedRootShadowNode &oldRootShadowNode, const SharedRootShadowNode &newRootShadowNode); bool commit(
const SharedRootShadowNode &oldRootShadowNode,
const SharedRootShadowNode &newRootShadowNode,
const TreeMutationInstructionList &mutationInstructions
);
void toggleEventEmitters(const TreeMutationInstructionList &instructions);
void emitLayoutEvents(const TreeMutationInstructionList &instructions); void emitLayoutEvents(const TreeMutationInstructionList &instructions);
const Tag rootTag_; const Tag rootTag_;