To read all comments associated with this story, please click here.
I don't understand: why would the code require a notification each time a data structure changes after having started a big operation on it, instead of asking for a notification once the big operation is over? More important, why would they put a very intensive callback function on something as fine-grained as changing a data structure ?
If you can prove that this is a valid use case and not poor programming practices at work, a feature that gives notifications a "cooldown time" could indeed be the right answer.
Edited 2011-05-28 09:52 UTC
Well, that's a simple MVC pattern at work, which is also a kind of an asynchronous framework. I wouldn't call it a bad programming practice, quite the opposite. It does what it is designed for - separating concerns. The idea is that you do what you want to do on the Model and leave the task of updating the View to the framework.
And it works well too, if only you remember not to use the naive implementation for anything but the simplest models.





Member since:
2009-06-30
When implementing a callback-based API be careful not to fragment the callbacks too much.
Say, user starts an operation on some large data structure and requests notification when that data structure changes. Perhaps the callback function does some expensive tasks, like updating the GUI etc. The operation itself requires a lot of atomic operation to be performed on the data structure, thus it generates a lot of notification events.
If you implemented this naively and update the user after every changes to the data structure (1), the user function would needlessly be called multiple times, consuming a lot of CPU time.
If you notified the user only at the successful completion of the whole task (2), then the user wouldn't be at all aware of the progress of the operation he started.
What you probably want to do instead, is to collect the notifications like in (2) and emit user code notifications periodically + immediately after the whole operation has finished. This probably requires some concurrency in your code but it can be done cooperatively if your main loop supports timers and idle events.