You open a url (www.example.com)
A request is sent to the server
The server reads the request and based on the url it chooses what middleware to run
The middleware sends a request for some state in the database, or requests a change in the database
When the database responds the controller produces the HTML view using a template, passing in any state from the database
The browser gets the response and renders the HTML
In the traditional MVC state was solely contained in the database and the process to access it was pretty straightforward.
Your view has to make a request to get or change any application state. The controller will always control this process of getting or changing application state. The model is the only place you have application state.
The only way to mutate the state is to emit an action, an object describing what happened.
To specify how the state tree is transformed by actions, you write pure reducers.
Actions are payloads of information that send data from your application to your store. They are the only source of information for the store. You send them to the store using store.dispatch().
The reducer is a pure function that takes the previous state and an action, and returns the next state.
It’s called a reducer because it’s the type of function you would pass toArray.prototype.reduce(reducer, ?initialValue). It’s very important that the reducer stays pure.
"A memory leak is a gradual loss of available computer memory. It occurs when a program repeatedly fails to return memory it has obtained for temporary use."
The main notion garbage collection algorithms rely on is the notion of reference. Within the context of memory management, an object is said to reference another object if the former has an access to the latter (either implicitly or explicitly).
An object is considered garbage collectable if there is zero reference pointing at this object.
var o = {
a: {
b:2
}
};
// 2 objects are created. One is referenced by the other as one of its property.
// The other is referenced by virtue of being assigned to the 'o' variable.
// Obviously, none can be garbage-collected
var o2 = o; // the 'o2' variable is the second thing that
// has a reference to the object
o = 1; // now, the object that was originally in 'o' has a unique reference
// embodied by the 'o2' variable
var oa = o2.a; // reference to 'a' property of the object.
// This object has now 2 references: one as a property,
// the other as the 'oa' variable
o2 = "yo"; // The object that was originally in 'o' has now zero
// references to it. It can be garbage-collected.
// However what was its 'a' property is still referenced by
// the 'oa' variable, so it cannot be freed
oa = null; // what was the 'a' property of the object originally in o
// has zero references to it. It can be garbage collected.function f(){
var o = {};
var o2 = {};
o.a = o2; // o references o2
o2.a = o; // o2 references o
return "azerty";
}
f();This algorithm assumes the knowledge of a set of objects called roots (In JavaScript, the root is the global object). Periodically, the garbage-collector will start from these roots, find all objects that are referenced from these roots, then all objects referenced from these, etc.
Starting from the roots, the garbage collector will thus find all reachable objects and collect all non-reachable objects.