ListenerList issue: need reliable notifications if adding/removing listeners from within a callback


#1

ListenerList doc says:

If you add or remove listeners from the list during one of the callbacks - i.e. while
it's in the middle of iterating the listeners, then it's guaranteed that no listeners
will be mistakenly called after they've been removed, but it may mean that some of the
listeners could be called more than once, or not at all, depending on the list's order.

For the use case I’m working on, I need reliable callbacks for each listener, regardless of what any other listener in the system may be doing. I’d expect this guarantee from ListenerList in first place, but I can see how it hasn’t been implemented as default due to performance/memory impact. Nevertheless, I’d like to add make this option available where needed.

A simple implementation at the call site would be something like:
// auto lock (listeners.getLock());
auto listenersCopy (listeners.getListeners());
for (auto listener : listenersCopy)
{
if (listeners.contains (listener))
listener->someCallback (this);
}

  1. Is there a good way to optimise this without adding iVars to ListenerList?

  2. Instead of doing it at call site it would be better if there were variants of call()/callChecked() etc. that did the copy. However I cannot subclass ListenerList since the listeners iVar is private. Any chance of adding this to main-line JUCE?

Thanks,
Stefan