Skip to content

Commit

Permalink
fix: don't send command when element is disposed.
Browse files Browse the repository at this point in the history
  • Loading branch information
andycall committed Nov 24, 2021
1 parent a899519 commit 71020d2
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 14 deletions.
5 changes: 4 additions & 1 deletion bridge/bindings/qjs/dom/element.cc
Original file line number Diff line number Diff line change
Expand Up @@ -593,7 +593,10 @@ JSClassID ElementInstance::classID() {
return Element::classId();
}

ElementInstance::~ElementInstance() {}
ElementInstance::~ElementInstance() {
// Should reset weak reference between style and element.
m_style->ownerEventTarget = nullptr;
}

JSValue ElementInstance::internalGetTextContent() {
JSValue array = JS_NewArray(m_ctx);
Expand Down
20 changes: 11 additions & 9 deletions bridge/bindings/qjs/dom/style_declaration.cc
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ JSValue CSSStyleDeclaration::instanceConstructor(QjsContext* ctx, JSValue func_o
JSValue eventTargetValue = argv[0];

auto eventTargetInstance = static_cast<EventTargetInstance*>(JS_GetOpaque(eventTargetValue, EventTarget::classId(eventTargetValue)));
auto style = new StyleDeclarationInstance(this, eventTargetInstance->eventTargetId());
auto style = new StyleDeclarationInstance(this, eventTargetInstance);
return style->instanceObject;
}

Expand Down Expand Up @@ -118,10 +118,11 @@ bool StyleDeclarationInstance::internalSetProperty(std::string& name, JSValue va

properties[name] = jsValueToStdString(m_ctx, value);

NativeString* args_01 = stringToNativeString(name);
NativeString* args_02 = jsValueToNativeString(m_ctx, value);

foundation::UICommandBuffer::instance(m_context->getContextId())->addCommand(m_ownerEventTargetId, UICommand::setStyle, *args_01, *args_02, nullptr);
if (ownerEventTarget != nullptr) {
NativeString* args_01 = stringToNativeString(name);
NativeString* args_02 = jsValueToNativeString(m_ctx, value);
foundation::UICommandBuffer::instance(m_context->getContextId())->addCommand(ownerEventTarget->eventTargetId(), UICommand::setStyle, *args_01, *args_02, nullptr);
}

return true;
}
Expand All @@ -135,10 +136,11 @@ void StyleDeclarationInstance::internalRemoveProperty(std::string& name) {

properties.erase(name);

NativeString* args_01 = stringToNativeString(name);
NativeString* args_02 = jsValueToNativeString(m_ctx, JS_NULL);

foundation::UICommandBuffer::instance(m_context->getContextId())->addCommand(m_ownerEventTargetId, UICommand::setStyle, *args_01, *args_02, nullptr);
if (ownerEventTarget != nullptr) {
NativeString* args_01 = stringToNativeString(name);
NativeString* args_02 = jsValueToNativeString(m_ctx, JS_NULL);
foundation::UICommandBuffer::instance(m_context->getContextId())->addCommand(ownerEventTarget->eventTargetId(), UICommand::setStyle, *args_01, *args_02, nullptr);
}
}

JSValue StyleDeclarationInstance::internalGetPropertyValue(std::string& name) {
Expand Down
7 changes: 4 additions & 3 deletions bridge/bindings/qjs/dom/style_declaration.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,17 @@ class CSSStyleDeclaration : public HostClass {
class StyleDeclarationInstance : public Instance {
public:
StyleDeclarationInstance() = delete;
explicit StyleDeclarationInstance(CSSStyleDeclaration* cssStyleDeclaration, int32_t ownerEventTargetId)
: Instance(cssStyleDeclaration, "CSSStyleDeclaration", &m_exoticMethods, CSSStyleDeclaration::kCSSStyleDeclarationClassId, finalize), m_ownerEventTargetId(ownerEventTargetId){};
explicit StyleDeclarationInstance(CSSStyleDeclaration* cssStyleDeclaration, EventTargetInstance* ownerEventTarget)
: Instance(cssStyleDeclaration, "CSSStyleDeclaration", &m_exoticMethods, CSSStyleDeclaration::kCSSStyleDeclarationClassId, finalize), ownerEventTarget(ownerEventTarget){};
~StyleDeclarationInstance();
bool internalSetProperty(std::string& name, JSValue value);
void internalRemoveProperty(std::string& name);
JSValue internalGetPropertyValue(std::string& name);
std::string toString();
void copyWith(StyleDeclarationInstance* instance);

const EventTargetInstance* ownerEventTarget;

private:
static int hasProperty(QjsContext* ctx, JSValueConst obj, JSAtom atom);
static int setProperty(QjsContext* ctx, JSValueConst obj, JSAtom atom, JSValueConst value, JSValueConst receiver, int flags);
Expand All @@ -71,7 +73,6 @@ class StyleDeclarationInstance : public Instance {
static JSClassExoticMethods m_exoticMethods;

std::unordered_map<std::string, std::string> properties;
int32_t m_ownerEventTargetId;
friend EventTargetInstance;
};

Expand Down
2 changes: 1 addition & 1 deletion kraken/lib/src/dom/element_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ class ElementManager implements WidgetsBindingObserver, ElementsBindingObserver
}

void setInlineStyle(int targetId, String key, dynamic value) {
if (!existsTarget(targetId)) return;
assert(existsTarget(targetId), 'id: $targetId key: $key value: $value');
Node? target = getEventTargetByTargetId<Node>(targetId);
if (target == null) return;

Expand Down

0 comments on commit 71020d2

Please sign in to comment.