How to control order of response by callback functions to an event?
2 views (last 30 days)
I have two listeners for event but according to documentation, "the order in which listeners execute is not defined". I want to check whether one callback has executed before the next without having that callback generate it's own event. Is there a way of doing this?
listener callbacks: UpdateBankAccount, GoShopping
How can I ensure that UpdateBankAccount has ocurred before GoShopping?
TADA on 15 Aug 2021
If you implement the event provider yourself, it is possible to track which event listeners were fired before a specific callback.
You can implement a daughter class of event.EventData (I called it TrackableEventData here)
then you can add functionality for callbacks to subscribe once they are finished so that subsequent callbacks can check wheter a specific callback was fired already.
I made a class with the event PayDay (PayDayClass) this class will raise the PayDay event and send a copy of TrackableEventData in stead of the usual event.EventData that is generated by default (when you don't send anything particular).
The last class has both callbacks - UpdateBankAccount and GoShopping (I assume you have different classes for those callbacks but it doesn't matter for the sake of the example)
when the callbacks happen in the expected order, everything is fine, but when the order is wrong, an exception is thrown by the GoShopping callback.
when making the example, I noticed that the event listeners always fired in LIFO order. As far as I can say, this behavior is not documented. Therefore, it may change between Matlab releases. Allthough, it could be just because it's almost impossible to track the order of registration of listeners in more complex use cases. Either way, I would'nt rely on this.
As far as I know, it is impossible to control the order of callbacks by any other means. So, If you want to control the order of callbacks, rather than just track which ones had already fired, you will have to implement your own event listeners. While it is possible, it is by far more complex.