complete notification plugin

This commit is contained in:
王劲鹏
2021-06-07 21:54:47 +08:00
committed by osborn
parent 4c3b5b1278
commit c8ab358fd7
4 changed files with 106 additions and 2 deletions

View File

@@ -1,6 +1,7 @@
#include "DoricNotificationPlugin.h"
#include "engine/DoricPromise.h"
#include "utils/DoricGlobalBroadcast.h"
#include <QCoreApplication>
#include <QJsonDocument>
@@ -11,6 +12,18 @@ void DoricNotificationPlugin::publish(QString jsValueString,
QJsonDocument document = QJsonDocument::fromJson(jsValueString.toUtf8());
QJsonValue jsValue = document.object();
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);
getContext()->getDriver()->asyncCall(
[this, callbackId] {
QVariantList args;
@@ -24,9 +37,27 @@ void DoricNotificationPlugin::subscribe(QString jsValueString,
QJsonDocument document = QJsonDocument::fromJson(jsValueString.toUtf8());
QJsonValue jsValue = document.object();
getContext()->getDriver()->asyncCall(
[this, callbackId] {
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);
});
getContext()->getDriver()->asyncCall(
[this, callbackId, notificationCallbackId] {
QVariantList args;
args.append(notificationCallbackId);
DoricPromise::resolve(getContext(), callbackId, args);
},
DoricThreadMode::JS);
@@ -37,6 +68,8 @@ void DoricNotificationPlugin::unsubscribe(QString jsValueString,
QJsonDocument document = QJsonDocument::fromJson(jsValueString.toUtf8());
QJsonValue jsValue = document.object();
DoricGlobalBroadcast::getInstance()->unsubscribe(jsValueString);
getContext()->getDriver()->asyncCall(
[this, callbackId] {
QVariantList args;