changed RsEvents so that it takes event types when registering handlers, which limits the number of handlers called for each event

This commit is contained in:
csoler 2019-12-29 13:50:54 +01:00
parent dc2f2f5eb4
commit c544b1da7c
No known key found for this signature in database
GPG key ID: 7BCA522266C0804C
19 changed files with 186 additions and 140 deletions

View file

@ -72,8 +72,21 @@ static NewsFeed *instance = NULL;
/** Constructor */
NewsFeed::NewsFeed(QWidget *parent) : MainPage(parent), ui(new Ui::NewsFeed)
{
mEventHandlerId =0; // needed to force intialization by registerEventsHandler()
rsEvents->registerEventsHandler( [this](std::shared_ptr<const RsEvent> event) { handleEvent(event); }, mEventHandlerId );
mEventTypes = {
RsEventType::AUTHSSL_CONNECTION_AUTENTICATION,
RsEventType::PEER_CONNECTION ,
RsEventType::GXS_CIRCLES ,
RsEventType::GXS_CHANNELS ,
RsEventType::GXS_FORUMS ,
RsEventType::GXS_POSTED ,
RsEventType::MAIL_STATUS
};
for(uint32_t i=0;i<mEventTypes.size();++i)
{
mEventHandlerIds.push_back(0); // needed to force intialization by registerEventsHandler()
rsEvents->registerEventsHandler(mEventTypes[i], [this](std::shared_ptr<const RsEvent> event) { handleEvent(event); }, mEventHandlerIds.back() );
}
/* Invoke the Qt Designer generated object setup routine */
ui->setupUi(this);
@ -117,7 +130,8 @@ QString hlp_str = tr(
NewsFeed::~NewsFeed()
{
rsEvents->unregisterEventsHandler(mEventHandlerId);
for(uint32_t i=0;i<mEventHandlerIds.size();++i)
rsEvents->unregisterEventsHandler(mEventHandlerIds[i]);
// save settings
processSettings(false);
@ -190,7 +204,7 @@ void NewsFeed::handleEvent_main_thread(std::shared_ptr<const RsEvent> event)
if(event->mType == RsEventType::GXS_POSTED && (flags & RS_FEED_TYPE_POSTED))
handleMailEvent(event);
if(event->mType == RsEventType::MAIL_STATUS_CHANGE && (flags & RS_FEED_TYPE_MSG))
if(event->mType == RsEventType::MAIL_STATUS && (flags & RS_FEED_TYPE_MSG))
handlePostedEvent(event);
}
@ -202,7 +216,7 @@ void NewsFeed::handleMailEvent(std::shared_ptr<const RsEvent> event)
switch(pe->mMailStatusEventCode)
{
case RsMailStatusEvent::NEW_MESSAGE:
case RsMailStatusEventType::NEW_MESSAGE:
for(auto msgid: pe->mChangedMsgIds)
addFeedItem( new MsgItem(this, NEWSFEED_MESSAGELIST, msgid, false));
break;

View file

@ -118,7 +118,8 @@ private:
/* UI - from Designer */
Ui::NewsFeed *ui;
RsEventsHandlerId_t mEventHandlerId;
std::vector<RsEventsHandlerId_t> mEventHandlerIds;
std::vector<RsEventType> mEventTypes;
};
#endif

View file

@ -178,7 +178,7 @@ NewFriendList::NewFriendList(QWidget *parent) : /* RsAutoUpdatePage(5000,parent)
ui->filterLineEdit->showFilterIcon();
mEventHandlerId=0; // forces initialization
rsEvents->registerEventsHandler( [this](std::shared_ptr<const RsEvent> e) { handleEvent(e); }, mEventHandlerId );
rsEvents->registerEventsHandler( RsEventType::PEER_CONNECTION, [this](std::shared_ptr<const RsEvent> e) { handleEvent(e); }, mEventHandlerId );
mModel = new RsFriendListModel();
mProxyModel = new FriendListSortFilterProxyModel(ui->peerTreeWidget->header(),this);
@ -258,13 +258,10 @@ NewFriendList::NewFriendList(QWidget *parent) : /* RsAutoUpdatePage(5000,parent)
void NewFriendList::handleEvent(std::shared_ptr<const RsEvent> e)
{
if(e->mType == RsEventType::PEER_CONNECTION)
{
// /!\ The function we're in is called from a different thread. It's very important
// to use this trick in order to avoid data races.
// /!\ The function we're in is called from a different thread. It's very important
// to use this trick in order to avoid data races.
RsQThreadUtils::postToObject( [=]() { forceUpdateDisplay() ; }, this ) ;
}
RsQThreadUtils::postToObject( [=]() { forceUpdateDisplay() ; }, this ) ;
}
NewFriendList::~NewFriendList()

View file

@ -52,26 +52,23 @@ GxsChannelDialog::GxsChannelDialog(QWidget *parent)
{
mEventHandlerId = 0;
// Needs to be asynced because this function is likely to be called by another thread!
rsEvents->registerEventsHandler( [this](std::shared_ptr<const RsEvent> event) { RsQThreadUtils::postToObject( [=]() { handleEvent_main_thread(event); }, this ); }, mEventHandlerId );
rsEvents->registerEventsHandler(RsEventType::GXS_CHANNELS, [this](std::shared_ptr<const RsEvent> event) { RsQThreadUtils::postToObject( [=]() { handleEvent_main_thread(event); }, this ); }, mEventHandlerId );
}
void GxsChannelDialog::handleEvent_main_thread(std::shared_ptr<const RsEvent> event)
{
if(event->mType == RsEventType::GXS_CHANNELS)
{
const RsGxsChannelEvent *e = dynamic_cast<const RsGxsChannelEvent*>(event.get());
const RsGxsChannelEvent *e = dynamic_cast<const RsGxsChannelEvent*>(event.get());
if(!e)
return;
if(!e)
return;
switch(e->mChannelEventCode)
{
case RsGxsChannelEvent::ChannelEventCode::SUBSCRIBE_STATUS_CHANGED: updateDisplay(true);
break;
default:
break;
}
}
switch(e->mChannelEventCode)
{
case RsGxsChannelEvent::ChannelEventCode::SUBSCRIBE_STATUS_CHANGED: updateDisplay(true);
break;
default:
break;
}
}
GxsChannelDialog::~GxsChannelDialog()

View file

@ -132,31 +132,28 @@ GxsChannelPostsWidget::GxsChannelPostsWidget(const RsGxsGroupId &channelId, QWid
mEventHandlerId = 0;
// Needs to be asynced because this function is likely to be called by another thread!
rsEvents->registerEventsHandler( [this](std::shared_ptr<const RsEvent> event) { RsQThreadUtils::postToObject( [=]() { handleEvent_main_thread(event); }, this ); }, mEventHandlerId );
rsEvents->registerEventsHandler(RsEventType::GXS_CHANNELS, [this](std::shared_ptr<const RsEvent> event) { RsQThreadUtils::postToObject( [=]() { handleEvent_main_thread(event); }, this ); }, mEventHandlerId );
}
void GxsChannelPostsWidget::handleEvent_main_thread(std::shared_ptr<const RsEvent> event)
{
if(event->mType == RsEventType::GXS_CHANNELS)
{
const RsGxsChannelEvent *e = dynamic_cast<const RsGxsChannelEvent*>(event.get());
const RsGxsChannelEvent *e = dynamic_cast<const RsGxsChannelEvent*>(event.get());
if(!e)
return;
if(!e)
return;
switch(e->mChannelEventCode)
{
case RsGxsChannelEvent::ChannelEventCode::UPDATED_CHANNEL:
case RsGxsChannelEvent::ChannelEventCode::NEW_CHANNEL:
case RsGxsChannelEvent::ChannelEventCode::UPDATED_MESSAGE:
case RsGxsChannelEvent::ChannelEventCode::NEW_MESSAGE:
if(e->mChannelGroupId == mChannelGroupId)
updateDisplay(true);
break;
default:
break;
}
}
switch(e->mChannelEventCode)
{
case RsGxsChannelEvent::ChannelEventCode::UPDATED_CHANNEL:
case RsGxsChannelEvent::ChannelEventCode::NEW_CHANNEL:
case RsGxsChannelEvent::ChannelEventCode::UPDATED_MESSAGE:
case RsGxsChannelEvent::ChannelEventCode::NEW_MESSAGE:
if(e->mChannelGroupId == mChannelGroupId)
updateDisplay(true);
break;
default:
break;
}
}
GxsChannelPostsWidget::~GxsChannelPostsWidget()

View file

@ -436,31 +436,28 @@ GxsForumThreadWidget::GxsForumThreadWidget(const RsGxsGroupId &forumId, QWidget
mEventHandlerId = 0;
// Needs to be asynced because this function is likely to be called by another thread!
rsEvents->registerEventsHandler( [this](std::shared_ptr<const RsEvent> event) { RsQThreadUtils::postToObject( [=]() { handleEvent_main_thread(event); }, this ); }, mEventHandlerId );
rsEvents->registerEventsHandler(RsEventType::GXS_FORUMS, [this](std::shared_ptr<const RsEvent> event) { RsQThreadUtils::postToObject( [=]() { handleEvent_main_thread(event); }, this ); }, mEventHandlerId );
}
void GxsForumThreadWidget::handleEvent_main_thread(std::shared_ptr<const RsEvent> event)
{
if(event->mType == RsEventType::GXS_FORUMS)
{
const RsGxsForumEvent *e = dynamic_cast<const RsGxsForumEvent*>(event.get());
const RsGxsForumEvent *e = dynamic_cast<const RsGxsForumEvent*>(event.get());
if(!e)
return;
if(!e)
return;
switch(e->mForumEventCode)
{
case RsGxsForumEvent::ForumEventCode::UPDATED_FORUM:
case RsGxsForumEvent::ForumEventCode::NEW_FORUM:
case RsGxsForumEvent::ForumEventCode::UPDATED_MESSAGE:
case RsGxsForumEvent::ForumEventCode::NEW_MESSAGE:
if(e->mForumGroupId == mForumGroup.mMeta.mGroupId)
updateDisplay(true);
break;
default:
break;
}
}
switch(e->mForumEventCode)
{
case RsGxsForumEvent::ForumEventCode::UPDATED_FORUM:
case RsGxsForumEvent::ForumEventCode::NEW_FORUM:
case RsGxsForumEvent::ForumEventCode::UPDATED_MESSAGE:
case RsGxsForumEvent::ForumEventCode::NEW_MESSAGE:
if(e->mForumGroupId == mForumGroup.mMeta.mGroupId)
updateDisplay(true);
break;
default:
break;
}
}
void GxsForumThreadWidget::blank()

View file

@ -45,26 +45,23 @@ GxsForumsDialog::GxsForumsDialog(QWidget *parent)
mEventHandlerId = 0;
// Needs to be asynced because this function is likely to be called by another thread!
rsEvents->registerEventsHandler( [this](std::shared_ptr<const RsEvent> event) { RsQThreadUtils::postToObject( [=]() { handleEvent_main_thread(event); }, this ); }, mEventHandlerId );
rsEvents->registerEventsHandler(RsEventType::GXS_FORUMS, [this](std::shared_ptr<const RsEvent> event) { RsQThreadUtils::postToObject( [=]() { handleEvent_main_thread(event); }, this ); }, mEventHandlerId );
}
void GxsForumsDialog::handleEvent_main_thread(std::shared_ptr<const RsEvent> event)
{
if(event->mType == RsEventType::GXS_FORUMS)
{
const RsGxsForumEvent *e = dynamic_cast<const RsGxsForumEvent*>(event.get());
const RsGxsForumEvent *e = dynamic_cast<const RsGxsForumEvent*>(event.get());
if(!e)
return;
if(!e)
return;
switch(e->mForumEventCode)
{
case RsGxsForumEvent::ForumEventCode::SUBSCRIBE_STATUS_CHANGED: updateDisplay(true);
break;
default:
break;
}
}
switch(e->mForumEventCode)
{
case RsGxsForumEvent::ForumEventCode::SUBSCRIBE_STATUS_CHANGED: updateDisplay(true);
break;
default:
break;
}
}
GxsForumsDialog::~GxsForumsDialog()

View file

@ -39,11 +39,9 @@ RsGxsUpdateBroadcast::RsGxsUpdateBroadcast(RsGxsIfaceHelper *ifaceImpl) :
{
mEventHandlerId = 0; // forces initialization in registerEventsHandler()
rsEvents->registerEventsHandler( [this](std::shared_ptr<const RsEvent> event)
rsEvents->registerEventsHandler(RsEventType::GXS_CHANGES, [this](std::shared_ptr<const RsEvent> event)
{
if(event->mType == RsEventType::GXS_CHANGES)
onChangesReceived(*dynamic_cast<const RsGxsChanges*>(event.get()));
onChangesReceived(*dynamic_cast<const RsGxsChanges*>(event.get()));
}, mEventHandlerId );
}