Skip to content
This repository has been archived by the owner on Sep 4, 2021. It is now read-only.

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge pull request #12 from nemomobile/staging
[subscriber] do not delete property until unsubscribed
  • Loading branch information
Denis Zalevskiy committed Mar 16, 2015
2 parents d6acdf3 + 32db98f commit 8b6cf7b
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 17 deletions.
46 changes: 30 additions & 16 deletions src/contextkit-subscriber/property.cpp
Expand Up @@ -189,9 +189,9 @@ class UnsubscribeRequest : public Event
, key_(key)
, done_(std::move(done))
{}
virtual ~UnsubscribeRequest() {}

UnsubscribeRequest(SubscribeRequest const&) = delete;
virtual ~UnsubscribeRequest() {
done_.set_value();
}

ContextPropertyPrivate const *tgt_;
QString key_;
Expand All @@ -215,8 +215,6 @@ class WriteRequest : public QObject, public Event
, tgt, &PropertyWriterImpl::updated
, Qt::QueuedConnection);
}

WriteRequest(WriteRequest const &) = delete;
virtual ~WriteRequest() {}

PropertyWriterImpl const *tgt_;
Expand All @@ -235,7 +233,6 @@ class RefreshRequest : public Event
, tgt_(tgt)
, key_(key)
{}
RefreshRequest(RefreshRequest const&) = delete;
virtual ~RefreshRequest() {}

ContextPropertyPrivate const *tgt_;
Expand Down Expand Up @@ -371,12 +368,6 @@ void PropertyMonitor::subscribe(SubscribeRequest *req)

void PropertyMonitor::unsubscribe(UnsubscribeRequest *req)
{
auto on_exit = cor::on_scope_exit([req]() {
execute_nothrow([req]() {
req->done_.set_value();
}, __PRETTY_FUNCTION__);
});

auto tgt = req->tgt_;
auto key = req->key_;

Expand Down Expand Up @@ -634,6 +625,29 @@ ContextPropertyPrivate::ContextPropertyPrivate(const QString &key, QObject *pare
ContextPropertyPrivate::~ContextPropertyPrivate()
{
unsubscribe();
waitForUnsubscription();
}

bool ContextPropertyPrivate::waitForUnsubscription() const
{
static const auto min_timeout = std::chrono::milliseconds(5000);
static const auto count_end = 3;

if (state_ == Initial)
return true;

std::future_status status;
for (auto count = 0; count != count_end; --count) {
// cor::wait_for for compatibility with gcc 4.6
status = cor::wait_for(on_unsubscribed_, min_timeout);
if (status != std::future_status::timeout) {
return true;
} else if (!count) {
debug::warning("Waiting for ages unsubscribing:", key_);
}
}
debug::warning("Timeout unsubscribing:", key_);
return false;
}

QString ContextPropertyPrivate::key() const
Expand Down Expand Up @@ -693,8 +707,8 @@ void ContextPropertyPrivate::subscribe() const

// unsubscription is asynchronous, so wait for it to be finished
// if resubcribing
if (state_ == Unsubscribing)
on_unsubscribed_.wait_for(std::chrono::milliseconds(20000));
if (state_ == Unsubscribing && !waitForUnsubscription())
debug::warning("Resubscribing while not unsubscribed yet");

state_ = Subscribing;
std::promise<QVariant> res;
Expand Down Expand Up @@ -757,7 +771,7 @@ void ContextPropertyPrivate::ignoreCommander()
{
}

void ContextPropertyPrivate::setTypeCheck(bool typeCheck)
void ContextPropertyPrivate::setTypeCheck(bool)
{
}

Expand Down Expand Up @@ -823,7 +837,7 @@ void ContextProperty::ignoreCommander()
{
}

void ContextProperty::setTypeCheck(bool typeCheck)
void ContextProperty::setTypeCheck(bool)
{
}

Expand Down
2 changes: 1 addition & 1 deletion src/contextkit-subscriber/property.hpp
Expand Up @@ -141,7 +141,7 @@ public slots:
};

bool update(QVariant const&) const;

bool waitForUnsubscription() const;
static ckit::PropertyMonitor::monitor_ptr actor();
QString key_;
mutable State state_;
Expand Down

0 comments on commit 8b6cf7b

Please sign in to comment.