-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
869 lines (776 loc) · 26.6 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>reveal.js - The HTML Presentation Framework</title>
<meta name="apple-mobile-web-app-capable" content="yes" />
<meta name="apple-mobile-web-app-status-bar-style" content="black-translucent" />
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
<link rel="stylesheet" href="css/reveal.min.css">
<link rel="stylesheet" href="css/theme/night.css" id="theme">
<!-- For syntax highlighting -->
<link rel="stylesheet" href="lib/css/zenburn.css">
<!-- If the query includes 'print-pdf', use the PDF print sheet -->
<script>
document.write( '<link rel="stylesheet" href="css/print/' + ( window.location.search.match( /print-pdf/gi ) ? 'pdf' : 'paper' ) + '.css" type="text/css" media="print">' );
</script>
<!--[if lt IE 9]>
<script src="lib/js/html5shiv.js"></script>
<![endif]-->
<style>
.reveal section img {
border: none;
background: none;
}
.reveal pre {
margin: 5px;
height: auto;
width: 100%;
}
.reveal pre code {
font-size: 1.3em;
line-height: 1.3em;
background: none;
max-height: none;
}
table.timeline {
border-collapse: collapse;
border: 0;
width: 100%;
border: 0px solid black;
}
table.timeline td {
padding: 2px 20px;
color: white;
font-size: 9.5pt;
text-align: center;
}
</style>
</head>
<body>
<div class="reveal">
<!-- Any section element inside of this container is displayed as a slide -->
<div class="slides">
<section>
<h3>Bert Belder</h3>
<br>
<p>
aka @piscisaureus
</p>
<br/>
<p>
node and libuv and strongloop
</p>
</section>
<section>
<h2></h2>
<p class="fragment"><q>"Future of asynchronous programming in node"</q></p>
<p class="fragment"><q>"The future of node streams. We need to talk"</q></p>
<p class="fragment"><q>"Callbacks as our Generations' Go To Statement"</q></p>
<aside class="notes">
So this talk has two parts.
The first part is kind of abstract and it's a lot of words. The second part will be concrete but I'd ask you to pay attention during the abstract part so you understand where I come from.
<br/>
So at times when I should be working on execSync.
In would visit websites.
Like hacker news.
And like the node mailing list.
And there's people there and they are ranting and trolling and gossiping and being stupid.
So here are some quotes.
</aside>
</section>
<section>
<h2>node 2</h2>
<aside class="notes">
This was the node summer of bikeshedding.
I'm sorry for being so northern hemisphere normative btw.
In fact it was so bad that Isaac could barely keep himself under control.
<br/>
But you know what.
These people have a point.
<br/>
Even if we know what Node one zero is going to look like, it's not done.
There will be a successor to node one no matter what it's called.
So let's get the discussion started.
</aside>
</section>
<section>
<section>
<h2></h2>
<ul>
<li>Double / missed callbacks</li>
<li>Ad-hoc ironing out of flow control kinks</li>
<li>Not handling errors (effectively)</li>
<li>To nextTick or not nextTick</li>
</ul>
<aside class="notes">
So here's a bunch of things that people struggle with and cause bugs in node code.
I was opportunistic and I picked issues that I think we can solve.
There are others of course like 64-bit integers and the christmas tree problem but that's not what my talk is about.
</aside>
</section>
<section>
<pre>
<code class="javascript" data-trim>
var waiting = 2;
stream1.on('close', function(err) {
if (--waiting === 0)
nextStep();
});
process.on('exit', function() {
if (--waiting === 0)
nextStep();
});
</code>
</pre>
</section>
<section>
<pre>
<code class="javascript" style="font-size: 1em" data-trim>
var Url = require('url'),
Http = require('http'),
Fs = require('fs');
function download(url, filename, cb) {
var urlObject = Url.parse(url),
file;
var request = Http.get(urlObject, function(res) {
if (res.statusCode != 200) {
makeCallback(new Error("HTTP Error: " + res.statusCode));
return;
}
file = Fs.createWriteStream(filename);
res.pipe(file);
file.on('error', function() {
file.destroy();
});
file.on('close', function() {
file = null;
makeCallback();
});
});
request.on('error', function(err) {
if (file) file.destroy();
makeCallback(err);
});
function makeCallback(err) {
if (!cb) return;
cb(err);
cb = null;
}
}
</code>
</pre>
</section>
</section>
<section>
<p class="fragment"><q>Callbacks as our Generations' Go To Statement</q></p>
<aside class="notes">
So callbacks truly *are* the goto statement of our generation.
The difference is that a goto statement expresses what your processor does.
Versus a while loop, a for loop, an if statement encapsulates meaning.
A repetition. A decision.
Nobody will argue that a for loop isn't a glorified goto statement.
People prefer "high level" constructs because they express structure.
<br/>
Now node has not only callbacks but also EventEmitters.
So here's a question for you to chew on while I babble on.
What kind of structure does an EventEmitter express?
</aside>
</section>
<section>
<ul>
<li class="fragment">"construct" for working with callbacks</li>
<li class="fragment">express intent over inner workings</li>
<li class="fragment">build <i>robust</i> applications</li>
<li class="fragment">un-opinionated</li>
<li class="fragment">doesn't break existing apps/modules</li>
<li class="fragment">in core!</li>
</ul>
<aside class="notes">
So in the past of course node had promises and they still exist.
And they were ripped out for a reason.
They were too opinionated and bikeshedding prone.
You could do them in userland (or so people would argue) and how that is much better.
Now in reality userland promises didn't really pick up much steam. Sure there's Q and stuff which is a world on it's own.
I think part of the reason for that is that it doesn't actually solve the problem people care about most.
It is mostly about the christmas tree thing but for many people that's the least of their concerns.
And there's Mikeal and Isaacs.
In my opinion eventually we'll use something akin to coroutines or generators to soothen that pain, but that's not the topic of this talk.
<br/>
So this is what I set out to create.
<br/>
I want to think about it as a language construct. It can't be a language extension but it should be similar. So it should be universal and transcend how node works and what you can do with the standard library.
<br/>
It should help with reasonable error handling. Like domains but proper.
<br/>
Make it as obvious as possible. To the extent that it inevitably has opinionated parts you're welcome to discuss it with me.
<br/>
It should be in core. Now I don't control core so in the meantime I will fork node and try to make something better. If I turn out to be wrong then there's no harm done. Don't get scared - it is not my intention to fork the community too. In fact:
<br/>
It shouldn't break stuff that already works in node. If this were a biology conference I would make it a race and not a species.
</aside>
</section>
<aside class="notes">
So this is the last slide before I go show you some actual code.
</aside>
</section>
<section>
<pre>
<code class="javascript" data-trim>
try_async(callback) {
// Do async stuff.
// Eventually:
succeed_with 42;
// Or you might:
throw new Error('Вы облажался');
} catch_finally_async(err, result) {
// We get here when all async stuff
// in the try_async block is over.
}
</code>
</pre>
</section>
<section>
<pre>
<code class="javascript" data-trim>
task.create(function(callback) {
// Do async stuff.
// Eventually:
callback(null, 42);
// Or you might:
throw new Error('Вы облажался');
}).setCallback(function(err, result) {
// We get here when all async stuff
// in the try_async block is over.
});
</code>
</pre>
</section>
<section>
<pre>
<code class="javascript" data-trim>
task.create(function(callback) {
// Within the outer task.
task.create(function(callback) {
// Within a nested task.
}).setCallback(err, result) {
// Back in the outer task
if (err) throw err;
callback(null, result + 1);
});
setTimeout(function() {
// In the outer task too.
throw new Error("It's too slow");
}, 100);
}).setCallback(function(err, result) {
// In the global task.
});
</code>
</pre>
</section>
<section>
<h2></h2>
<ul>
<li><code class="javascript">fs.readFile('/foo/bar', cb)</code></li>
<li>100 lines of code</li>
</ul>
</section>
<section>
<pre>
<code class="javascript" data-trim>
fs.readFile = function(path, options, callback_) {
...
function afterRead(er, bytesRead) {
if (er) {
return fs.close(fd, function(er2) {
return callback(er);
});
}
...
}
...
}
</code>
</pre>
</section>
<section>
<pre>
<code class="javascript" style="font-size: 1em" data-trim>
fs.readFile = function(path, options, callback_) {
return task.create(function(callback) {
fs.open(path, function(err, fd) {
if (err) throw err;
var result = '', offset = 0;
doRead();
function doRead() {
fs.read(fd, afterRead, offset, 65536);
}
function afterRead(err, data) {
if (err)
throw err;
if (!data.length) // EOF
return fs.close(fd, afterClose);
offset += data.length;
result += data.toString();
doRead();
}
function afterClose(err) {
if (err)
throw err;
callback(null, result);
}
});
}).setCallback(callback_);
};
</code>
</pre>
</section>
<section>
</section>
<section data-background="img/shit.png">
<h1 style="color: #f00">EventEmitter</h1>
<aside class="notes">
It's almost too good to be true, isn't it?
But we have one problem.
</aside>
</section>
<section>
<pre>
<code class="javascript" data-trim>
task.create(function(callback) {
var conn = net.createConnection(80, 'www.google.com');
conn.on('data', function(data) {
callback(null, data);
});
});
</code>
</pre>
</section>
<section>
<pre>
<code class="javascript" data-trim>
var conn = net.createConnection(80, 'www.google.com');
task.create(function(callback) {
conn.on('data', function(data) {
callback(null, data);
});
});
</code>
</pre>
<aside class="notes">
So what if that connection fails or dies?
Or what if it has already failed when you add the data listener?
Or what if it receives an EOF and the data event never comes?
</aside>
</section>
<section>
<h2>EventEmitter -> Resource</h2>
<p>
<ul>
<li class="fragment">All tasks that have a listener on a resource event are 'vulnerable'</li>
<li class="fragment">You <i>can</i> have an error handler within a task.</li>
</ul>
</p>
<pre class="fragment">
<code class="javascript" data-trim>
var conn = net.createConnection(80, 'www.google.com');
task.create(function(callback) {
conn.on('data', function(data) {
callback(null, data);
});
conn.on('error', function(err) {
// You'd only need to do this if you actually wanted to
// handle this - because default action is implied:
throw err;
});
});
</code>
</pre>
<ul>
<li class="fragment">Listeners are 'ended' when the Resource is closed</li>
<li class="fragment">No external .emit()</li>
</ul>
</section>
<section>
<h2></h2>
<p style="text-align: center">
<table class="timeline">
<tbody>
<tr>
<td style="background:#A5A5A5;">First tick</td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td style="background:#221408;">setTimeout</td>
<td style="background:#221408;"></td>
<td style="background:#221408;"></td>
<td style="background:#221408;"></td>
<td style="background:#221408;"></td>
<td style="background:#715624;">cb</td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td style="background:#031B29;">accept</td>
<td style="background:#0b4f7b;">cb</td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td></td>
<td style="background:#1d1025;">read</td>
<td style="background:#1d1025;"></td>
<td style="background:#562f71;">cb</td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
<td style="background:#1d1025;">read</td>
<td style="background:#562f71;">cb</td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
<td style="background:#000d33;">write</td>
<td style="background:#000d33;"></td>
<td style="background:#000d33;"></td>
<td style="background:#002699;">cb</td>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td style="background:#083e1e;">shutdown</td>
<td style="background:#083e1e;"></td>
<td style="background:#083e1e;"></td>
<td style="background:#149849;">cb</td>
</tr>
<tr>
<td></td>
<td style="background:#031B29;">accept</td>
<td style="background:#0b4f7b;">cb</td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td></td>
<td></td>
<td style="background:#1d1025;">read</td>
<td style="background:#1d1025;"></td>
<td style="background:#1d1025;"></td>
<td style="background:#1d1025;"></td>
<td style="background:#562f71;">cb</td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td style="background:#000d33;">write</td>
<td style="background:#000d33;"></td>
<td style="background:#002699;">cb</td>
<td></td>
<td></td>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td style="background:#083e1e;">shutdown</td>
<td style="background:#083e1e;"></td>
<td style="background:#083e1e;"></td>
<td style="background:#149849;">cb</td>
<td></td>
</tr>
</tbody>
</table>
</p>
</section>
<section>
<h2></h2>
<p style="text-align: center">
<table class="timeline">
<colgroup>
<col span=4></col>
<col style="border: 2px solid red;"></col>
</colgroup>
<tbody>
<tr>
<td style="background:#A5A5A5;">First tick</td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td style="background:#221408;">setTimeout</td>
<td style="background:#221408;"></td>
<td style="background:#221408;"></td>
<td style="background:#221408;"></td>
<td style="background:#221408;"></td>
<td style="background:#715624;">cb</td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td style="background:#031B29;">accept</td>
<td style="background:#0b4f7b;">cb</td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td></td>
<td style="background:#1d1025;">read</td>
<td style="background:#1d1025;"></td>
<td style="background:#562f71;">cb</td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
<td style="background:#1d1025;">read</td>
<td style="background:#562f71;">cb</td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
<td style="background:#000d33;">write</td>
<td style="background:#000d33;"></td>
<td style="background:#000d33;"></td>
<td style="background:#002699;">cb</td>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td style="background:#083e1e;">shutdown</td>
<td style="background:#083e1e;"></td>
<td style="background:#083e1e;"></td>
<td style="background:#149849;">cb</td>
</tr>
<tr>
<td></td>
<td style="background:#031B29;">accept</td>
<td style="background:#0b4f7b;">cb</td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td></td>
<td></td>
<td style="background:#1d1025;">read</td>
<td style="background:#1d1025;"></td>
<td style="background:#1d1025;"></td>
<td style="background:#1d1025;"></td>
<td style="background:#562f71;">cb</td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td style="background:#000d33;">write</td>
<td style="background:#000d33;"></td>
<td style="background:#002699;">cb</td>
<td></td>
<td></td>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td style="background:#083e1e;">shutdown</td>
<td style="background:#083e1e;"></td>
<td style="background:#083e1e;"></td>
<td style="background:#149849;">cb</td>
<td></td>
</tr>
</tbody>
</table>
</p>
</section>
<section>
<img src="img/timeline-task-1.png" style="width: 100%">
</section>
<section>
<img src="img/timeline-task-2.png" style="width: 100%">
</section>
<section>
<pre>
<code class="javascript" data-trim>
task.create(function OuterTask(callback) {
task.create(function InnerTask(callback) {
// beep
}).setCallback(err, result) {
// boop
});
setTimeout(function() {
throw new Error("It's too slow");
}, 100);
}).setCallback(function(err, result) {
throw err;
});
</code>
</pre>
</section>
<section>
<pre>
Error: ugly, yellow, no-good keister off my property
Task: InnerTask (lib/server.js:52:3)
at <anonymous> (lib/index.js:15)
at invokeTimer (timer.js:110:21)
at TimerWrap.ontimeout
Task: OuterTask (lib/index.js:1:13)
at Task.<anonymous> (as _complete) (index.js:14:3)
</pre>
</section>
<section>
<h2>Hello nu.js</h2>
<p>
<br/>
<a href="http://github.com/nujs/nu">http://github.com/nujs/nu</a><br/>
<a href="http://nujs.github.io/nodeland-2013">http://nujs.github.io/nodeland-2013</a><br/>
<br/>
</p>
<p>
Bikeshedding workshop!
</p>
</section>
</div>
</div>
<script src="lib/js/head.min.js"></script>
<script src="js/reveal.min.js"></script>
<script>
// Full list of configuration options available here:
// https://github.com/hakimel/reveal.js#configuration
Reveal.initialize({
controls: true,
progress: false,
history: true,
center: true,
theme: Reveal.getQueryHash().theme, // available themes are in /css/theme
transition: Reveal.getQueryHash().transition || 'sweep', // default/cube/page/concave/zoom/linear/fade/none
// Optional libraries used to extend on reveal.js
dependencies: [
{ src: 'lib/js/classList.js', condition: function() { return !document.body.classList; } },
{ src: 'plugin/markdown/marked.js', condition: function() { return !!document.querySelector( '[data-markdown]' ); } },
{ src: 'plugin/markdown/markdown.js', condition: function() { return !!document.querySelector( '[data-markdown]' ); } },
{ src: 'plugin/highlight/highlight.js', async: true, callback: function() { hljs.initHighlightingOnLoad(); } },
{ src: 'plugin/zoom-js/zoom.js', async: true, condition: function() { return !!document.body.classList; } },
{ src: 'plugin/notes/notes.js', async: true, condition: function() { return !!document.body.classList; } },
{ src: 'plugin/leap/leap.js', async: true }
]
});
</script>
</body>
</html>