diff --git a/src/corelib/platform/wasm/qwasmsuspendresumecontrol.cpp b/src/corelib/platform/wasm/qwasmsuspendresumecontrol.cpp index bf1d30bf23c..7b71e73816b 100644 --- a/src/corelib/platform/wasm/qwasmsuspendresumecontrol.cpp +++ b/src/corelib/platform/wasm/qwasmsuspendresumecontrol.cpp @@ -180,12 +180,37 @@ QWasmEventHandler::QWasmEventHandler(emscripten::val element, const std::string QWasmEventHandler::~QWasmEventHandler() { + // Do nothing if this instance is default-constructed, or was moved from. + if (m_element.isUndefined()) + return; + QWasmSuspendResumeControl *suspendResume = QWasmSuspendResumeControl::get(); Q_ASSERT(suspendResume); m_element.call("removeEventListener", m_name, suspendResume->jsEventHandlerAt(m_eventHandlerIndex)); suspendResume->removeEventHandler(m_eventHandlerIndex); } +QWasmEventHandler::QWasmEventHandler(QWasmEventHandler&& other) noexcept +:m_element(std::move(other.m_element)) +,m_name(std::move(other.m_name)) +,m_eventHandlerIndex(other.m_eventHandlerIndex) +{ + other.m_element = emscripten::val(); + other.m_name = emscripten::val(); + other.m_eventHandlerIndex = 0; +} + +QWasmEventHandler& QWasmEventHandler::operator=(QWasmEventHandler&& other) noexcept +{ + m_element = std::move(other.m_element); + other.m_element = emscripten::val(); + m_name = std::move(other.m_name); + other.m_name = emscripten::val(); + m_eventHandlerIndex = other.m_eventHandlerIndex; + other.m_eventHandlerIndex = 0; + return *this; +} + // // The QWasmTimer class creates a native single-shot timer. The event handler is provided in the // constructor and can be reused: each call setTimeout() sets a new timeout, though with the diff --git a/src/corelib/platform/wasm/qwasmsuspendresumecontrol_p.h b/src/corelib/platform/wasm/qwasmsuspendresumecontrol_p.h index c500c40791a..8b660a91719 100644 --- a/src/corelib/platform/wasm/qwasmsuspendresumecontrol_p.h +++ b/src/corelib/platform/wasm/qwasmsuspendresumecontrol_p.h @@ -51,12 +51,13 @@ class Q_CORE_EXPORT QWasmEventHandler { public: QWasmEventHandler() = default; + QWasmEventHandler(emscripten::val element, const std::string &name, + std::function fn); ~QWasmEventHandler(); QWasmEventHandler(QWasmEventHandler const&) = delete; QWasmEventHandler& operator=(QWasmEventHandler const&) = delete; - QWasmEventHandler(emscripten::val element, const std::string &name, - std::function fn); - + QWasmEventHandler(QWasmEventHandler&& other) noexcept; + QWasmEventHandler& operator=(QWasmEventHandler&& other) noexcept; private: emscripten::val m_element; emscripten::val m_name;