-
Notifications
You must be signed in to change notification settings - Fork 62
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support for promise cancellation #26
Comments
What exactly do you mean by "cancel"? If you just don't want the promise continuations (then/fail) to be invoked, stopping the event loop and destroying all Or are you looking for a |
Yes, I mean such cancellation. But stopping event loop is a bad idea, I think, if user close some dialog with a long time query running. |
Oh ok, by "close window" I thought you meant shutting down the application. So you have some kind of dialog with a long running promise and you want that promise to be cancelled once the window is closed? I think the best option is to connect a lambda to the appropriate dialog signal and call the promise |
What about a I do wonder if it would be useful to NOT invoke the default fail handler on cancel. promise
.then([]() {
// resolve handler
})
.fail([](QtPromise::QPromiseCanceledException) {}) // no fail on cancel
.fail([]() {
// actual fail handler
});
connect(dialog, &QDialog::rejected,
&promise, &QPromise::cancel); |
I support this. Currently, it is a hassle to keep track of future execution. Here is the situation I often encounter in my app: void Widget::getData() {
m_dataFuture.cancel();
QFuture<Data> dataFuture = m_dataProvider->fetchData();
QtPromise::resolve(dataFuture)
.then([this, dataFuture](const Data &data) { if (!future.isCanceled()) setData(data) })
.fail([](){});
m_dataFuture = dataFuture;
}
void Widget::~Widget() {
m_dataFuture.cancel();
} This situation can be easily encountered by user closing widget before execution finishes. If I don't capture actual future and check for its state, app will crash on setting data into widget thats been already destroyed. What I would like to see is a mechanism to cancel/disarm/whatever-you-want-to-call-it all promise callbacks. Pretty often I have to keep track of What would be even better, is to control life-time of the |
I use QPointer like this: QPointer<QObject> local(this);
promise.then([local]() {
if (local.isNull()) {
return;
}
......
}).fail([local]() {
if (local.isNull()) {
return;
}
......
}); This avoid most crash, but it cannot immediately reject the promise when 'close the dialog/widget', and need many if in every |
Would you believe that! I came up with this solution today as well, just couple of hours before your post. After going through library code today, I figured my suggestion wouldn't help too much either and was planning on writing another comment later today. Anyhow, what I figured out, is that when my promise gets fullfilled and handler events are scheduled, my object gets destroyed and this results in a crash. |
When I close window I want to cancel all pending promises, but cannot figure how can do this?
The text was updated successfully, but these errors were encountered: