Add "bool updateSynchronously" to CachedValue<Type>::getPropertyAsValue()?

I find myself in the unfortunate situation that I need to wait on the async update callbacks triggered by a ValueTree before I can proceed with the rest of some code. Is there some way to do this reliably?

This particular piece of code might be called from either the main thread or from a background thread.

I really don’t want to resort to sleeping.

Thanks!

Are the async callbacks always called in the order there were put on the message queue - I recon it’s called queue for a reason? If so, you could try this:

WaitableEvent asyncLoadingIsDone;
MessageManager::callAsync([&]{
    asyncLoadingIsDone.signal();
});

if(MessageManager::getInstance()->isThisTheMessageThread())
{
    while(!asyncLoadingIsDone.wait(1))
    {
        MessageManager::getInstance()->runDispatchLoopUntil(25);
    }
}
else
{
    asyncLoadingIsDone.wait();
}

Maybe you’re referring to something else, but I thought that ValueTree::Listener callbacks were synchronous, not asynchronous.

Thanks for pointing this out, it made me look again how this is working internally. In my case the listeners aren’t registered to the ValueTree itself but to Values that refer to CachedValues which in turn reference the properties of a ValueTree. After jumping through some hoops my call to vt.copyPropertiesFrom(child, &undoManager); internally calls Value::ValueSource::sendChangeMessage(false) which is async. Would be awesome if I could specify at the call side if I want this async or not - but I can’t. I also can’t specify this when obtaining the value.

TL;DR:

I think adding bool updateSynchronously like this would solve this issue:

template <typename Type>
inline Value CachedValue<Type>::getPropertyAsValue(bool 
updateSynchronously=false)
{
    return targetTree.getPropertyAsValue (targetProperty, undoManager, updateSynchronously);
}