benlau/asyncfuture

Reporting "started" state from DeferredFuture

Closed this issue · 4 comments

In my use case, a function compute() that is called by UI classes returns a QFuture. The UI classes use a QFutureWatcher to update their state. They also depend on the started() signal, showing a progress bar when a computation is going on.
The calculation performed inside compute() consists basically of two parts, the first of which may or may not be cached. It looks about like this:

QFuture<double> compute(QVector<QPointF> coords) const
{
	QFuture<System> as = getSystem();
	auto f = [=](System&& system)
	{
		return QtConcurrent::mapped(std::move(coords), Worker(system));
	};

	auto rv = AsyncFuture::observe(as).subscribe(f);
	return rv.future();
}

So I needed the future being returned to trigger the started() signal at some point. The following patch to asyncfuture.h worked for me:

     void complete(QFuture<T> future) {
+     QFutureInterface<T>::reportStarted();
+
        incRefCount();
        auto onFinished = [=]() {
            this->completeByFinishedFuture<T>(future);
            this->decRefCount();
        };
        ...

This feels a little bit like a hack and I'm not sure when exactly the started() signal should be emitted for the most generally useful behaviour, but as the QtConcurrent implementations of QFuture always trigger started, I think DeferredFuture should also do this at some point for consistency.

Hi @daniel-adb-fa ,

Thanks for reporting the issue. Just a quick glance, I think I may let's the track() function also monitor the signals like started, paused, resumed. That should solve your problem.

But before go ahead, I have to digest about the issue carefully and make sure it don't have any side effect. A bit busy on this week, I guess I may start working on this weekend.

Hi @daniel-adb-fa ,

Lucky that I could arrange some time for this issue before weekend. I have committed a fix on this issue to master branch. I think it could solve your problem without changing any code. Please try about it, and let me know the result.

Hi @benlau , thank you! This does the job.

FYI, the fix has been released together with v0.4.

https://github.com/benlau/asyncfuture/releases/tag/v0.4