Fabric: Refined conception and usage of Sealable

Summary: Slightly new approach: Some non-const methods might not always mutate objects, so sometimes we should call `ensureUnsealed()` only inside conditional branches where we actually mutate an instance.

Reviewed By: fkgozali

Differential Revision: D7467793

fbshipit-source-id: 1b9f229cf6816e54e0df36699a571fdb612d3c3c
This commit is contained in:
Valentin Shergin 2018-04-10 12:45:35 -07:00 коммит произвёл Facebook Github Bot
Родитель c83aeaaf82
Коммит 7e84cadc9c
2 изменённых файлов: 10 добавлений и 5 удалений

Просмотреть файл

@ -23,6 +23,8 @@ bool LayoutableShadowNode::setLayoutMetrics(LayoutMetrics layoutMetrics) {
return false;
}
ensureUnsealed();
layoutMetrics_ = layoutMetrics;
return true;
}
@ -60,8 +62,6 @@ Float LayoutableShadowNode::lastBaseline(Size size) const {
}
void LayoutableShadowNode::layout(LayoutContext layoutContext) {
ensureUnsealed();
layoutChildren(layoutContext);
for (auto child : getChildren()) {
@ -69,6 +69,8 @@ void LayoutableShadowNode::layout(LayoutContext layoutContext) {
continue;
}
ensureUnsealed();
// The assumption:
// All `sealed` children were replaced with not-yet-sealed clones
// somewhere in `layoutChildren`.
@ -89,7 +91,6 @@ void LayoutableShadowNode::layout(LayoutContext layoutContext) {
}
void LayoutableShadowNode::layoutChildren(LayoutContext layoutContext) {
ensureUnsealed();
// Default implementation does nothing.
}

Просмотреть файл

@ -31,7 +31,12 @@ namespace react {
*
* How to use:
* 1. Inherit your class from `Sealable`.
* 2. Call `ensureUnsealed()` from all non-const methods.
* 2. Call `ensureUnsealed()` in all cases where the object might be mutated:
* a. At the beginning of all *always* mutating `non-const` methods;
* b. Right before the place where actual mutation happens in all *possible*
* mutating `non-const` methods;
* c. Right after performing `const_cast`. (Optionally. This is not strictly
* necessary but might help detect problems earlier.)
* 3. Call `seal()` at some point from which any modifications
* must be prevented.
*/
@ -55,7 +60,6 @@ public:
*/
bool getSealed() const;
protected:
/*
* Throws an exception if the object is sealed.
* Call this from all non-`const` methods.