Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Order when notifying subscribers

Tags:

knockout.js

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.

like image 564
jvhang Avatar asked Aug 25 '26 15:08

jvhang


2 Answers

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.

like image 126
jvhang Avatar answered Aug 28 '26 03:08

jvhang


You can't reliably determine the order in which subscribers are notified. Your subscribers should be independent and not depending on other subscribers, IMO.

like image 34
Bill Gregg Avatar answered Aug 28 '26 05:08

Bill Gregg