add subscription recycle
This commit is contained in:
parent
fa60fd2bc6
commit
59bd3ea1ab
@ -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,
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
@ -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);
|
||||
|
@ -42,8 +42,12 @@ 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) {
|
||||
|
Reference in New Issue
Block a user