What, if anything, determines the order in which subscribers to an observable value are notified?
Looking at the below GitHub issue it seems like there is no reliable order. https://github.com/knockout/knockout/issues/553
However, stepping through the code I see that notifySubscribers() uses forEach() so shouldn't it notify subscribers in the order they are added to the array?
Unfortunately, this doesn't seem to be the case for me in Chrome. I can provide code if needed but these seems like more of general conceptual thing that either Knockout or I am missing.
I solved the issue by adding my order-sensitive code to a single observer function. I.e., the watched value is observed only by handleValueChange() which then calls everything in the desired order.
My take-away from this is that the observation pattern used here is great for updating the View but not so great for structuring imperative business logic.
You can't reliably determine the order in which subscribers are notified. Your subscribers should be independent and not depending on other subscribers, IMO.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With