Skip to content

Commit

Permalink
feat(dom): add dom style diff
Browse files Browse the repository at this point in the history
  • Loading branch information
zealotchen0 committed Oct 18, 2023
1 parent 3523c95 commit aa93c41
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 14 deletions.
2 changes: 1 addition & 1 deletion dom/include/dom/diff_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class DiffUtils {
* k: 11,
* }
*/
static DiffValue DiffProps(const DomValueMap& old_props_map, const DomValueMap& new_props_map);
static DiffValue DiffProps(const DomValueMap& old_props_map, const DomValueMap& new_props_map, bool skip_style_diff);
};
} // namespace dom
} // namespace hippy
11 changes: 10 additions & 1 deletion dom/include/dom/dom_node.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,14 @@ enum RelativeType {
kBack = 1,
};

struct DiffInfo {
bool skip_style_diff;
DiffInfo(bool skip_style_diff) : skip_style_diff(skip_style_diff) {}

private:
friend std::ostream& operator<<(std::ostream& os, const DiffInfo& diff_info);
};

struct RefInfo {
uint32_t ref_id;
int32_t relative_to_ref = RelativeType::kDefault;
Expand All @@ -67,7 +75,8 @@ struct RefInfo {
struct DomInfo {
std::shared_ptr<DomNode> dom_node;
std::shared_ptr<RefInfo> ref_info;
DomInfo(std::shared_ptr<DomNode> node, std::shared_ptr<RefInfo> ref) : dom_node(node), ref_info(ref) {}
std::shared_ptr<DiffInfo> diff_info;
DomInfo(std::shared_ptr<DomNode> node, std::shared_ptr<RefInfo> ref, std::shared_ptr<DiffInfo> diff) : dom_node(node), ref_info(ref), diff_info(diff) {}

private:
friend std::ostream& operator<<(std::ostream& os, const DomInfo& dom_info);
Expand Down
6 changes: 5 additions & 1 deletion dom/src/dom/diff_utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,13 @@ static bool ShouldUpdateProperty(const std::string& key, const DomValueMap& old_
return false;
}

DiffValue DiffUtils::DiffProps(const DomValueMap& old_props_map, const DomValueMap& new_props_map) {
DiffValue DiffUtils::DiffProps(const DomValueMap& old_props_map, const DomValueMap& new_props_map, bool skip_style_diff) {
std::shared_ptr<DomValueMap> update_props = std::make_shared<DomValueMap>();
std::shared_ptr<std::vector<std::string>> delete_props = std::make_shared<std::vector<std::string>>();
if (skip_style_diff) {
// 跳过 style diff 计算
return std::make_tuple(update_props, delete_props);
}

// delete props
// Example:
Expand Down
2 changes: 1 addition & 1 deletion dom/src/dom/dom_manager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ bool DomManager::SetSnapShot(const std::shared_ptr<RootNode>& root_node, const b
if (dom_node->GetPid() == orig_root_id) {
dom_node->SetPid(root_node->GetId());
}
nodes.push_back(std::make_shared<DomInfo>(dom_node, nullptr));
nodes.push_back(std::make_shared<DomInfo>(dom_node, nullptr, nullptr));
}

CreateDomNodes(root_node, std::move(nodes));
Expand Down
3 changes: 2 additions & 1 deletion dom/src/dom/dom_manager_unittests.cc
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,8 @@ std::vector<std::shared_ptr<hippy::DomInfo>> ParserJson(const std::string& json_
ref = std::make_shared<hippy::dom::RefInfo>(id, ref_id);
}

std::shared_ptr<hippy::dom::DomInfo> dom_info = std::make_shared<hippy::dom::DomInfo>(dom_node, ref);
auto diff_info = std::make_shared<hippy::dom::DiffInfo>(false);
std::shared_ptr<hippy::dom::DomInfo> dom_info = std::make_shared<hippy::dom::DomInfo>(dom_node, ref, diff_info);
nodes.push_back(dom_info);
}
return nodes;
Expand Down
4 changes: 2 additions & 2 deletions dom/src/dom/dom_node.cc
Original file line number Diff line number Diff line change
Expand Up @@ -494,8 +494,8 @@ void DomNode::UpdateDiff(const std::unordered_map<std::string,
std::shared_ptr<HippyValue>>& update_style,
const std::unordered_map<std::string,
std::shared_ptr<HippyValue>>& update_dom_ext) {
auto style_diff_value = DiffUtils::DiffProps(*this->GetStyleMap(), update_style);
auto ext_diff_value = DiffUtils::DiffProps(*this->GetExtStyle(), update_dom_ext);
auto style_diff_value = DiffUtils::DiffProps(*this->GetStyleMap(), update_style, false);
auto ext_diff_value = DiffUtils::DiffProps(*this->GetExtStyle(), update_dom_ext, false);
auto style_update = std::get<0>(style_diff_value);
auto ext_update = std::get<0>(ext_diff_value);
std::shared_ptr<DomValueMap> diff_value = std::make_shared<DomValueMap>();
Expand Down
10 changes: 7 additions & 3 deletions dom/src/dom/root_node.cc
Original file line number Diff line number Diff line change
Expand Up @@ -141,9 +141,13 @@ void RootNode::UpdateDomNodes(std::vector<std::shared_ptr<DomInfo>>&& nodes) {
if (dom_node == nullptr) {
continue;
}
auto skip_style_diff = false;
if (node_info->diff_info != nullptr) {
skip_style_diff = node_info->diff_info->skip_style_diff;
}
// diff props
auto style_diff_value = DiffUtils::DiffProps(*dom_node->GetStyleMap(), *node_info->dom_node->GetStyleMap());
auto ext_diff_value = DiffUtils::DiffProps(*dom_node->GetExtStyle(), *node_info->dom_node->GetExtStyle());
auto style_diff_value = DiffUtils::DiffProps(*dom_node->GetStyleMap(), *node_info->dom_node->GetStyleMap(), skip_style_diff);
auto ext_diff_value = DiffUtils::DiffProps(*dom_node->GetExtStyle(), *node_info->dom_node->GetExtStyle(), false);
auto style_update = std::get<0>(style_diff_value);
auto ext_update = std::get<0>(ext_diff_value);
std::shared_ptr<DomValueMap> diff_value = std::make_shared<DomValueMap>();
Expand Down Expand Up @@ -205,7 +209,7 @@ void RootNode::MoveDomNodes(std::vector<std::shared_ptr<DomInfo>>&& nodes) {
continue;
}
nodes_to_move.push_back(node);
parent_node->AddChildByRefInfo(std::make_shared<DomInfo>(node, node_info->ref_info));
parent_node->AddChildByRefInfo(std::make_shared<DomInfo>(node, node_info->ref_info, nullptr));
}
for (const auto& node : nodes_to_move) {
node->SetRenderInfo({node->GetId(), node->GetPid(), node->GetSelfIndex()});
Expand Down
20 changes: 16 additions & 4 deletions driver/js/src/modules/scene_builder_module.cc
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ constexpr char kNodePropertyProps[] = "props";
constexpr char kNodePropertyStyle[] = "style";
constexpr char kNodePropertyRefId[] = "refId";
constexpr char kNodePropertyRelativeToRef[] = "relativeToRef";
constexpr char KNodePropertySkipStyleDiff[] = "skipStyleDiff";
constexpr char kEventCapture[] = "capture";

const int32_t kInvalidValue = -1;
Expand Down Expand Up @@ -322,6 +323,7 @@ std::tuple<bool, std::string, std::shared_ptr<DomInfo>> CreateDomInfo(
std::shared_ptr<DomInfo> dom_info = nullptr;
std::shared_ptr<DomNode> dom_node = nullptr;
std::shared_ptr<RefInfo> ref_info = nullptr;
std::shared_ptr<DiffInfo> diff_info = nullptr;
uint32_t len = context->GetArrayLength(node);
if (len > 0) {
auto dom_node_tuple =
Expand All @@ -330,17 +332,26 @@ std::tuple<bool, std::string, std::shared_ptr<DomInfo>> CreateDomInfo(
return std::make_tuple(false, "get dom node info error.", dom_info);
}
dom_node = std::get<2>(dom_node_tuple);
if (len == 2) {
if (len > 1) {
auto ref_info_tuple =
CreateRefInfo(context, context->CopyArrayElement(node, 1), scope);
if (std::get<0>(ref_info_tuple)) {
ref_info = std::get<2>(ref_info_tuple);
}
}
if (len == 3) {
auto diff = context->CopyArrayElement(node, 2);
std::shared_ptr<CtxValue> style_diff = context->GetProperty(diff, KNodePropertySkipStyleDiff);
if (style_diff) {
bool skip_style_diff;
context->GetValueBoolean(style_diff, &skip_style_diff);
diff_info = std::make_shared<hippy::dom::DiffInfo>(skip_style_diff);
}
}
} else {
return std::make_tuple(false, "dom info length error.", dom_info);
}
dom_info = std::make_shared<DomInfo>(dom_node, ref_info);
dom_info = std::make_shared<DomInfo>(dom_node, ref_info, diff_info);
return std::make_tuple(true, "", dom_info);
}

Expand Down Expand Up @@ -484,7 +495,8 @@ std::shared_ptr<ClassTemplate<SceneBuilder>> RegisterSceneBuilder(const std::wea
std::get<2>(id_tuple),
std::get<2>(pid_tuple),
scope->GetRootNode()),
std::get<2>(ref_info_tuple)));
std::get<2>(ref_info_tuple),
nullptr));
}
}
}
Expand Down Expand Up @@ -528,7 +540,7 @@ std::shared_ptr<ClassTemplate<SceneBuilder>> RegisterSceneBuilder(const std::wea
std::get<2>(id_tuple),
std::get<2>(pid_tuple),
scope->GetRootNode()),
nullptr));
nullptr, nullptr));
}
}
SceneBuilder::Delete(scope->GetDomManager(), scope->GetRootNode(), std::move(dom_infos));
Expand Down

0 comments on commit aa93c41

Please sign in to comment.