add subscription recycle
This commit is contained in:
parent
fa60fd2bc6
commit
59bd3ea1ab
@ -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,
|
||||||
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -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);
|
||||||
|
@ -42,7 +42,11 @@ 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);
|
||||||
this->subjects.insert(targetKey, value);
|
if (value.size() == 0) {
|
||||||
|
this->subjects.remove(targetKey);
|
||||||
|
} else {
|
||||||
|
this->subjects.insert(targetKey, value);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user