Skip to content

Commit 257cdcd

Browse files
committed
[core] support component ref finder
1 parent e7e2abd commit 257cdcd

File tree

4 files changed

+68
-23
lines changed

4 files changed

+68
-23
lines changed

weex_core/Source/core/data_render/parser.cc

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ namespace data_render {
3333

3434
static const char kCreateComponentRecursively[] = "__createComponentRecursively";
3535
static const char kCreateBodyFunction[] = "main";
36+
static const char kAttrRepeatControl[] = "[[repeat]]";
3637

3738
struct ASTParser final {
3839
/* State */
@@ -60,6 +61,14 @@ struct ASTParser final {
6061
}
6162
}
6263

64+
inline void SetAttributeStatement(std::vector<Handle<Expression>> args,
65+
Handle<BlockStatement>& block) {
66+
Handle<Expression> set_attr_func_id = factory_->NewIdentifier("setAttr");
67+
Handle<CallExpression> call_func =
68+
factory_->NewCallExpression(set_attr_func_id, args);
69+
block->PushExpression(call_func);
70+
}
71+
6372
void AddAppendChildCall(json11::Json& json, Handle<Identifier>& parent_identifier,
6473
Handle<Identifier>& child_identifier) {
6574
Handle<BlockStatement> statement = stacks_[stacks_.size() - 1];
@@ -580,6 +589,12 @@ struct ASTParser final {
580589
std::vector<Handle<Expression>> control_exprs;
581590
if (control.is_object()) {
582591
control_exprs = ParseControl(control);
592+
if (control["repeat"].is_string()) {
593+
SetAttributeStatement({child_identifier,
594+
factory_->NewStringConstant(kAttrRepeatControl),
595+
factory_->NewStringConstant("")},
596+
stacks_[stacks_.size() - 1]);
597+
}
583598
}
584599
json11::Json node_id = json["nodeId"];
585600
json11::Json tag_name = json["tagName"];
@@ -655,22 +670,12 @@ struct ASTParser final {
655670
for (auto it = items.begin(); it != items.end(); ++it) {
656671
const auto& key = it->first;
657672
const auto& value = it->second;
658-
std::vector<Handle<Expression>> args;
659-
args.push_back(child_identifier);
660-
args.push_back(factory_->NewStringConstant(key));
661-
if (value.is_string()) {
662-
args.push_back(
663-
factory_->NewStringConstant(value.string_value()));
664-
} else {
665-
args.push_back(ParseBindingExpression(value));
666-
}
667-
668-
Handle<Expression> set_attr_func_id =
669-
factory_->NewIdentifier("setAttr");
670-
Handle<CallExpression> call_func =
671-
factory_->NewCallExpression(set_attr_func_id, args);
672-
Handle<BlockStatement> statement = stacks_[stacks_.size() - 1];
673-
statement->PushExpression(call_func);
673+
SetAttributeStatement(
674+
{child_identifier, factory_->NewStringConstant(key),
675+
value.is_string()
676+
? factory_->NewStringConstant(value.string_value())
677+
: ParseBindingExpression(value)},
678+
stacks_[stacks_.size() - 1]);
674679
}
675680
}
676681

weex_core/Source/core/data_render/vnode/vcomponent.cc

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,40 @@ static bool Equals(Value a, Value b) {
8686
return true;
8787
}
8888

89+
static void BuildRefsInner(
90+
std::unordered_map<std::string, VComponent::VNodeRefs> &ref_map,
91+
VNode *node, bool in_for_loop) {
92+
if (!node->ref().empty()) {
93+
VComponent::VNodeRef ref;
94+
ref.insert({"ref", node->render_object_ref()});
95+
auto it = ref_map.find(node->ref());
96+
if (it != ref_map.end()) {
97+
if (!in_for_loop) {
98+
return;
99+
}
100+
it->second.push_back(ref);
101+
} else {
102+
VComponent::VNodeRefs refs;
103+
refs.push_back(ref);
104+
ref_map.insert({node->ref(), refs});
105+
}
106+
}
107+
if (node->child_list()->size() > 0 && !node->IsVirtualComponent()) {
108+
if (node->attributes()->find("[[repeat]]") != node->attributes()->end()) {
109+
in_for_loop = true;
110+
}
111+
for (auto it = node->child_list()->begin(); it != node->child_list()->end();
112+
it++) {
113+
BuildRefsInner(ref_map, *it, in_for_loop);
114+
}
115+
}
116+
}
117+
118+
void VComponent::BuildRefMap() {
119+
ref_map_.clear();
120+
BuildRefsInner(ref_map_, root_vnode(), false);
121+
}
122+
89123
bool VComponent::Equal(VComponent *old_component) {
90124
VComponent *new_component = this;
91125
if (old_component == new_component) return true;
@@ -170,8 +204,10 @@ void VComponent::TravelVComponentsWithFunc(TravelTreeFunc func, VNode *root) {
170204

171205
void VComponent::DispatchCreated() {
172206
if (has_dispatch_created_) return;
207+
BuildRefMap();
173208
if (listener_) {
174-
listener_->OnCreated(this, ValueTo<Table>(&data_), ValueTo<Table>(&props_));
209+
listener_->OnCreated(this, ValueTo<Table>(&data_), ValueTo<Table>(&props_),
210+
ref_map_);
175211
}
176212
TravelVComponentsWithFunc(&VComponent::DispatchCreated, root_vnode());
177213
has_dispatch_created_ = true;
@@ -183,6 +219,7 @@ void VComponent::DispatchUpdated() {
183219
return;
184220
}
185221
if (!is_dirty_) return;
222+
BuildRefMap();
186223
if (listener_) {
187224
listener_->OnUpdated(this, ValueTo<Table>(&updated_props_));
188225
}

weex_core/Source/core/data_render/vnode/vcomponent.h

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,16 @@ namespace core {
2828
namespace data_render {
2929
class VComponent : public VNode {
3030
public:
31+
typedef std::unordered_map<std::string, std::string> VNodeRef;
32+
typedef std::vector<std::unordered_map<std::string, std::string>> VNodeRefs;
3133
class LifecycleListener {
3234
public:
33-
virtual void OnCreated(VComponent* component, Table* data,
34-
Table* props) = 0;
35+
virtual void OnCreated(
36+
VComponent* component, Table* data, Table* props,
37+
const std::unordered_map<std::string, VNodeRefs>& ref_map) = 0;
3538
virtual void OnUpdated(VComponent* component, Table* props) = 0;
39+
virtual void OnEvent(VComponent* component, const std::string& func,
40+
Array* params) = 0;
3641
virtual void OnDestroyed(VComponent* component) = 0;
3742
};
3843
VComponent(ExecState* exec_state, int template_id, const std::string& name,
@@ -71,6 +76,7 @@ class VComponent : public VNode {
7176
typedef void (VComponent::*TravelTreeFunc)();
7277
void TravelVComponentsWithFunc(TravelTreeFunc func, VNode* root);
7378

79+
void BuildRefMap();
7480
bool Equal(VComponent* old_component);
7581
void DispatchAttachedToParent() override;
7682
void DispatchDetachedFromParent() override;
@@ -90,7 +96,7 @@ class VComponent : public VNode {
9096
Value props_;
9197
Value updated_props_;
9298
std::unique_ptr<LifecycleListener> listener_;
93-
std::unordered_map<std::string, VNode*> refs_;
99+
std::unordered_map<std::string, VNodeRefs> ref_map_;
94100
std::unique_ptr<VNode> old_root_vnode_;
95101
std::unique_ptr<VNode> root_vnode_;
96102
ExecState* exec_state_;

weex_core/Source/core/data_render/vnode/vnode.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,6 @@ namespace data_render {
3131
class VComponent;
3232
class Value;
3333

34-
typedef std::function<void()> OnAttachedListener;
35-
typedef std::function<void()> OnDettachedListener;
36-
3734
class VNode {
3835
public:
3936
VNode(const std::string &tag_name, const std::string &node_id,

0 commit comments

Comments
 (0)