diff --git a/doric-Qt/example/doric/DoricRegistry.cpp b/doric-Qt/example/doric/DoricRegistry.cpp index 10ae6dfa..c5b5d11a 100644 --- a/doric-Qt/example/doric/DoricRegistry.cpp +++ b/doric-Qt/example/doric/DoricRegistry.cpp @@ -3,6 +3,7 @@ #include "plugin/DoricModalPlugin.h" #include "plugin/DoricNetworkPlugin.h" +#include "plugin/DoricNotificationPlugin.h" #include "plugin/DoricPopoverPlugin.h" #include "plugin/DoricShaderPlugin.h" #include "plugin/DoricStoragePlugin.h" @@ -29,6 +30,7 @@ DoricRegistry::DoricRegistry() { registerNativePlugin("popover"); registerNativePlugin("network"); registerNativePlugin("storage"); + registerNativePlugin("notification"); registerViewNode("Root"); registerViewNode("Stack"); diff --git a/doric-Qt/example/doric/plugin/DoricNotificationPlugin.cpp b/doric-Qt/example/doric/plugin/DoricNotificationPlugin.cpp index 5c8dc26f..ea859d65 100644 --- a/doric-Qt/example/doric/plugin/DoricNotificationPlugin.cpp +++ b/doric-Qt/example/doric/plugin/DoricNotificationPlugin.cpp @@ -1,6 +1,7 @@ #include "DoricNotificationPlugin.h" #include "engine/DoricPromise.h" +#include "utils/DoricGlobalBroadcast.h" #include #include @@ -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; diff --git a/doric-Qt/example/doric/utils/DoricGlobalBroadcast.cpp b/doric-Qt/example/doric/utils/DoricGlobalBroadcast.cpp index a195cb7e..05b60e8a 100644 --- a/doric-Qt/example/doric/utils/DoricGlobalBroadcast.cpp +++ b/doric-Qt/example/doric/utils/DoricGlobalBroadcast.cpp @@ -1 +1,59 @@ #include "DoricGlobalBroadcast.h" + +QString DoricGlobalBroadcast::subscribe(QString name, + std::function callback) { + int id = this->idGenerator.fetchAndAddAcquire(1); + QPair> pair(QString::number(id), + callback); + if (this->subjects.contains(name)) { + QList>> value = + this->subjects[name]; + value.append(pair); + this->subjects.insert(name, value); + } else { + QList>> value; + value.append(pair); + this->subjects.insert(name, value); + } + + return QString::number(id); +} + +void DoricGlobalBroadcast::unsubscribe(QString subscribeId) { + + QString targetKey; + int targetIndex = -1; + foreach (QString key, this->subjects.keys()) { + QList>> value = + this->subjects[key]; + + for (int i = 0; i != value.size(); i++) { + QPair> pair = value.at(i); + if (pair.first == subscribeId) { + targetKey = key; + targetIndex = i; + } + } + } + + if (targetKey.isEmpty()) { + return; + } else { + QList>> value = + this->subjects[targetKey]; + value.removeAt(targetIndex); + this->subjects.insert(targetKey, value); + } +} + +void DoricGlobalBroadcast::publish(QString name, QString data) { + if (this->subjects.contains(name)) { + QList>> value = + this->subjects[name]; + + for (int i = 0; i != value.size(); i++) { + QPair> pair = value.at(i); + pair.second(data); + } + } +} diff --git a/doric-Qt/example/doric/utils/DoricGlobalBroadcast.h b/doric-Qt/example/doric/utils/DoricGlobalBroadcast.h index e3128aad..057ade81 100644 --- a/doric-Qt/example/doric/utils/DoricGlobalBroadcast.h +++ b/doric-Qt/example/doric/utils/DoricGlobalBroadcast.h @@ -15,6 +15,17 @@ public: static DoricGlobalBroadcast instance; return &instance; } + + QString subscribe(QString name, std::function callback); + + void unsubscribe(QString subscribeId); + + void publish(QString name, QString data); + +private: + QAtomicInt idGenerator; + + QMap>>> subjects; }; #endif // DORICGLOBALBROADCAST_H