2021-06-07 17:12:55 +08:00
|
|
|
#include "DoricNotificationPlugin.h"
|
|
|
|
|
|
|
|
#include "engine/DoricPromise.h"
|
2021-06-07 21:54:47 +08:00
|
|
|
#include "utils/DoricGlobalBroadcast.h"
|
2021-06-07 17:12:55 +08:00
|
|
|
|
|
|
|
#include <QCoreApplication>
|
|
|
|
#include <QJsonDocument>
|
|
|
|
#include <QJsonObject>
|
|
|
|
|
|
|
|
void DoricNotificationPlugin::publish(QString jsValueString,
|
|
|
|
QString callbackId) {
|
|
|
|
QJsonDocument document = QJsonDocument::fromJson(jsValueString.toUtf8());
|
|
|
|
QJsonValue jsValue = document.object();
|
|
|
|
|
2021-06-07 21:54:47 +08:00
|
|
|
QString name = jsValue["name"].toString();
|
|
|
|
|
|
|
|
if (jsValue.toObject().contains("biz")) {
|
|
|
|
QString biz = jsValue["biz"].toString();
|
|
|
|
QString templateName = "__doric__%s1#%s2";
|
|
|
|
name = templateName.replace("%s1", biz).replace("%s2", name);
|
|
|
|
}
|
|
|
|
|
|
|
|
QString data = jsValue["data"].toString();
|
|
|
|
|
|
|
|
DoricGlobalBroadcast::getInstance()->publish(name, data);
|
|
|
|
|
2021-06-07 17:12:55 +08:00
|
|
|
getContext()->getDriver()->asyncCall(
|
|
|
|
[this, callbackId] {
|
|
|
|
QVariantList args;
|
|
|
|
DoricPromise::resolve(getContext(), callbackId, args);
|
|
|
|
},
|
|
|
|
DoricThreadMode::JS);
|
|
|
|
}
|
|
|
|
|
|
|
|
void DoricNotificationPlugin::subscribe(QString jsValueString,
|
|
|
|
QString callbackId) {
|
|
|
|
QJsonDocument document = QJsonDocument::fromJson(jsValueString.toUtf8());
|
|
|
|
QJsonValue jsValue = document.object();
|
|
|
|
|
2021-06-07 21:54:47 +08:00
|
|
|
QString name = jsValue["name"].toString();
|
|
|
|
|
|
|
|
if (jsValue.toObject().contains("biz")) {
|
|
|
|
QString biz = jsValue["biz"].toString();
|
|
|
|
QString templateName = "__doric__%s1#%s2";
|
|
|
|
name = templateName.replace("%s1", biz).replace("%s2", name);
|
|
|
|
}
|
|
|
|
|
|
|
|
QString notificationCallbackId = jsValue["callback"].toString();
|
|
|
|
|
|
|
|
QString subscribeId = DoricGlobalBroadcast::getInstance()->subscribe(
|
|
|
|
name, [this, notificationCallbackId](QString data) {
|
|
|
|
QVariantList args;
|
|
|
|
args.append(data);
|
|
|
|
DoricPromise::resolve(getContext(), notificationCallbackId, args);
|
|
|
|
});
|
2021-06-08 10:08:38 +08:00
|
|
|
subscriptions.append(subscribeId);
|
2021-06-07 21:54:47 +08:00
|
|
|
|
2021-06-07 17:12:55 +08:00
|
|
|
getContext()->getDriver()->asyncCall(
|
2021-06-07 21:54:47 +08:00
|
|
|
[this, callbackId, notificationCallbackId] {
|
2021-06-07 17:12:55 +08:00
|
|
|
QVariantList args;
|
2021-06-07 21:54:47 +08:00
|
|
|
args.append(notificationCallbackId);
|
2021-06-07 17:12:55 +08:00
|
|
|
DoricPromise::resolve(getContext(), callbackId, args);
|
|
|
|
},
|
|
|
|
DoricThreadMode::JS);
|
|
|
|
}
|
|
|
|
|
|
|
|
void DoricNotificationPlugin::unsubscribe(QString jsValueString,
|
|
|
|
QString callbackId) {
|
|
|
|
QJsonDocument document = QJsonDocument::fromJson(jsValueString.toUtf8());
|
|
|
|
QJsonValue jsValue = document.object();
|
|
|
|
|
2021-06-08 10:08:38 +08:00
|
|
|
QString subscribeId = jsValueString;
|
|
|
|
DoricGlobalBroadcast::getInstance()->unsubscribe(subscribeId);
|
|
|
|
subscriptions.removeOne(subscribeId);
|
2021-06-07 21:54:47 +08:00
|
|
|
|
2021-06-07 17:12:55 +08:00
|
|
|
getContext()->getDriver()->asyncCall(
|
|
|
|
[this, callbackId] {
|
|
|
|
QVariantList args;
|
|
|
|
DoricPromise::resolve(getContext(), callbackId, args);
|
|
|
|
},
|
|
|
|
DoricThreadMode::JS);
|
|
|
|
}
|
2021-06-08 10:08:38 +08:00
|
|
|
|
|
|
|
DoricNotificationPlugin::~DoricNotificationPlugin() {
|
|
|
|
foreach (QString subscribeId, this->subscriptions) {
|
|
|
|
DoricGlobalBroadcast::getInstance()->unsubscribe(subscribeId);
|
|
|
|
}
|
|
|
|
}
|