add subscription recycle

This commit is contained in:
王劲鹏 2021-06-08 10:08:38 +08:00 committed by osborn
parent fa60fd2bc6
commit 59bd3ea1ab
4 changed files with 20 additions and 5 deletions

View File

@ -19,6 +19,8 @@ DoricContext::~DoricContext() {
QVariantList args; QVariantList args;
callEntity(DoricConstant::DORIC_ENTITY_DESTROY, args); callEntity(DoricConstant::DORIC_ENTITY_DESTROY, args);
DoricContextManager::getInstance()->destroyContext(this); DoricContextManager::getInstance()->destroyContext(this);
QList<QObject *> plugins = mPluginMap.values();
foreach (QObject *plugin, plugins) { delete plugin; }
} }
DoricContext *DoricContext::create(QString script, QString source, DoricContext *DoricContext::create(QString script, QString source,

View File

@ -53,6 +53,7 @@ void DoricNotificationPlugin::subscribe(QString jsValueString,
args.append(data); args.append(data);
DoricPromise::resolve(getContext(), notificationCallbackId, args); DoricPromise::resolve(getContext(), notificationCallbackId, args);
}); });
subscriptions.append(subscribeId);
getContext()->getDriver()->asyncCall( getContext()->getDriver()->asyncCall(
[this, callbackId, notificationCallbackId] { [this, callbackId, notificationCallbackId] {
@ -68,7 +69,9 @@ void DoricNotificationPlugin::unsubscribe(QString jsValueString,
QJsonDocument document = QJsonDocument::fromJson(jsValueString.toUtf8()); QJsonDocument document = QJsonDocument::fromJson(jsValueString.toUtf8());
QJsonValue jsValue = document.object(); QJsonValue jsValue = document.object();
DoricGlobalBroadcast::getInstance()->unsubscribe(jsValueString); QString subscribeId = jsValueString;
DoricGlobalBroadcast::getInstance()->unsubscribe(subscribeId);
subscriptions.removeOne(subscribeId);
getContext()->getDriver()->asyncCall( getContext()->getDriver()->asyncCall(
[this, callbackId] { [this, callbackId] {
@ -77,3 +80,9 @@ void DoricNotificationPlugin::unsubscribe(QString jsValueString,
}, },
DoricThreadMode::JS); DoricThreadMode::JS);
} }
DoricNotificationPlugin::~DoricNotificationPlugin() {
foreach (QString subscribeId, this->subscriptions) {
DoricGlobalBroadcast::getInstance()->unsubscribe(subscribeId);
}
}

View File

@ -5,17 +5,17 @@
#include "DoricNativePlugin.h" #include "DoricNativePlugin.h"
#include <QUdpSocket>
class DORIC_EXPORT DoricNotificationPlugin : public DoricNativePlugin { class DORIC_EXPORT DoricNotificationPlugin : public DoricNativePlugin {
Q_OBJECT Q_OBJECT
private: private:
QUdpSocket udpSocket; QList<QString> subscriptions;
public: public:
using DoricNativePlugin::DoricNativePlugin; using DoricNativePlugin::DoricNativePlugin;
~DoricNotificationPlugin();
Q_INVOKABLE void publish(QString jsValueString, QString callbackId); Q_INVOKABLE void publish(QString jsValueString, QString callbackId);
Q_INVOKABLE void subscribe(QString jsValueString, QString callbackId); Q_INVOKABLE void subscribe(QString jsValueString, QString callbackId);

View File

@ -42,8 +42,12 @@ void DoricGlobalBroadcast::unsubscribe(QString subscribeId) {
QList<QPair<QString, std::function<void(QString)>>> value = QList<QPair<QString, std::function<void(QString)>>> value =
this->subjects[targetKey]; this->subjects[targetKey];
value.removeAt(targetIndex); value.removeAt(targetIndex);
if (value.size() == 0) {
this->subjects.remove(targetKey);
} else {
this->subjects.insert(targetKey, value); this->subjects.insert(targetKey, value);
} }
}
} }
void DoricGlobalBroadcast::publish(QString name, QString data) { void DoricGlobalBroadcast::publish(QString name, QString data) {