-
Notifications
You must be signed in to change notification settings - Fork 62
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Promise from Qt Signals helper #11
Comments
I'm not sure I would try to handle multiple signals/slots in one helper with a map as suggested in this comment, because all signals would need to have the same signature which may not fit many use cases? We could indeed expose something similar to QPromiseConnections connections;
return QPromise<void>([&](const auto& resolve, const auto& reject) {
connections << connect(...);
connections << connect(...);
// ...
}.finally([=]() {
connections.disconnect();
}); In case the
I think it should reject with a special reason. I may add a new feature, void QObjectBaseClass::download(const QUrl& url)
{
auto ctx = QPromiseContext(this);
manager->request(url)
.then([ctx](const QByteArray& data) {
ctx->process(data); // throws QPromiseContextException if ctx is null
});
}
It could be handled using |
Allright, I hope I addressed your points to get the PR in a more mature state. You are right in that multiple resolve signals are probably too cumbersome for a general purpose API, so those are off the table. For such corner cases it is indeed much better to give users a tool for handling this conveniently without overcomplicating I renamed the connection guard to I agree that pushing timeout into the API feels a bit rushed and promise cancellation would be the best way to cleanup after timeout. Thinking about it, My previous idea of automatically connecting to That being said, I think this makes supporting multiple rejection signals important since rejection reasons can be manifold (errors of different types occurring, operation canceled, resource destroyed). In contrast to resolve, multiple rejection signals with different types are naturally mapped to the typed In between I added support for automatic |
@pwuertz in my tuples branch I have some helpers in |
I plan to release 0.4 first, then iterate on the signals PR. Actually, I would like to simplify the API a bit more for a first version. I'm also re-organizing helpers ( I would prefer to keep the BTW, I'm on the QtMob Slack if you guys prefer a more interactive exchange. |
Right, I'll simplify and refactor the PR on top of 0.4 once it is settled and get back to you. |
Ok keeping it simple. I reduced the PR to just the |
Is there any update on this? This type of functionality could make our code a lot cleaner. |
@msarehn I totally forgot about #13 (sorry for that @pwuertz). I would like to update a few things before merging but most of the work is here so I hope to get it in before hopefully before the end of the month. The API will remain the same, so you can checkout and test this branch to make sure it works as expected. |
@simonbrunel np ;) Just ping me if there is anything I can help with. I've been using the signal helpers for quite some time now. For what it's worth, they seem quite solid for given use cases, i.e. no further API changes. |
(Small recap of #6 (comment)): When setting up promises from Qt signals, the signal connections used for resolving are tied to the lifetime of the signal source only, i.e. not to the state of the
QPromise
they resolve/reject. This is fine for signal sources that are already designed as promises to be deleted after resolve (likeQNetworkReply
).However, if a signal source is a persistent object there is no intrinsic mechanism for tearing down signal connections after resolve/reject. This is an easy to exploit memory leak and may cause other problems if the lifetime of other captured resources is crucial to an applications logic (RAII).
Unfortunately, keeping track of signal connections and disconnecting them all at once is kind of an ugly task. I ended up creating
ConnectionGuard
for conveniently collecting and disconnecting multiple signals in the context ofQPromise
construction (pwuertz@09a709e):The next step would be a high level API for creating a
QPromise
from resolve and reject signals that automatically connects to and disconnects from signal sources like your suggestion:qPromise(QObject* fulfiller, F fsignal, QObject* rejecter, R rsignal)
I think this could cover a lot of use cases.
However, in some scenarios I still ended up with stale situations where none of the signals would emit. Possible problems are:
destroyed
signal as rejection.So it would seem to me that there is a high chance of requiring more than one reason for rejection, which is why I'd be keen on the idea of allowing multiple rejection signals.
I know you prefer the idea of handling such cases by
race
, but that implies that there must be acancellation
feature as well. This is of course a powerful tool but certainly more difficult to implement and from what I've read cancellation is still a highly discussed topic without general consensus (unlike the A+ pattern).The text was updated successfully, but these errors were encountered: