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;
callEntity(DoricConstant::DORIC_ENTITY_DESTROY, args);
DoricContextManager::getInstance()->destroyContext(this);
QList<QObject *> plugins = mPluginMap.values();
foreach (QObject *plugin, plugins) { delete plugin; }
}
DoricContext *DoricContext::create(QString script, QString source,

View File

@ -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);
}
}

View File

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

View File

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