frisk0
frisk0

Reputation: 259

Explanation of mobx observables

I've recently started exploring libraries for state management in JavaScript, and I came across Mobx. I've tried searching through the web unable to find an explanation about what those observables really are. I understand what they do, but I'd like a deeper understanding of how they work. As a library the observables obviously makes things easier for us without having to reason about them much, but I still feel like I'd like an explanation.

Is Mobx "just" an implementation of the observable pattern, or does it deviate in some manner?

Upvotes: 0

Views: 154

Answers (1)

arthurakay
arthurakay

Reputation: 5651

If you really want to know how something works, it's often best just to look at the code.

I'm no expert on the "observer pattern", but using @zvona's link it seems to me that "yes, mobx is just an implementation of the observer pattern."

Rationale based on that post:

  • Defines a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically.

    • note that IObservable has a property named observers
  • Observers explicitly ask Observables to add them to the observer list the Observable maintains.

    • I'd say this is definitely something that happens in MobX via addObserver
    • Any new observer added to observable.observers array is of type IDerivation
    • Note the comment in that file pointing you to An in-depth explanation of MobX

Does [MobX] deviate [from the observer pattern] in some manner?

I already stated I'm not an expert in the textbook definition of the observer pattern. I couldn't find anything written by Michel Weststrate explicitly stating that MobX is an implementation of this pattern (not that I looked all that hard), but I'm going to go out on a limb and suggest that (like most JavaScript libraries) there is some deviation from the textbook. But I don't know if it's worth nitpicking those.

Upvotes: 1

Related Questions