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.