From 59bd3ea1ab90d14c0a0be45c6445cc699ca47251 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E5=8A=B2=E9=B9=8F?= Date: Tue, 8 Jun 2021 10:08:38 +0800 Subject: [PATCH] add subscription recycle --- doric-Qt/example/doric/DoricContext.cpp | 2 ++ .../example/doric/plugin/DoricNotificationPlugin.cpp | 11 ++++++++++- .../example/doric/plugin/DoricNotificationPlugin.h | 6 +++--- doric-Qt/example/doric/utils/DoricGlobalBroadcast.cpp | 6 +++++- 4 files changed, 20 insertions(+), 5 deletions(-) diff --git a/doric-Qt/example/doric/DoricContext.cpp b/doric-Qt/example/doric/DoricContext.cpp index 15caec45..0123e5ad 100644 --- a/doric-Qt/example/doric/DoricContext.cpp +++ b/doric-Qt/example/doric/DoricContext.cpp @@ -19,6 +19,8 @@ DoricContext::~DoricContext() { QVariantList args; callEntity(DoricConstant::DORIC_ENTITY_DESTROY, args); DoricContextManager::getInstance()->destroyContext(this); + QList plugins = mPluginMap.values(); + foreach (QObject *plugin, plugins) { delete plugin; } } DoricContext *DoricContext::create(QString script, QString source, diff --git a/doric-Qt/example/doric/plugin/DoricNotificationPlugin.cpp b/doric-Qt/example/doric/plugin/DoricNotificationPlugin.cpp index ea859d65..00aaa33d 100644 --- a/doric-Qt/example/doric/plugin/DoricNotificationPlugin.cpp +++ b/doric-Qt/example/doric/plugin/DoricNotificationPlugin.cpp @@ -53,6 +53,7 @@ void DoricNotificationPlugin::subscribe(QString jsValueString, args.append(data); DoricPromise::resolve(getContext(), notificationCallbackId, args); }); + subscriptions.append(subscribeId); getContext()->getDriver()->asyncCall( [this, callbackId, notificationCallbackId] { @@ -68,7 +69,9 @@ void DoricNotificationPlugin::unsubscribe(QString jsValueString, QJsonDocument document = QJsonDocument::fromJson(jsValueString.toUtf8()); QJsonValue jsValue = document.object(); - DoricGlobalBroadcast::getInstance()->unsubscribe(jsValueString); + QString subscribeId = jsValueString; + DoricGlobalBroadcast::getInstance()->unsubscribe(subscribeId); + subscriptions.removeOne(subscribeId); getContext()->getDriver()->asyncCall( [this, callbackId] { @@ -77,3 +80,9 @@ void DoricNotificationPlugin::unsubscribe(QString jsValueString, }, DoricThreadMode::JS); } + +DoricNotificationPlugin::~DoricNotificationPlugin() { + foreach (QString subscribeId, this->subscriptions) { + DoricGlobalBroadcast::getInstance()->unsubscribe(subscribeId); + } +} diff --git a/doric-Qt/example/doric/plugin/DoricNotificationPlugin.h b/doric-Qt/example/doric/plugin/DoricNotificationPlugin.h index 3422352d..28358fe8 100644 --- a/doric-Qt/example/doric/plugin/DoricNotificationPlugin.h +++ b/doric-Qt/example/doric/plugin/DoricNotificationPlugin.h @@ -5,17 +5,17 @@ #include "DoricNativePlugin.h" -#include - class DORIC_EXPORT DoricNotificationPlugin : public DoricNativePlugin { Q_OBJECT private: - QUdpSocket udpSocket; + QList subscriptions; public: using DoricNativePlugin::DoricNativePlugin; + ~DoricNotificationPlugin(); + Q_INVOKABLE void publish(QString jsValueString, QString callbackId); Q_INVOKABLE void subscribe(QString jsValueString, QString callbackId); diff --git a/doric-Qt/example/doric/utils/DoricGlobalBroadcast.cpp b/doric-Qt/example/doric/utils/DoricGlobalBroadcast.cpp index 05b60e8a..3cc17357 100644 --- a/doric-Qt/example/doric/utils/DoricGlobalBroadcast.cpp +++ b/doric-Qt/example/doric/utils/DoricGlobalBroadcast.cpp @@ -42,7 +42,11 @@ void DoricGlobalBroadcast::unsubscribe(QString subscribeId) { QList>> value = this->subjects[targetKey]; value.removeAt(targetIndex); - this->subjects.insert(targetKey, value); + if (value.size() == 0) { + this->subjects.remove(targetKey); + } else { + this->subjects.insert(targetKey, value); + } } }