2. Server Side JavaScript
•
1996 Netscape LiveWire
PHP-like.
Fail.
•
Now Node.js on V8.
Event driven, turn based execution.
Win.
3. node.js
•
node.js implements a web server in a
JavaScript event loop.
•
It is a high-performance event pump.
fs.readFile(filename, encoding,
function (err, data) {...})
•
Everything is (or can be) non-
blocking.
•
Except:
–
some synchronous functions
–
require
4. Your stuff runs on both sides
Your stuff Your stuff
YUI3 YUI3
DOM node.js DOM
JS
JS/V8 Browser
5. Turn
•
A turn is started by an external
event, such as the completion of an
asynchronous request, a user action,
or the ticking of the clock.
•
A callback function associated with
the event is called. It runs to
completion. When it returns, the turn
ends.
•
No need for threads. No races. No
deadlocks.
6. The Law of Turns
Never wait.
Never block.
Finish fast.
7. Turns
•
Nothing can ever block, not even I/O.
•
Do not poll. Instead, register a
callback.
•
This is how browsers work.
•
This is how servers should also work.
8. Long running tasks
•
Two solutions for long running
programs:
•
Eteration: Break the task into
multiple turns.
•
Move the task into a separate
process (workers).
9. Threads are evil
•
In systems programming, threads are
a necessary evil.
•
In application programming, threads
are just evil.
•
Threads provide a deceptively simple
model of concurrency.
•
Threads are subject to races and
deadlocks.
14. It is impossible to have
application integrity when
subject to race conditions.
Read - Modify - Write
15. Mutual Exclusion
•
semaphore
•
monitor
•
rendezvous
•
synchronization
•
This used to be operating system
stuff.
•
It has leaked into applications
because of networking and the multi-
18. Remote Procedure Call
•
Combines two great ideas, functions
and networking, producing a really
bad idea.
•
Attempts to isolate programs from
time. The program blacks out.
•
In reading the program, it is by
design difficult to see where time is
lost.
•
This can result in a terrible
experience for the user. Lost time
20. Quiz 1
function funky(o) {
o = null;
}
var x = [];
funky(x); A. null
B. []
C. undefined
alert(x);
D. throw
21. Quiz 2
function swap(a, b) {
var temp = a;
a = b;
b = temp
}
var x = 1, y = 2; A. 1
swap(x, y); B. 2
C. undefined
D. throw
alert(x);
22. Quiz 3
Make a function that puts a value in a
variable when it is finished.
23. Pass the name of the
variable.
function do_it(inputs, name) {
...
eval(name + ' = ' + result);
window[name] = result;
}
•
Not only bad practice, but illegal in
ES5/strict.
25. function do_it(inputs, func) {
...
func(result);
}
•
We pass a function to do_it.
•
do_it cannot abuse the function.
•
The function cannot abuse do_it.
•
func is a callback.
34. Server programming can be
more complicated.
•
Call 20 services, each contributing
material for the page, wait until all
respond, then assemble the result.
•
Call a service, use its result to call
another service, avoiding deeply
nested event handlers.
40. Composition function
makers
paror([requestor…], timeout)
seqor([requestor…], timeout)
mapor([requestor…], timeout)
41. Also see
•
Directing JavaScript with Arrows
www.cs.umd.edu/~mwh/papers/jsarro
ws.pdf
•
Reactive Extensions for JavaScript
http://blogs.msdn.com/b/rxteam/archiv
e/
2010/03/17/reactive-extensions-for-
javascript.aspx