Angular & MobX,
Where magic happens
/d100f5338cd345b19083721981040c9b/image.png)

-
Laurent Wroblewski
-
Full Stack developer
-
Davidson consulting
/d100f5338cd345b19083721981040c9b/image.png)
/d100f5338cd345b19083721981040c9b/image.png)
-
Why am I here?
-
Because Angular is a lier.
-
It does not do everything for us.
-
Nothing about the state management? Really?
/d100f5338cd345b19083721981040c9b/image.png)
MobX
State Container Manager
...
Like Redux

/d100f5338cd345b19083721981040c9b/image.png)
-
MobX purposes
-
Functional Reactive Programming
-
No brain fucking
-
Less code
-
Freedom
/d100f5338cd345b19083721981040c9b/image.png)

/d100f5338cd345b19083721981040c9b/image.png)
-
Mobx concepts
1) Actions:
-
Event handler
-
Incoming socket
-
...
/d100f5338cd345b19083721981040c9b/image.png)
1) Actions:
-
Updates observable state
-
@action('[USER] ADD') -
useStrict(true)
/d100f5338cd345b19083721981040c9b/image.png)
1) Actions:
-
Transactions:
/d100f5338cd345b19083721981040c9b/image.png)
transaction(() => {
action1();
action2();
});
2) State
-
Any type
-
Do not care about immutability
-
Make data Observable
observable({})
OR
@observable
/d100f5338cd345b19083721981040c9b/image.png)
2) State:
-
@observable/*.deep*/
-
@observable.ref
-
@observable.shallow
/d100f5338cd345b19083721981040c9b/image.png)
2) State:
class Store {
@observable collection1 = []
@observable.ref collection2 = []
@observable.shallow collection3 = []
}
const todos = [{ test: "value }]
const store = new Store()
store.collection1 = todos;
store.collection2 = todos;
store.collection3 = todos;
/d100f5338cd345b19083721981040c9b/image.png)
store.collection1 === todos
FAUX
store.collection1[0] === todos[0]
FAUX
store.collection2 === todos
VRAI
store.collection2[0] === todos[0]
VRAI
store.collection3 === todos
FAUX
store.collection3[0] === todos[0]
VRAI
3) Derivations:
-
@computed
Values based on observable data.
/d100f5338cd345b19083721981040c9b/image.png)
3) Derivations:
-
autorun
-
reaction
-
when
Produce side effects: update UI, log, ie...
/d100f5338cd345b19083721981040c9b/image.png)
MobX Efficiency
-
Only computed values used in reactions are kept in sync with the observable state.
-
Complex dependency tree
/d100f5338cd345b19083721981040c9b/image.png)
How to use?
-
npm i --save mobx mobx-angular
-
import MobxAngularModule
- That's all.
... or...
-
npm i --save mobx
/d100f5338cd345b19083721981040c9b/image.png)
Need help?
-
mobxAngularDebug(true)
-
trace() (or whyRun())
- Plugin MobX developer tools
/d100f5338cd345b19083721981040c9b/image.png)
Demo time...

-
So, suited for me?
-
+
-
Less refactoring and boilerplate
-
Easy to use and to understand
-
-
-
Coding conventions are mandatory
-
Little to medium dev teams?
-
rxjs, where are you?
/d100f5338cd345b19083721981040c9b/image.png)
Any questions?
Thanks for your time.
Have a nice beer.
/d100f5338cd345b19083721981040c9b/image.png)
Angular & MobX
By Laurent WROBLEWSKI
Angular & MobX
- 457