Immutability
Problem
* Mutable State *
credit: http://www.memecreator.org/
What is Wrong with Mutable State ?
var dataStore = { key: "value", ... };
renderUI(dataStore);
loadDataIntoStore(url, () => {
renderUI(dataStore);
});
Notice about the Changes ?
var dataStore = { key: "value", ... };
renderUI(dataStore);
loadDataIntoStore(url, () => {
renderUI(dataStore);
});
We never notice how loadDataIntoStore change
our dataStore, thus, we should re - render
the UI again.
Consider this Code
var a = {message: ā€˜Hello’};
var b = {message: ā€˜Hello’};
console.log(a === b); // false
var a = {message: ā€˜Hi’};
b = a;
b.message = ā€˜Hello’;
console.log(a === b); // true
Mutable State Master
Race
Let the data is mutating along the
system is live.
ā€œHISTORYā€
Credit: The Joy Of Clojure ( e - Book )
Fix Code {1}
var dataStore = { key: "value", ... };
renderUI(dataStore);
Object.observe(dataStore, changes => {
renderUI(dataStore, changes);
});
loadDataIntoStore(url);
Fix Code {2}
var data = {
dirty: false,
_raw: { key: ā€œfalseā€ },
get: function(key) {
return this._raw[key];
},
set: function(key, newValue) {
this._raw[key] = newValue;
this.dirty = true;
}
};
function renderUI(data) {
if (!data.dirty) return;
data.dirty = false;
}
ā€œWe have to differentiate something which
Complex and Complicatedā€
ā€œImmutability comes to removes Complicated
Codeā€
Fix Code with Immutable.js
var dataStore = { key: "value", ... };
<script src="immutable.min.js"></script>
<script>
var map1 = Immutable.Map({a:1, b:2, c:3});
var map2 = map1.set('b', 4);
console.log(map1 === map2);
</script>
Immutable in Depth
What makes immutable data
Immutable
DAG ( Directed Acyclic Graph )
Update Process on Persistent Immutable Data Structures

Immutability

  • 1.
  • 2.
  • 3.
  • 4.
    What is Wrongwith Mutable State ? var dataStore = { key: "value", ... }; renderUI(dataStore); loadDataIntoStore(url, () => { renderUI(dataStore); });
  • 5.
    Notice about theChanges ? var dataStore = { key: "value", ... }; renderUI(dataStore); loadDataIntoStore(url, () => { renderUI(dataStore); }); We never notice how loadDataIntoStore change our dataStore, thus, we should re - render the UI again.
  • 6.
    Consider this Code vara = {message: ā€˜Hello’}; var b = {message: ā€˜Hello’}; console.log(a === b); // false var a = {message: ā€˜Hi’}; b = a; b.message = ā€˜Hello’; console.log(a === b); // true
  • 7.
    Mutable State Master Race Letthe data is mutating along the system is live.
  • 8.
  • 9.
    Credit: The JoyOf Clojure ( e - Book )
  • 10.
    Fix Code {1} vardataStore = { key: "value", ... }; renderUI(dataStore); Object.observe(dataStore, changes => { renderUI(dataStore, changes); }); loadDataIntoStore(url);
  • 11.
    Fix Code {2} vardata = { dirty: false, _raw: { key: ā€œfalseā€ }, get: function(key) { return this._raw[key]; }, set: function(key, newValue) { this._raw[key] = newValue; this.dirty = true; } }; function renderUI(data) { if (!data.dirty) return; data.dirty = false; }
  • 12.
    ā€œWe have todifferentiate something which Complex and Complicatedā€
  • 13.
    ā€œImmutability comes toremoves Complicated Codeā€
  • 14.
    Fix Code withImmutable.js var dataStore = { key: "value", ... }; <script src="immutable.min.js"></script> <script> var map1 = Immutable.Map({a:1, b:2, c:3}); var map2 = map1.set('b', 4); console.log(map1 === map2); </script>
  • 15.
    Immutable in Depth Whatmakes immutable data Immutable
  • 16.
    DAG ( DirectedAcyclic Graph ) Update Process on Persistent Immutable Data Structures

Editor's Notes

  • #11Ā Adding some Observation on data and render when the dataStore changed is complicated. Imagine that was not just dataStore, maybe the dataStore representation is a deep map ( map on map ), we have to listen all over the dataStore changes. * yuck *
  • #12Ā when u render for the first time, its ok, because we have dirty data and render the ui, ok it will render, but when it was dirty, ok the renderUI will return the function. But what will happened if we have more than 1 function of render ? terrible mistakes.