Skip to content

Commit

Permalink
fix(dom): fix modifying event by multi hippy engine
Browse files Browse the repository at this point in the history
  • Loading branch information
ilikethese authored and zealotchen0 committed Jul 25, 2023
1 parent c690f86 commit 4a731a2
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 16 deletions.
10 changes: 0 additions & 10 deletions driver/js/include/driver/modules/event_module.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,6 @@ namespace hippy {
inline namespace driver {
inline namespace module {

class DomEventWrapper {
public:
static void Set(std::shared_ptr<DomEvent> dom_event) { dom_event_ = dom_event; }
static std::shared_ptr<DomEvent> Get() { return dom_event_; }
static void Release() { dom_event_ = nullptr; }

private:
static std::shared_ptr<DomEvent> dom_event_;
};

std::shared_ptr<hippy::ClassTemplate<DomEvent>> MakeEventClassTemplate(const std::weak_ptr<Scope>& weak_scope);

} // namespace module
Expand Down
3 changes: 3 additions & 0 deletions driver/js/include/driver/scope.h
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,8 @@ class Scope : public std::enable_shared_from_this<Scope> {
hippy::dom::EventListenerInfo RemoveListener(const EventListenerInfo& event_listener_info);
bool HasListener(const EventListenerInfo& event_listener_info);
uint64_t GetListenerId(const EventListenerInfo& event_listener_info);
inline void SetCurrentEvent(std::any current_event) { current_event_ = current_event; }
inline std::any GetCurrentEvent() { return current_event_; }

void RunJS(const string_view& js,
const string_view& name,
Expand Down Expand Up @@ -461,6 +463,7 @@ class Scope : public std::enable_shared_from_this<Scope> {
std::unordered_map<uint32_t, std::shared_ptr<CtxValue>> call_ui_function_callback_holder_;
std::unordered_map<uint32_t, std::unordered_map<std::string, std::unordered_map<uint64_t, std::shared_ptr<CtxValue>>>>
bind_listener_map_; // bind js function and dom event listener id
std::any current_event_;
std::unique_ptr<ScopeWrapper> wrapper_;
std::weak_ptr<UriLoader> loader_;
std::weak_ptr<DomManager> dom_manager_;
Expand Down
12 changes: 7 additions & 5 deletions driver/js/src/modules/event_module.cc
Original file line number Diff line number Diff line change
Expand Up @@ -46,21 +46,23 @@ namespace hippy {
inline namespace driver {
inline namespace module {

std::shared_ptr<DomEvent> DomEventWrapper::dom_event_ = nullptr;

std::shared_ptr<ClassTemplate<DomEvent>> MakeEventClassTemplate(
const std::weak_ptr<Scope>& weak_scope) {
using DomEvent = hippy::dom::DomEvent;
ClassTemplate<DomEvent> class_template;
class_template.name = "Event";
class_template.constructor = [](
class_template.constructor = [weak_scope](
const std::shared_ptr<CtxValue>& receiver,
size_t argument_count,
const std::shared_ptr<CtxValue> arguments[],
void* external,
std::shared_ptr<CtxValue>& exception) -> std::shared_ptr<DomEvent> {
auto event = DomEventWrapper::Get();
return event;
auto scope = weak_scope.lock();
if (!scope) {
return nullptr;
}
auto current_event =std::any_cast<std::shared_ptr<DomEvent>>(scope->GetCurrentEvent());
return current_event;
};

// function
Expand Down
2 changes: 1 addition & 1 deletion driver/js/src/scope.cc
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,7 @@ hippy::dom::EventListenerInfo Scope::AddListener(const EventListenerInfo& event_
if (context) {
auto callback = event_listener_info.callback.lock();
if (callback == nullptr) return;
hippy::DomEventWrapper::Set(event);
scope->SetCurrentEvent(std::make_any<std::shared_ptr<hippy::dom::DomEvent>>(event));
auto event_class = scope->GetJavascriptClass(kEventName);
auto event_instance = context->NewInstance(event_class, 0, nullptr, nullptr);
FOOTSTONE_DCHECK(callback) << "callback is nullptr";
Expand Down

0 comments on commit 4a731a2

Please sign in to comment.