Skip to content
This repository has been archived by the owner on Jun 3, 2021. It is now read-only.

Commit

Permalink
[Android][iOS][C++] Merge eagle modify code to master (#2199)
Browse files Browse the repository at this point in the history
Cherry pick the following commits.

* [core] fix the error of vnode ref is null
* [iOS] add lifecycle of mounted on iOS
* [core] fix the order of onupdated
* [Android] Add mounted for Android
* [Android] Update OnMounted
* [Android] Update event binding.
* [Android] Fix memory leak in Android
  • Loading branch information
jianhan-he authored and YorkShen committed Mar 11, 2019
1 parent 36aedc2 commit bf88859
Show file tree
Hide file tree
Showing 13 changed files with 96 additions and 9 deletions.
8 changes: 8 additions & 0 deletions weex_core/Source/core/data_render/parser.cc
Original file line number Diff line number Diff line change
Expand Up @@ -578,6 +578,14 @@ struct ASTParser final {
statement->PushExpression(child_declaration);
}

args.clear();
args.push_back(child_identifier);
args.push_back(attr_init);
Handle<Expression> set_component_func = factory_->NewIdentifier("setComponentRef");
Handle<Expression> call_set_component_expr = nullptr;
call_set_component_expr = factory_->NewCallExpression(set_component_func, args);
statement->PushExpression(call_set_component_expr);

// appendChild(parent, child);
AddAppendChildCall(json, parent_identifier, child_identifier);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ VALUE_WITH_TYPE* Conversion::GenValueWithType(const char* event_str) {
buffer, reinterpret_cast<const uint8_t*>(event_str), strlen(event_str));
event->value.byteArray = genWeexByteArray(
static_cast<const char*>(buffer->data), buffer->position);
buffer->data = nullptr;
wson_buffer_free(buffer);
return event;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ static const char* kKeyMethod = "method";
static const char* kKeyArgs = "args";
static const char* kEventOnCreated = "created";
static const char* kEventOnUpdated = "updated";
static const char* kEventOnMounted = "mounted";
static const char* kEventOnDestroyed = "destroyed";
static int kNonParentId = -1;

Expand Down Expand Up @@ -202,7 +203,6 @@ static void GenParamsForCallJS(std::vector<VALUE_WITH_TYPE*>& params,

args->value.byteArray = genWeexByteArray(
static_cast<const char*>(buffer->data), buffer->position);
buffer->data = nullptr;
wson_buffer_free(buffer);
params.push_back(args);
}
Expand All @@ -229,6 +229,23 @@ void VComponentLifecycleListener::OnCreated(
freeParams(params);
}

void VComponentLifecycleListener::OnMounted(
VComponent* component,
const std::unordered_map<std::string, VComponent::VNodeRefs>& ref_map) {
auto page_id = component->exec_state()->context()->page_id();

std::vector<VALUE_WITH_TYPE*> params;
// [pageId, args]
//
// args -> { method: 'componentHook', args: [ componentId, 'lifecycle',
// lifecycle, [props, refList] ] }
GenParamsForCallJS(params, component, kEventOnMounted, component->id(), 1,
ref_map);
WeexCore::WeexCoreManager::Instance()->script_bridge()->script_side()->ExecJS(
page_id.c_str(), "", kMethodOnComponentEvent, params);
freeParams(params);
}

void VComponentLifecycleListener::OnUpdated(
VComponent* component, Table* props,
const std::unordered_map<std::string, VComponent::VNodeRefs>& ref_map) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,7 @@ void VNodeOnEventListener::OnEvent(VNode* node, const std::string& event,

std::vector<json11::Json> args_in_args_object;
// ref TODO make sure the difference between node id and ref
if (!node->ref().empty()) {
args_in_args_object.push_back(node->ref());
} else {
args_in_args_object.push_back(node->node_id());
}
args_in_args_object.push_back(node->node_id());
// type -> "nodeEvent"
args_in_args_object.push_back("nodeEvent");
// args
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,18 @@
[[WXSDKManager bridgeMgr] callJSMethod:@"callJS" args:@[instanceId, @[@{@"method":@"componentHook", @"args":@[[NSNumber numberWithInt:template_id], @"lifecycle", @"created", @[[NSNumber numberWithInt:component_id],[NSNumber numberWithInt:parent_id], NSDICTIONARY(data), NSDICTIONARY(props), NSDICTIONARY(ref_map)]]}]]];
}

void VComponentLifecycleListener::OnMounted(VComponent* component, const std::unordered_map<std::string, VComponent::VNodeRefs>& ref_map) {
std::string page_id = component->exec_state()->context()->page_id();
NSString *instanceId = NSSTRING(page_id.c_str());
WXSDKInstance *instance = [WXSDKManager instanceForID:instanceId];
if (!instance) {
return;
}

int component_id = component->id();
[[WXSDKManager bridgeMgr] callJSMethod:@"callJS" args:@[instanceId, @[@{@"method":@"componentHook", @"args":@[[NSNumber numberWithInt:component_id], @"lifecycle", @"mounted", @[NSDICTIONARY(ref_map)]]}]]];
}

void VComponentLifecycleListener::OnUpdated(
VComponent* component, Table* props, const std::unordered_map<std::string, VComponent::VNodeRefs>& ref_map) {
std::string page_id = component->exec_state()->context()->page_id();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
int template_id = node->component()->template_id();
int component_id = node->component()->id();

[[WXSDKManager bridgeMgr] callJSMethod:@"callJS" args:@[instanceId, @[@{@"method":@"fireEvent", @"args":@[NSSTRING(node->ref().empty() ? node->node_id().c_str() : node->ref().c_str()), @"nodeEvent", [WXUtility objectFromJSON:NSSTRING(json_args.c_str())], NSSTRING(dom_changes.c_str()), @{@"params":@[@{@"templateId":[NSNumber numberWithInt:template_id], @"componentId":[NSNumber numberWithInt:component_id],@"type":NSSTRING(event.c_str()), @"params": NSARRAY(params)}]}]}]]];
[[WXSDKManager bridgeMgr] callJSMethod:@"callJS" args:@[instanceId, @[@{@"method":@"fireEvent", @"args":@[NSSTRING(node->node_id().c_str()), @"nodeEvent", [WXUtility objectFromJSON:NSSTRING(json_args.c_str())], NSSTRING(dom_changes.c_str()), @{@"params":@[@{@"templateId":[NSNumber numberWithInt:template_id], @"componentId":[NSNumber numberWithInt:component_id],@"type":NSSTRING(event.c_str()), @"params": NSARRAY(params)}]}]}]]];
}

} // namespace data_render
Expand Down
20 changes: 19 additions & 1 deletion weex_core/Source/core/data_render/vnode/vcomponent.cc
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ VComponent::VComponent(ExecState *exec_state, int template_id,
: VNode("", "", ""),
is_dirty_(false),
has_dispatch_created_(false),
has_dispatch_mounted_(false),
has_moved_(false),
id_(g_component_id++),
template_id_(template_id),
Expand Down Expand Up @@ -103,6 +104,12 @@ static void BuildRefsInner(
if (!node->ref().empty()) {
VComponent::VNodeRef ref;
ref.insert({"ref", node->render_object_ref()});
if (node->IsVirtualComponent()) {
ref.insert({"isComponent", "true"});
ref.insert({"componentId", std::to_string(node->component()->id())});
} else {
ref.insert({"tagName", node->tag_name()});
}
auto it = ref_map.find(node->ref());
if (it != ref_map.end()) {
if (!in_for_loop) {
Expand Down Expand Up @@ -171,6 +178,7 @@ bool VComponent::Equal(VComponent *old_component) {

void VComponent::MoveTo(VComponent *new_component) {
new_component->has_dispatch_created_ = has_dispatch_created_;
new_component->has_dispatch_mounted_ = has_dispatch_mounted_;
new_component->id_ = id_;
new_component->SetRootNode(root_vnode_.release());
new_component->data_ = data_;
Expand Down Expand Up @@ -241,20 +249,30 @@ void VComponent::DispatchCreated() {
has_dispatch_created_ = true;
}

void VComponent::DispatchMounted() {
if (has_dispatch_mounted_) return;
TravelVComponentsWithFunc(&VComponent::DispatchMounted, root_vnode());
if (listener_) {
listener_->OnMounted(this, ref_map_);
}
has_dispatch_mounted_ = true;
}

void VComponent::DispatchUpdated() {
if (!has_dispatch_created_) {
DispatchCreated();
DispatchMounted();
return;
}
if (!is_dirty_) return;
TravelVComponentsWithFunc(&VComponent::DispatchUpdated, root_vnode());
BuildRefMap();
if (listener_) {
if (updated_props_.type == Value::Type::NIL) {
updated_props_ = exec_state_->class_factory()->CreateTable();
}
listener_->OnUpdated(this, ValueTo<Table>(&updated_props_), ref_map_);
}
TravelVComponentsWithFunc(&VComponent::DispatchUpdated, root_vnode());
is_dirty_ = false;
}

Expand Down
3 changes: 3 additions & 0 deletions weex_core/Source/core/data_render/vnode/vcomponent.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class VComponent : public VNode {
virtual void OnCreated(
VComponent* component, Table* data, Table* props,
const std::unordered_map<std::string, VNodeRefs>& ref_map) = 0;
virtual void OnMounted(VComponent* component, const std::unordered_map<std::string, VNodeRefs>& ref_map) = 0;
virtual void OnUpdated(
VComponent* component, Table* props,
const std::unordered_map<std::string, VNodeRefs>& ref_map) = 0;
Expand Down Expand Up @@ -69,6 +70,7 @@ class VComponent : public VNode {
VComponent* component() override {return this;}

void DispatchCreated();
void DispatchMounted();
void DispatchUpdated();
void DispatchDestroyed();

Expand All @@ -88,6 +90,7 @@ class VComponent : public VNode {

bool is_dirty_;
bool has_dispatch_created_;
bool has_dispatch_mounted_;
bool has_moved_;
// unique id for unique component
int id_;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class VComponentLifecycleListener : public VComponent::LifecycleListener {
VComponent* component, Table* props,
const std::unordered_map<std::string, VComponent::VNodeRefs>& ref_map);
void OnDestroyed(VComponent* component);
void OnMounted(VComponent* component, const std::unordered_map<std::string, VComponent::VNodeRefs>& ref_map);
};
} // namespace data_render
} // namespace core
Expand Down
3 changes: 3 additions & 0 deletions weex_core/Source/core/data_render/vnode/vnode.cc
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ void VNode::SetStyle(const std::string &key, const std::string &value) {
}

void VNode::SetAttribute(const std::string &key, const std::string &value) {
if (key == "ref") {
ref_ = value;
}
MapInsertOrAssign(attributes_, key, value);
}

Expand Down
3 changes: 3 additions & 0 deletions weex_core/Source/core/data_render/vnode/vnode.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@ class VNode {
return render_object_ref_;
}

inline void set_ref(std::string ref) {
ref_ = ref;
}
inline void set_render_object_ref(std::string ref) {
render_object_ref_ = std::move(ref);
}
Expand Down
25 changes: 25 additions & 0 deletions weex_core/Source/core/data_render/vnode/vnode_exec_env.cc
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,30 @@ static Value SetComponentRoot(ExecState* exec_state) {
return Value();
}

// setComponentRoot(component, "attrs");
static Value SetComponentRef(ExecState* exec_state) {
VComponent *component = exec_state->GetArgument(0)->type == Value::Type::NIL ?
nullptr : reinterpret_cast<VComponent *>(exec_state->GetArgument(0)->cptr);
if (!component) {
return Value();
}
Value *attributes = exec_state->GetArgument(1);
if (IsTable(attributes)) {
Table* attrs = ValueTo<Table>(attributes);
auto it = attrs->map.find("ref");
if (it != attrs->map.end()) {
Value value = it->second;
if (value.type == Value::STRING) {
component->SetAttribute(it->first, value.str->c_str());
} else if (value.type == Value::INT) {
std::string str = std::to_string(value.i);
component->SetAttribute(it->first, str);
}
}
}
return Value();
}

// createComponent(template_id, "template_name", func_name);
static Value CreateComponent(ExecState* exec_state) {
int template_id = 0;
Expand Down Expand Up @@ -594,6 +618,7 @@ void VNodeExecEnv::ImportExecEnv(ExecState *state) {
state->Register("createComponent", CreateComponent);
state->Register("saveComponentPropsAndData", saveComponentPropsAndData);
state->Register("setComponentRoot", SetComponentRoot);
state->Register("setComponentRef", SetComponentRef);
state->Register("appendChild", AppendChild);
state->Register("encodeURIComponent", encodeURIComponent);
state->Register("encodeURI", encodeURIComponent);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,8 @@ void VNodeRenderManager::DownloadAndExecScript(
if (root && root->IsVirtualComponent()) {
static_cast<weex::core::data_render::VComponent*>(root)
->DispatchCreated();
static_cast<weex::core::data_render::VComponent*>(root)
->DispatchMounted();

//fire event
exec_state->set_exec_js_finished(true);
Expand Down

0 comments on commit bf88859

Please sign in to comment.